## Durbin-Watson Test

### Purpose

The Durbin-Watson test assesses whether or not there is autocorrelation among the residuals of time series data.

### Definition

The Durbin-Watson test statistic, `DW`, is

`$DW=\frac{\sum _{i=1}^{n-1}{\left({r}_{i+1}-{r}_{i}\right)}^{2}}{\sum _{i=1}^{n}{r}_{i}^{2}},$`

where ri is the ith raw residual, and n is the number of observations.

### How To

After obtaining a fitted model, say, `mdl`, using `fitlm` or `stepwiselm`, you can perform the Durbin-Watson test using

`dwtest(mdl)`
For details, see the `dwtest` method of the `LinearModel` class.

### Test for Autocorrelation Among Residuals

This example shows how to test for autocorrelation among the residuals of a linear regression model.

Load the sample data and fit a linear regression model.

```load hald mdl = fitlm(ingredients,heat);```

Perform a two-sided Durbin-Watson test to determine if there is any autocorrelation among the residuals of the linear model, `mdl`.

`[p,DW] = dwtest(mdl,'exact','both')`
```p = 0.8421 ```
```DW = 2.0526 ```

The value of the Durbin-Watson test statistic is 2.0526. The $p$-value of 0.8421 suggests that the residuals are not autocorrelated.