How to test the autocorrelation of the residuals?

I have a matrix with two columns that have many prices (750). In the image below I plotted the residuals of the follow linear regression:

lm(prices[,1] ~ prices[,2])


Looking at image, seems to be a very strong autocorrelation of the residuals.

However how can I test if the autocorrelation of those residuals is strong? What method should I use?

Thank you!

-
You don't need to test for autocorrelation. It is there. The plot shows that. You could look at the autocorrelation function of these residuals (function acf()), but this will simply confirm what can be seen by plain eye: the correlations between lagged residuals are very high. – Wolfgang Aug 28 '11 at 20:23
@Wolfgang, yes, correct, but I have to check it programmatically.. I will take a look at acf function. Thanks! – Dail Aug 28 '11 at 22:12
@Wolfgang, I'm seeing acf() but I don't see a sort of p-value to understand if there is a strong correlation or not. How to interpret its result? Thanks – Dail Aug 28 '11 at 22:16
With H0: correlation (r) = 0, then r follows a normal/t dist with mean 0 and variance of sqrt(number of observations). So you could get the 95% confidence interval using +/- qt(0.75, numberofobs)/sqrt(numberofobs) – Jim Mar 17 at 5:46

There are probably many ways to do this but the first one that comes to mind is based on linear regression. You can regress the consecutive residuals against each other and test for a significant slope. If there is auto-correlation, then there should be a linear relationship between consecutive residuals. To finish the code you've written, you could do:

mod = lm(prices[,1] ~ prices[,2])
res = mod$res n = length(res) mod2 = lm(res[-n] ~ res[-1]) summary(mod2)  mod2 is a linear regression of the time$t$error,$\varepsilon_{t}$, against the time$t-1$error,$\varepsilon_{t-1}$. if the coefficient for res[-1] is significant, you have evidence of autocorrelation in the residuals. Note: This implicitly assumes that the residuals are autoregressive in the sense that only$\varepsilon_{t-1}$is important when predicting$\varepsilon_{t}$. In reality there could be longer range dependencies. In that case, this method I've described should be interpreted as the one-lag autoregressive approximation to the true autocorrelation structure in$\varepsilon$. - thank you so much for the example. Only one doubt, How can I test if res[-1] is significant? – Dail Aug 28 '11 at 22:10 you'd test it the same way you would any other regression coefficient - look at the$t$-statistic and$p\$-value – Macro Aug 28 '11 at 22:22
doing a fast test with: lm(rnorm(1000)~jitter(1:1000)) I get: Residual standard error: 1.006 on 997 degrees of freedom Multiple R-squared: 0.0003463, Adjusted R-squared: -0.0006564 F-statistic: 0.3454 on 1 and 997 DF, p-value: 0.5569 the p-value can't reject the null hypothesis – Dail Aug 28 '11 at 22:29
Macro, I have tested the residuals of the chart I plotted above, and the result is: Residual standard error: 0.04514 on 747 degrees of freedom Multiple R-squared: 0.9241, Adjusted R-squared: 0.924 F-statistic: 9093 on 1 and 747 DF, p-value: < 2.2e-16, It doesn't seem very good, It is very strange because there is a strong autocorrelation, what could I do? – Dail Aug 28 '11 at 22:44
This is called a Breusch-Godfrey test for autocorrelation. – Charlie Sep 7 '11 at 16:53

Use the Durbin-Watson test, implemented in the lmtest package.

dwtest(prices[,1] ~ prices[,2])

-
 very strange I get: p-value < 2.2e-16, How it is possible? the data seems very correlated! – Dail Aug 29 '11 at 7:50 The p-value is the probably of getting as much correlation as that observed if there is no real correlation. So if the p is very small, as it is, that suggests there is a lot of correlation present in the sample. – Rob Hyndman Aug 29 '11 at 10:03 Do you mean a p-value like this indicates that the residuals are very autocorrelated? – Dail Aug 29 '11 at 11:41 Yes............ – Rob Hyndman Aug 29 '11 at 12:56 hmm strange, take a look at: imageshack.us/f/59/17671620.png how is it possible that the right image is not autocorrelated? – Dail Aug 29 '11 at 13:35