Skip to content

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

performCookWeisbergTest(model, data = NULL, var_formula = NULL)

Arguments

model
an object of class lm.
data
optional data frame the model was fitted to, in which var_formula is evaluated; its rows are matched to the residuals by row name. When NULL, the default, the model frame of model is used. Without var_formula the statistic is computed from the residuals and fitted values of model, and data does not change the result.
var_formula
optional one-sided formula giving the variance model, evaluated in data (for example ~ x1 + x2). When NULL, 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 deprecated performNCVTest(model, var_formula) returned, labels included. In 0.14.0 var_formula was the second argument: a formula passed there by position is still read as var_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)