# Dashed lines in ACF plot in R

I'm going through the book 'Introductory Time Series with R' by Cowpertwait and Metcalfe. On page 36 Its says the lines are at: $-1/n \pm 2/\sqrt{n}$. I've read here R forum that the lines are at $\pm 1.96/\sqrt{n}$.

I ran the following code :

b = c(3,1,4,1)

acf(b)


and I see that the lines look to appear to be at $\pm 1.96/\sqrt{4}$. So, obviously the book is wrong? Or, Am I misreading what has been written? Are the authors talking about something slightly different?

*Note, I'm not interested in the 1.96 vs 2 minor detail discrepancy. I assume this was just the author using the rule of thumb of 2 sd's versus the actual 1.96 sd.

Edit: I ran this simulation:

acf1 = 0
acf2 = 0
acf3 = 0
for(i in 1:5000){
resids= runif(1000)
residsacf = c(acf(resids,plot= FALSE))
acf1[i] = residsacf$acf[2,,1] acf2[i] = residsacf$acf[3,,1]
acf3[i] = residsacf$acf[4,,1] } meanacf1 = mean(acf1) meanacf2 = mean(acf2) meanacf3 = mean(acf3) meanacf1 meanacf2 meanacf3  I always seem to get values near$1/n$for all 3. Further edit : I'm seeing a trend of$1/n-(k-1)/n^2$- Really,$-\frac{1}{n}\pm \frac{2}{\sqrt{n}}$? Centered at$-\frac{1}{n}$? – mpiktas Oct 31 '11 at 9:58 In Enders' Applied Economic Time Series (2nd edition, pp 67-68) explains that the$2 / \sqrt{N}$comes from Box and Jenkins (1976), Time Series Forecasting, Analysis, and Control. Enders used the following estimate of$var(r_s)$: $$var(r_s) = T^{-1} \left(1+2 \sum_{j=1}^{s-1}r_j^2\right).$$ Enders uses$T$as the length of the series. – Jason Morgan Nov 1 '11 at 0:20 The usual limits are critical values under the null hypothesis of white noise, in which case the variance expression in Enders collapses to$1/T$. – Rob Hyndman Nov 1 '11 at 1:18 Shumway and Stoffer in Time Series Analysis and Its Applications: With R Examples use$\pm 2/\sqrt{N}$as well. See their ACF code available here. – Jason Morgan Nov 1 '11 at 2:01 ## 1 Answer The sample autocorrelation is negatively biased and the first sample autocorrelation coefficient has mean$-1/n$where$n$is the number of observations. But Metcalfe and Cowpertwait are incorrect in saying that all autocorrelation coefficients have that mean, and they are also incorrect in saying that R plots lines at$-1/n \pm 1.96/\sqrt{n}$. Asymptotically the mean is 0 and that is what R uses in plotting the lines at$\pm 1.96/\sqrt{n}$. - Thanks for the response Rob. Am I correct in understanding that the expectation of the ACF at lag 1 is -1/n? If so, shouln't the dashed lines then be centered there for the first lag? Also, since it seems what they wrote wasn't a typo. Do you think they mean something different, or they are simply wrong? I went to their website, and don't see it listed as errata. – Adam Nov 8 '11 at 2:42 For any reasonable sample size,$1/n$is negligible compared to$2/\sqrt{n}\$ so it doesn't matter much. I've corresponded with Andrew Metcalfe and he acknowledged the error with respect to R. I guess they haven't updated the errata yet. – Rob Hyndman Nov 8 '11 at 9:38
Technically, isn't the flaw with R and the authors' assumption R was correct? – Adam Nov 8 '11 at 10:27
There are two problems. First, the mean of -1/n only applies to the first autocorrelation function, but the authors say it applies to all correlation functions. That is their error, not R's. Second, R uses the asymptotic result (as does every other software package I've seen) rather than the small sample result. So R is not wrong, it just uses an approximation that can be improved. – Rob Hyndman Nov 8 '11 at 22:20
is this analogous to calculating sample variance using n in the denominator instead of n-1? – Adam Nov 15 '11 at 2:56
show 1 more comment