Studying autocorrelation using R I ran into a brief exposure by Ryan Sheehy named Autocorrelation in R. In this exposure, the topic and the use of the function acf()
are nicely explained and it is illustrated how autocorrelations are in fact lagged correlations. Readers are instructed to run an example that shows that on their data set the result of the acf() function equals the correlation of the variable with its' lagged variable if a correction factor is applied. It works on their example, but in the example I had in mind in the box below, a difference remains.
x<-c(1,4,5,5,7,6,7,7)
## ac according to acf = 0.371
acf(x, plot=FALSE)
## ac from correlations with lagged variable (after correction for (n-1)/n) = 0.692
n<- length(x)
x_t0 <- x[-1]
x_t1 <- x[-n]
cor(x_t1, x_t0) * (n-1)/n
How?