Understanding DurbinWatson Test in R (original) (raw)

Understanding Durbin-Watson Test in R

Last Updated : 9 Dec, 2025

The Durbin-Watson test is a statistical test used to detect the presence of autocorrelation (serial correlation) in the residuals of a regression analysis. Autocorrelation occurs when the residuals (errors) are not independent of each other, which violates one of the key assumptions of linear regression. Specifically, the test helps determine if there is a relationship between the errors over time or across observations.

Before diving into the application of the Durbin-Watson test, it is important to understand the key concepts behind the test:

**The Durbin-Watson test statistic is computed using the following formula:

DW = \frac{\sum_{i=2}^{n} (e_i - e_{i-1})^2}{\sum_{i=1}^{n} e_i^2}

**Where:

The test compares the residuals from time t and t-1, determining whether there is a systematic pattern between consecutive residuals. In R, you can easily perform the Durbin-Watson test using the dwtest() function from the lmtest package.

Step 1: Install and Load the Necessary Libraries

If you haven't installed the lmtest package, you can do so using the following command:

R `

install.packages("lmtest")

Load required packages

library(lmtest)

`

Step 2: Fit a Linear Regression Model

To perform the Durbin-Watson test, we need to first fit a linear regression model. Let's use a simple dataset to fit a regression model:

R `

Load the built-in dataset 'mtcars'

data(mtcars)

Fit a linear regression model

model <- lm(mpg ~ wt + hp, data = mtcars)

`

In this example, we are modeling the relationship between mpg (miles per gallon) and the independent variables wt (weight of the car) and hp (horsepower).

Step 3: Perform the Durbin-Watson Test

With the linear regression model fitted, we can now apply the Durbin-Watson test to check for autocorrelation in the residuals:

R `

dw_test <- dwtest(model)

print(dw_test)

`

**Output:

Durbin-Watson test

data: model
DW = 1.3624, p-value = 0.02061
alternative hypothesis: true autocorrelation is greater than 0

Interpretation

The DW value (1.3624) and the p-value (0.02061) together show significant positive autocorrelation in the model’s residuals.