Perform Cook-Weisberg test for heteroscedasticity¶
Source: man/performCookWeisbergTest.Rd · Help: ?performCookWeisbergTest
The Cook-Weisberg (1983) score test, with the fitted values as the sole variance regressor by default, or against a variance model given by var_formula. Since 0.14.0 it is the one entry point for the test; performNCVTest is deprecated.
Usage¶
Arguments¶
model- an object of class
lm. data- optional data frame the model was fitted to, in which
var_formulais evaluated; its rows are matched to the residuals by row name. WhenNULL, the default, the model frame ofmodelis used. Withoutvar_formulathe statistic is computed from the residuals and fitted values ofmodel, anddatadoes not change the result. var_formula- optional one-sided formula giving the variance model, evaluated in
data(for example~ x1 + x2). WhenNULL, the default, the fitted values are the variance regressor and the result is what this function returned before 0.14.0; otherwise it is what the deprecatedperformNCVTest(model, var_formula)returned, labels included. In 0.14.0var_formulawas the second argument: a formula passed there by position is still read asvar_formula, with a warning that shows the new call; that form stops working in 1.0.0.
Value¶
An object of class htest containing the score statistic, its single degree of freedom and the p-value.
Details¶
Writing \(\hat{\sigma}^2 = \sum_i \hat{e}_i^2 / n\) for the maximum-likelihood error variance, the test regresses the scaled squared residuals \(\hat{e}_i^2 / \hat{\sigma}^2\) on the fitted values and refers half the explained sum of squares to a chi-square distribution with one degree of freedom. This is the diagnostic returned by Stata's estat hettest with the fitted option, and it is exactly performNCVTest with its default variance model.
The chi-square reference distribution follows from the null variance of \(\hat{e}^2 / \sigma^2\) being 2, which holds under normal errors. When that assumption is doubtful, prefer performKoenkerTest.
Validation¶
Reproduces car::ncvTest() to within 1e-8; see tests/testthat/test-pass-a-reference.R. Releases before 0.7.0 returned \(n R^2\) from regressing the raw squared residuals on the fitted values, which is the studentized (Koenker) statistic rather than the Cook-Weisberg score test.
References¶
Cook, R. D., & Weisberg, S. (1983). Diagnostics for heteroscedasticity in regression. Biometrika, 70(1), 1–10. doi:10.1093/biomet/70.1.1
See also¶
performNCVTest, performKoenkerTest, performBPTest
Examples¶
data(mtcars)
m <- lm(mpg ~ wt + qsec, data = mtcars)
performCookWeisbergTest(m)
# Score test against a specific variance model rather than the fitted values
performCookWeisbergTest(m, var_formula = ~wt)
# With the data, the variance model can use variables the mean model does not
performCookWeisbergTest(m, mtcars, var_formula = ~ wt + hp)