Categories &

Functions List

Function Reference: dwtest

statistics: p = dwtest (r, x)
statistics: p = dwtest (r, x, name, value)
statistics: [p, d] = dwtest (…)

Durbin-Watson test for autocorrelation in linear regression residuals.

p = dwtest (r, x) performs the Durbin-Watson test on the residuals r of a linear regression with design matrix x (which should include a column of ones if the model has a constant term). The null hypothesis is that the residuals are uncorrelated, against the alternative that they are autocorrelated. r is an N×1 vector and x is an N×P matrix. p is the p-value of the test.

The Durbin-Watson statistic is $$ d = \sum_{i=1}^{n-1} (r_{i+1} - r_i)^2 / \sum_{i=1}^{n} r_i^2. $$ Values near 2 indicate no autocorrelation, values towards 0 positive autocorrelation, and values towards 4 negative autocorrelation.

p = dwtest (r, x, name, value) specifies additional options using Name-Value pair arguments:

NameValue
'Method''exact' to compute the exact p-value from the null distribution of the statistic (a ratio of quadratic forms, evaluated with Imhof’s method), or 'approximate' to use a normal approximation based on the mean and variance of the statistic. The default is 'exact' for n < 400 and 'approximate' otherwise.
'Tail'The alternative hypothesis: 'both' (default) for a nonzero autocorrelation, 'right' for a positive autocorrelation, or 'left' for a negative autocorrelation.

Source Code: dwtest

[p, d] = dwtest (…) also returns the Durbin-Watson statistic d.

See also: regress, fitlm, runstest

Source Code: dwtest

Test regression residuals for autocorrelation

 x = [ones(20, 1), (1:20)'];
 y = x * [1; 0.5] + sin ((1:20)' / 2);   # add an autocorrelated component
 b = x \ y;
 r = y - x * b;
 [p, d] = dwtest (r, x)
p = 1.7225e-09
d = 0.2556