Serial correlation in the residuals

The Durbin-Watson function is used for testing whether there is autocorrelation in the residuals from a linear model or a GLM, and is implemented as part of the car package (see Fox, 2002):

install.packages("car")
model<-lm(response~explanatory)
durbin.watson(model)

lag  Autocorrelation  D-W Statistic p-value
  1      -0.07946739       2.049899   0.874
Alternative hypothesis: rho != 0

There is no evidence of serial correlation in these residuals (p = 0.874).

images

The car package also contains functions for drawing ellipses, including data ellipses, and confidence ellipses for linear and generalized linear models. Here is the data.ellipse function for the present example: by default, the ellipses are drawn at 50 and 90%:

data.ellipse(explanatory,response)

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.