35.1 Autocorrelation
Assess autocorrelation from residual
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.00037712, 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,1) errors
#>
#> Coefficients:
#> ma1 intercept time treatment timesincetreat
#> -0.0944 10.1536 14.9993 19.7243 25.0035
#> s.e. 0.0547 0.1279 0.0011 0.1898 0.0018
#>
#> sigma^2 = 1: log likelihood = -515.46
#> AIC=1042.92 AICc=1043.15 BIC=1066.32