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

試著忘記壹切 提交于 2019-12-02 17:43:47

问题


I've been running linear mixed models using an old version of lme4. Now that I have updated lme4 I'm getting the following error:

Error en [[<-.data.frame(*tmp*, i, value = integer(0)) : replacement has 0 rows, data has 4211

I found in this website an answer that suggests to put all the grouping variables within the data frame specified by the data argument. I've done that but my code still doesn't work.

Here it is:

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

Error en [[<-.data.frame(*tmp*, i, value = integer(0)) : replacement has 0 rows, data has 4211

Any idea why is this happening? Many thanks! Natalia Norden


回答1:


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).



来源:https://stackoverflow.com/questions/19749313/lme4-upgrade-produces-error-message-even-after-grouping-variables-within-the-dat

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