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:

  • Residuals (Errors): In regression analysis, residuals represent the differences between the observed values and the predicted values by the model.
  • Autocorrelation: Autocorrelation refers to the correlation between residuals across observations. If residuals are positively autocorrelated, it means that the errors in one period are similar to the errors in the previous period, leading to potential biases in the model.
  • Null Hypothesis: The null hypothesis for the Durbin-Watson test is that there is no autocorrelation in the residuals

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:

  • ei represents the residuals.
  • n is the number of observations.

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

  • Durbin-Watson statistic (DW = 1.3624): Since DW < 2, it indicates positive autocorrelation in the residuals.
  • p-value (0.02061): As the p-value is below 0.05, we reject the null hypothesis.
    This confirms that positive autocorrelation is statistically significant.
  • Alternative hypothesis: The test checks whether the residuals show positive autocorrelation

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

Comment

Explore