[R] Problems with bstats::white.test()

Iuri Gavronski iuri at ufrgs.br
Tue Sep 9 01:33:01 CEST 2014


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?

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
>



More information about the R-help mailing list