dwtest

Durbin-Watson test

Syntax

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

expand all

Test Residuals For Correlation

Load the sample census data.

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.

One-Sided Hypothesis Test

Load the sample census data.

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

expand all

x — Design matrixmatrix

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

r — Regression residualsvector

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.

'Method' — Computation method for p-value'exact' | 'approximate'

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'

'Tail' — Type of alternative hypothesis'both' (default) | 'right' | 'left'

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

expand all

pp-valuescalar value in the range [0,1]

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.

d — Test statisticnonnegative scalar value

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

More About

expand 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=t=2T(etet1)2t=1Tet2,

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.

See Also

Was this topic helpful?