[R] Problems with bstats::white.test()
Achim Zeileis
Achim.Zeileis at uibk.ac.at
Tue Sep 9 09:42:02 CEST 2014
On Mon, 8 Sep 2014, Iuri Gavronski wrote:
> Hi,
>
> I am trying to test for heteroskedascity in an OLS model, but I am not
> able to run the white.test() if the model has dummy variables built
> from factors. Any suggestions?
The White test is equivalent to the Breusch-Pagan test with an auxiliary
model containing all regressors, their squares and their cross-products.
For dummy variables the regressors and their squares are identical, so
this strategy has to be modified. Other collinearities might occur as
well.
For this reason the "lmtest" package has only a function bptest() but no
additional whitetest() function. Examples show how to compute the White
test, though. See e.g. example("CigarettesB", package = "AER").
The "bstats" package has copied code for bptest (and dwtest) from "lmtest"
without crediting the original authors. The maintainer is cc'ed here and
encouraged to read and follow the CRAN policies.
The white.test() function in "bstats" does not leverage the bptest()
function (and hence does not offer the recommended studentization option).
It sets up the auxiliary model by pasting "I(...^2)" and ":" into the
model formula which is very error prone and does not work for factor
variables. (This is the reason why "lmtest" doesn't do it.)
> Please find a reproducible code below:
>
> myswiss <- swiss
> myswiss$fert <- ifelse(
> myswiss$Fertility>70,
> "High","Low")
> myswiss$fert <- factor(myswiss$fert)
> str(myswiss)
> mod1 <- lm(Infant.Mortality ~ fert,
> data=myswiss)
> library(bstats)
> bptest(mod1)
> white.test(mod1)
>
> myswiss$fertlow <- ifelse(
> myswiss$Fertility>70,
> 0,1)
> mod2 <- lm(Infant.Mortality ~ fertlow,
> data=myswiss)
> library(bstats)
> bptest(mod2)
> white.test(mod2)
>
> Results:
>
>
>> bptest(mod2)
>
> studentized Breusch-Pagan test for homoscedasticity
>
> data: mod2
> BP = 2e-04, df = 1, p-value = 0.989
>
>> white.test(mod2)
>
> White test for constant variance
>
> data:
> White = 2e-04, df = 1, p-value = 0.989
>
>> bptest(mod1)
>
> studentized Breusch-Pagan test for homoscedasticity
>
> data: mod1
> BP = 2e-04, df = 1, p-value = 0.989
>
>> white.test(mod1)
> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
> contrasts can be applied only to factors with 2 or more levels
> In addition: Warning message:
> In Ops.factor(fert, fert) : * not meaningful for factors
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list