Open In App

Understanding Durbin-Watson Test in R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to perform the Durbin-Watson test in R, understand its interpretation, and learn how to apply it in regression analysis using R Programming Language.

What is the Durbin-Watson Test?

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
# Perform the Durbin-Watson test
dw_test <- dwtest(model)

# View the test result
print(dw_test)

Output:

	Durbin-Watson test

data: model
DW = 1.3624, p-value = 0.02061
alternative hypothesis: true autocorrelation is greater than 0
  • Durbin-Watson statistic (DW = 1.679): A value close to 2 indicates no autocorrelation, while values less than 2 suggest positive autocorrelation.
  • p-value (p-value = 0.062): A p-value greater than 0.05 suggests that we fail to reject the null hypothesis, implying no significant autocorrelation in the residuals.
  • Alternative hypothesis: Indicates that the test was conducted to detect positive autocorrelation.

In this example, the Durbin-Watson statistic is 1.679, and the p-value is 0.062, which is just above the 0.05 significance level. Therefore, we do not have enough evidence to reject the null hypothesis, suggesting no significant positive autocorrelation in the residuals.

Conclusion

The Durbin-Watson test is a simple but effective tool to detect autocorrelation in the residuals of a regression model. It is particularly useful when working with time series data or any scenario where residuals may not be independent. In this article, we covered the following:

  • The purpose and importance of the Durbin-Watson test.
  • How to apply the test in R using the lmtest package.
  • How to interpret the results of the test.

If autocorrelation is detected, it is essential to address it by modifying the model, either by including lag variables or by using time-series models like ARIMA.


Article Tags :

Similar Reads