Temporal autocorrelation and effective sample size

R
time series
autocorrelation
ecology tutorial
How autocorrelation shrinks the effective sample size of an ecological time series, why the naive correlation test between two series is anticonservative, and how to correct it in base R.
Author

Tidy Ecology

Published

2026-08-14

A field ecologist rarely gets independent observations through time. Counts, temperatures, water levels and phenological dates recorded month after month carry over: a warm April makes a warm May more likely, a high vole year props up the next. That carry-over is temporal autocorrelation, and ignoring it does more than annoy a reviewer. It quietly shrinks how much information the series actually holds, and it makes ordinary significance tests reject far too often. This post builds the autocorrelation and partial autocorrelation functions by hand, shows how they translate into an effective sample size, and demonstrates the single most common trap: two unrelated series that appear correlated simply because both are autocorrelated.

We stay in base R throughout. Everything here is stats: arima.sim, acf, pacf, lm, cor.

An autocorrelated series and its correlogram

A first-order autoregressive process, AR(1), is the simplest useful model of carry-over. Each value is a fraction of the previous one plus fresh noise: y[t] = phi * y[t-1] + e[t]. The single parameter phi sets the persistence. We simulate ten years of monthly values with strong persistence.

set.seed(4240)
phi <- 0.7; n <- 120
y <- as.numeric(arima.sim(list(ar = phi), n = n))

K <- 20
acf_h <- acf_hand(y, K)                 # by hand
acf_b <- as.numeric(acf(y, lag.max = K, plot = FALSE)$acf)
pacf_h <- pacf_dl(acf_h, K)
pacf_b <- as.numeric(pacf(y, lag.max = K, plot = FALSE)$acf)

acf_match  <- max(abs(acf_h - acf_b))
pacf_match <- max(abs(pacf_h - pacf_b))

The hand computation reproduces stats::acf to 1.1^{-16} and stats::pacf to 3.8^{-16}, so we understand exactly what the built-in functions return. The estimator is the standard biased one: the lag-k autocovariance is divided by the lag-0 sum, not by n - k.

The two functions read a series in complementary ways. The autocorrelation function (ACF) measures the correlation between the series and a copy of itself shifted by k steps. For an AR(1) process it should decay geometrically as phi^k. The lag-1 sample value here is 0.695, close to the true phi of 0.7. The partial autocorrelation function (PACF) removes the intervening lags, asking what lag k adds once lags 1 to k - 1 are accounted for. For an AR(1) process the PACF has a single spike at lag 1 and is near zero afterwards: here lag 1 is 0.695, lag 2 is -0.012 and lag 3 is 0.106. That contrast, a slow ACF decay against a sharp PACF cutoff, is the signature that tells you the order of an autoregressive model. The next post uses it directly.

Two panels. Left: sample autocorrelation as green stems declining from about 0.7 towards zero, tracking a gold theoretical phi-to-the-k line, with dashed rust lines marking the naive white-noise band; the early lags sit well above the band. Right: partial autocorrelation as green stems with a single tall spike at lag 1 near 0.7 and all later lags close to zero within the band.
Figure 1: Sample ACF and PACF of an AR(1) series. The ACF decays like phi^k; the PACF spikes once at lag 1 then falls silent.

How much information does the series really hold?

The dashed band in the ACF panel is the interval a naive analysis would use to decide whether an autocorrelation is real: plus or minus 1.96 / sqrt(n), built on the assumption that the data are independent. That assumption is exactly what fails here. When observations carry over, each new value repeats part of what the last one already told you, so the series behaves as if it had fewer independent points than its length suggests.

For estimating a mean from an AR(1) series, the variance of the sample mean is inflated relative to the independent case by a factor that depends only on phi:

vif_mean <- (1 + phi) / (1 - phi)
n_eff <- n * (1 - phi) / (1 + phi)

With phi of 0.7, the variance inflation factor is 5.67 and the effective sample size drops from 120 to about 21. Ten years of monthly counts carry roughly the information of 21 independent observations. A short simulation confirms it: we draw many AR(1) series, take each sample mean, and compare its true spread against the naive standard error sd / sqrt(n).

B <- 4000
means <- naive_se <- numeric(B)
set.seed(4240)
for (b in seq_len(B)) {
  z <- as.numeric(arima.sim(list(ar = phi), n = n))
  means[b] <- mean(z)
  naive_se[b] <- sd(z) / sqrt(n)
}
emp_sd <- sd(means)
ratio_se <- emp_sd / mean(naive_se)
cover_naive <- mean(abs(means) <= 1.96 * naive_se)
cover_eff <- mean(abs(means) <= 1.96 * naive_se * sqrt(vif_mean))

The true spread of the sample mean is 0.302, but the naive standard error averages 0.125. The naive figure is too small by a factor of 2.42, which matches sqrt(VIF) of 2.38. The practical damage: a naive 95% confidence interval for the mean covers the truth only 58% of the time, not 95%. Rescaling the standard error by sqrt(VIF), equivalently using the effective sample size, restores coverage to 94%.

What to carry away

Temporal autocorrelation is not a nuisance to be swept into the residuals. It sets how much a series actually tells you. A persistent monthly record can hold the information of a couple of dozen independent points, and any test that assumes independence, whether a confidence interval for a mean, a correlation, or an ordinary regression slope, will be too confident. The effective-degrees-of-freedom correction shown here is the temporal cousin of the spatial adjustment used when neighbouring sites are not independent: same logic, same fix. Detecting that a real ecological relationship exists between two time series takes an honest accounting of the autocorrelation, not just a small p-value.

Two habits follow. First, always look at the correlogram before testing anything on a time series; the ACF and PACF tell you both how strong the carry-over is and what model might describe it. Second, when you compare two series, correct for their autocorrelation, whether through an effective sample size, a generalised least squares fit with an autoregressive error, or the prewhitening approach we return to when checking a time series model. The next post reads the ACF and PACF the other way round, using them to identify and fit an autoregressive-moving-average model.

References

  • Bartlett 1946 Supplement to the Journal of the Royal Statistical Society 8(1):27-41 (10.2307/2983611)
  • Box, Jenkins, Reinsel & Ljung 2015 Time Series Analysis: Forecasting and Control, 5th ed, Wiley, ISBN 978-1-118-67502-1
  • Clifford, Richardson & Hemon 1989 Biometrics 45(1):123-134 (10.2307/2532039)
  • Dutilleul 1993 Biometrics 49(1):305-314 (10.2307/2532625)
  • Legendre & Legendre 2012 Numerical Ecology, 3rd ed, Elsevier, ISBN 978-0-444-53868-0

Newsletter

Get new tutorials by email

New R and QGIS tutorials for ecologists, straight to your inbox. No spam; unsubscribe anytime.

By subscribing you agree to receive these emails and confirm your address once. See the privacy policy.