# Durbin-Watson (DW) test (with R code)

## Durbin-Watson (DW) test

- Durbin-Watson (DW) test is a widely used diagnostic method for analyzing the first-order autocorrelation (serial correlation) in regression analysis i.e. residuals are independent of one time to its prior time. First-order autocorrelation refers to the correlation between the successive (present and prior) residuals for the same variable.
- Durbin-Watson test is commonly used on time series dataset (e.g. events measured on different periods) as time series dataset tends to exhibit positive autocorrelation.
- The value of autocorrelation can range from -1 to 1, where -1 to 0 range represents negative autocorrelation whereas the range 0 to 1 represents positive autocorrelation.
- Autocorrelation can cause problems on ordinary least square (OLS) regression analysis which assumes observations are independent of each other. For example, the estimated regression coefficients can be biased for the autocorrelated dataset.

## Durbin-Watson test Hypotheses

Durbin-Watson test analyzes the following hypotheses,

*Null hypothesis* (H_{0}): Residuals from the regression are not autocorrelated (autocorrelation coefficient, ρ = 0)

*Alternative hypothesis* (H_{a}): Residuals from the regression are autocorrelated (autocorrelation coefficient, ρ > 0)

Learn more about hypothesis testing and interpretation

## Durbin-Watson test formula

Durbin-Watson test statistics *d* is given as,

Durbin-Watson test statistics (*d*) ranges from 0 to 4, where values near 2, towards 0, and towards 4 indicate
non-autocorrelation, positive autocorrelation, and negative autocorrelation, respectively.

If *d* < lower critical value (*d*_{L,α}), there is a statistical significance (reject the null hypothesis) for
positive autocorrelation. Similarly, if *d* > upper critical value (*d*_{U,α}), there is no statistical
significance for positive autocorrelation (it can be negative autocorrelation or inconclusive).

The *d*_{L,α} and *d*_{U,α} represents lower and upper critical values for *k* independent variables and
*N* number of observations.

## Perform Durbin-Watson test in R

We will use the `tidyverse`

, `stats`

, and `lmtest`

R packages for this tutorial.

### Dataset

Suppose, we have a hypothetical time series dataset consisting of company stock price over a period of 12 months.

```
# R version 4.1.2 (2021-11-01)
library(tidyverse)
df = read_csv("https://reneshbedre.github.io/assets/posts/reg/stock_price.csv")
head(df, 2)
# output
months stock_price
<dbl> <dbl>
1 1 122
2 2 129
```

#### Fit the regression model

Fit the regression model with `months`

as independent variables and `stock_price`

as the dependent variable,

```
library(stats)
model <- lm(formula = months ~ stock_price, data = df)
model
# output
Call:
lm(formula = months ~ stock_price, data = df)
Coefficients:
(Intercept) stock_price
-16.5623 0.1507
```

Learn more about regression analysis

#### Calculate Durbin-Watson test in R

We will use `dwtest()`

function from `lmtest`

R package,

```
library(lmtest)
dwtest(formula = model, alternative = "two.sided")
# output
data: model
DW = 2.3053, p-value = 0.8007
alternative hypothesis: true autocorrelation is not 0
```

As the *p* value obtained from the Durbin-Watson test
is not significant (*d* = 2.30, *p* = 0.80), we fail to reject the null hypothesis. Hence, we conclude that the residuals
are not positively autocorrelated.

## Enhance your skills with statistical courses using R

## Related reading

- Linear regression basics and implementation in Python
- Multiple linear regression (MLR)
- What is
*p*value and how to calculate*p*value by hand

## References

- Salamon SJ, Hansen HJ, Abbott D. How real are observed trends in small correlated datasets?. Royal Society open science. 2019 Mar 20;6(3):181089.
- Turner SL, Forbes AB, Karahalios A, Taljaard M, McKenzie JE. Evaluation of statistical methods used in the analysis of interrupted time series studies: a simulation study. BMC medical research methodology. 2021 Dec;21(1):1-8.

**If you enhanced your knowledge and practical skills from this article, consider supporting me on**

This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.