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!
 A: Use the Durbin-Watson test, implemented in the lmtest package.
dwtest(prices[,1] ~ prices[,2])

A: 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$.
A: The DW Test or the Linear Regression test are not robust to anomalies in the data. If you have Pulses, Seasonal Pulses , Level Shifts or Local Time Trends these tests are useless as these untreated components inflate the variance of the errors thus downward biasing the tests causing you ( as you have found out ) to incorrectly accept the null hypothesis of no auto-correlation. Before these two tests or any other parametric test that I am aware of can be used one has to "prove" that the mean of the residuals is not statistically significantly different from 0.0 EVERYWHERE otherwise the underlying assumptions are invalid. It is well known that one of the constraints of the DW test is its assumption that the regression errors are normally distributed. Note normally distributed means among other things : No anomalies ( see http://homepage.newschool.edu/~canjels/permdw12.pdf ). Additionally the DW test only test for auto-correlation of lag 1. Your data might have a weekly/seasonal effect and this would go undiagnosed and furthermore , untreated , would downward bias the DW test.
A: 2021: R Provides an Autocorrelation Function - acf
I'm assuming that the other answers posted were created before the acf function existed in R.
However, in 2021, there is a dedicated function for calculating the autocorrelation.
Here's how to use it:
# calculate the ACF for lags between 1 and 20 (inclusive)
autocorrelation <- acf(your.data.here, lag.max=20, plot=FALSE)

# Plot figure
plot(autocorrelation,
     main="Autocorrelation",
     xlab="Lag Parameter",
     ylab="ACF")

Reference:
https://www.datacamp.com/community/tutorials/autocorrelation-r
