# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# dwtest

Durbin-Watson test

## Syntax

``p = dwtest(r,x)``
``p = dwtest(r,x,Name,Value)``
``````[p,d] = dwtest(___)``````

## 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

```load census ```

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 = 0 d = 0.1308 ```

The returned value `p = 0` indicates rejection of the null hypothesis at the 5% significance level.

```load census ```

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 = 0 d = 0.1308 ```

The returned value `p = 0` 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 Pair 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 single 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.

collapse all

Computation method for the p-value, specified as the comma-separated pair consisting of `'Method'` and one of the following.

 `'exact'` Calculate an exact p-value using the Pan algorithm. This is the default if the sample size is less than 400. `'approximate'` Calculate the p-value using a normal approximation. 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 is used to test the null hypothesis that linear regression residuals are uncorrelated, against the alternative that autocorrelation exists.

The test statistic for the Durbin-Watson test is

`$d=\frac{\sum _{t=2}^{T}{\left({e}_{t}-{e}_{t-1}\right)}^{2}}{\sum _{t=1}^{T}{e}_{t}^{2}},$`
where T is the number of observations, and et is the residual at time t.

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 correlation among residuals. The p-value can be calculated exactly using the Pan algorithm. Alternatively, the p-value can be estimated using a normal approximation.