32.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.00037607, 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(3,0,2) errors 
#> 
#> Coefficients:
#>          ar1      ar2     ar3      ma1     ma2  intercept     time  treatment
#>       0.1904  -0.9672  0.0925  -0.1327  0.9557     9.7122  15.0026    19.8588
#> s.e.  0.0693   0.0356  0.0543   0.0467  0.0338     0.1446   0.0012     0.2141
#>       timesincetreat
#>              24.9965
#> s.e.          0.0021
#> 
#> sigma^2 = 0.91:  log likelihood = -496.34
#> AIC=1012.67   AICc=1013.3   BIC=1051.67