## 31.1 Autocorrelation

Assess autocorrelation from residual

# simple regression on time
simple_ts <- lm(outcome ~ time, data = df)

plot(resid(simple_ts))


# alternatively
acf(resid(simple_ts))

This is not the best example since I created this dataset. But when residuals do have autocorrelation, you should not see any patterns (i.e., points should be randomly distributed on the plot)

To formally test for autocorrelation, we can use the Durbin-Watson test

lmtest::dwtest(df$outcome ~ df$time)
#>
#>  Durbin-Watson test
#>
#> data:  df$outcome ~ df$time
#> DW = 0.00037609, p-value < 2.2e-16
#> alternative hypothesis: true autocorrelation is greater than 0

From the p-value, we know that there is autocorrelation in the time series

A solution to this problem is to use more advanced time series analysis (e.g., ARIMA - coming up in the book) to adjust for seasonality and other dependency.

forecast::auto.arima(df$outcome, xreg = as.matrix(df[,-1])) #> Series: df$outcome
#> Regression with ARIMA(0,0,0) errors
#>
#> Coefficients:
#>       intercept     time  treatment  timesincetreat
#>          9.9843  15.0012    19.9123         24.9978
#> s.e.     0.1432   0.0012     0.2123          0.0021
#>
#> sigma^2 = 1.029:  log likelihood = -521.03
#> AIC=1052.07   AICc=1052.23   BIC=1071.57