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:
| Name | Value |
|---|---|
'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. |
[p, d] = dwtest (…) also returns the Durbin-Watson
statistic d.
If x has rank equal to the number of observations, no residual degrees
of freedom remain and d has no null distribution. In that case
p is 0 for 'right' and 'both' and 1 for
'left' with the exact method, and NaN with the approximate
method. A p of exactly 0 or 1 signals this rather than strong
evidence.
If r is empty, p is 0 and d is NaN. MATLAB
returns d = 0 for some empty inputs, a 0×3 array among them,
and NaN for [] and for zero-length vectors; d is
NaN here for every empty r, the statistic being undefined in
all of them alike. MATLAB also accepts a character array as r and
returns the empty result for it; r must be numeric here.
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