lme4 upgrade produces error message even after grouping variables within the data frame

瘦欲@ 提交于 2019-12-02 07:34:02
Ben Bolker

This is indeed an as-yet-unfixed bug in lme4: https://github.com/lme4/lme4/issues/156 . However, the workaround is easy: just do conversions such as as.factor() and as.numeric() within the data frame, rather than within the formula, e.g.

d.sdg.ngb = transform(d.sdg.ngb,species=factor(species))
msdgtot = glmer(sdg.dens ~ ngbr.trees + (1 + ngbr.trees | species),
    data=d.sdg.ngb,family=poisson)

in general, I think this should not even be necessary -- at least recent versions of glmer automatically convert grouping variables such as species to factors -- but I can appreciate wanting to be careful/explicit. If for some reason I don't want to permanently convert the grouping variable to a factor, I usually make a factor version of the variable, e.g.

d.sdg.ngb = transform(d.sdg.ngb,fspecies=factor(species))

and then use fspecies rather than species in the formula.

For what it's worth, this would have been a problem in previously released versions of lme4 as well: with lme4.0 (the backward-compatible version),

gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
  data=cbpp,family=binomial)

works fine but

gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | factor(herd)),
data=cbpp,family=binomial)

gives Error in factor(herd) : object 'herd' not found (admittedly a less cryptic error message, but still an error).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!