# dwtest

Durbin-Watson test with residual inputs

## Description

example

p = dwtest(r,x) returns the p-value for the Durbin-Watson test of the null hypothesis that the residuals from a linear regression are uncorrelated. The alternative hypothesis is that there is autocorrelation among the residuals.

example

p = dwtest(r,x,Name,Value) returns the p-value for the Durbin-Watson test with additional options specified by one or more name-value pair arguments. For example, you can conduct a one-sided test or calculate the p-value using a normal approximation.

example

[p,d] = dwtest(___) also returns the Durbin-Watson test statistic, d, using any of the input arguments from the previous syntaxes.

## Examples

collapse all

Create a design matrix using the census date (cdate) as the predictor. Add a column of 1 values to include a constant term.

n = length(cdate);
x = [ones(n,1),cdate];

Fit a linear regression to the data.

[b,bint,r] = regress(pop,x);

Test the null hypothesis that there is no autocorrelation among the residuals, r.

[p,d] = dwtest(r,x)
p = 3.6190e-15
d = 0.1308

The returned value p = 3.6190e-15 indicates rejection of the null hypothesis at the 5% significance level.

Create a design matrix using the census date (cdate) as the predictor. Add a column of 1 values to include a constant term.

n = length(cdate);
x = [ones(n,1),cdate];

Fit a linear regression to the data.

[b,bint,r] = regress(pop,x);

Test the null hypothesis that there is no autocorrelation among regression residuals, against the alternative hypothesis that the autocorrelation is greater than zero.

[p,d] = dwtest(r,x,'Tail','right')
p = 1.8095e-15
d = 0.1308

The returned value p = 1.8095e-15 indicates rejection of the null hypothesis at the 5% significance level, in favor of the alternative hypothesis that the autocorrelation among residuals is greater than zero.

## Input Arguments

collapse all

Design matrix for a linear regression, specified as a matrix. Include a column of 1 values in the design matrix so the model contains a constant term.

Data Types: single | double

Regression residuals, specified as a vector. Obtain r by performing a linear regression using a function such as regress, or by using the backslash operator.

Data Types: single | double

### Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Tail','right','Method','approximate' specifies a right-tailed hypothesis test and calculates the p-value using a normal approximation.

Algorithm for computing the p-value, specified as the comma-separated pair consisting of 'Method' and one of these values:

 'exact' Calculate an exact p-value using the Pan algorithm [2]. This is the default if the sample size is less than 400. 'approximate' Calculate the p-value using a normal approximation [1]. This is the default if the sample size is 400 or larger.

Example: 'Method','exact'

Type of alternative hypothesis to evaluate, specified as the comma-separated pair consisting of 'Tail' and one of the following.

 'both' Test the alternate hypothesis that autocorrelation among the residuals is not zero. 'right' Test the alternative hypothesis that autocorrelation among the residuals is greater than zero. 'left' Test the alternative hypothesis that autocorrelation among the residuals is less than zero.

Example: 'Tail','right'

## Output Arguments

collapse all

p-value of the test, returned as a scalar value in the range [0,1]. p is the probability of observing a test statistic as extreme as, or more extreme than, the observed value under the null hypothesis. Small values of p cast doubt on the validity of the null hypothesis.

Test statistic of the hypothesis test, returned as a nonnegative scalar value.

collapse all

### Durbin-Watson Test

The Durbin-Watson test tests the null hypothesis that linear regression residuals of time series data are uncorrelated, against the alternative hypothesis that autocorrelation exists.

The test statistic for the Durbin-Watson test 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.

The p-value of the Durbin-Watson test is the probability of observing a test statistic as extreme as, or more extreme than, the observed value under the null hypothesis. A significantly small p-value casts doubt on the validity of the null hypothesis and indicates autocorrelation among residuals.

## Alternative Functionality

• You can create a linear regression model object by using fitlm or stepwiselm and use the object function dwtest to perform the Durbin-Watson test.

A LinearModel object provides the object properties and the object functions to investigate a fitted linear regression model. The object properties include information about coefficient estimates, summary statistics, fitting method, and input data. Use the object functions to predict responses and to modify, evaluate, and visualize the linear regression model.

## References

[1] Durbin, J., and G. S. Watson. "Testing for Serial Correlation in Least Squares Regression I." Biometrika 37, pp. 409–428, 1950.

[2] Farebrother, R. W. Pan's "Procedure for the Tail Probabilities of the Durbin-Watson Statistic." Applied Statistics 29, pp. 224–227, 1980.