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$.
acf()
), but this will simply confirm what can be seen by plain eye: the correlations between lagged residuals are very high. $\endgroup$qt(0.75, numberofobs)/sqrt(numberofobs)
$\endgroup$