Apparent error in R acf calculation

时间:2015-09-14 16:14:31

标签: r

I have a series

D=c(1.7,5.4, 3.8, 9.7, -4.5, -1.1, 0.5)

I simply want to calculate the ACF at various lags for this function. If I run acf(D) in R I get the following: Autocorrelations of series ‘d’, by lag

     0      1      2      3      4      5      6 
 1.000 -0.055 -0.007 -0.339 -0.077 -0.029  0.007 

I went to check this ACF by manually computing it. A very simple way to do ACF is to do the correlation between D and Lag(D,lag_num)

So for example the ACF of D at lag 1 is the correlation between D and lag(D,1). This is widely regarded as an acceptable formula. Yet if you do this

# for the first one
temp_a = c(1.7,5.4,3.8,9.7,-4.5,-1.1) 
temp_b = c(5.4,3.8,9.7,-4.5,-1.1,0.5) 
cor(temp_a,temp_b)

you get the following numbers:

-0.057  0.01575  -0.49937

For lags 1,2,3 respectively.

I am very confused in this discrepancy. Especially since the manual checked numbers make a lot more sense. I know R must be correct on such a simple function but I can't figure out why they are so different. And would appreciate any clarification!

Thanks!

1 个答案:

答案 0 :(得分:3)

我怀疑你的配方已关闭。此手动计算与acf

匹配
x <- D - mean(D)
sum(x[1:6] * x[2:7]) / sum(x^2)
[1] -0.05503743

有关我使用的公式,请参阅http://www.itl.nist.gov/div898/handbook/eda/section3/eda35c.htm

请注意,整个时间序列的样本均值包含在此公式中,在您的实现中将会遗漏。