即使在数据框中对变量进行分组之后,lme4升级也会产生错误消息 [英] lme4 upgrade produces error message even after grouping variables within the data frame

查看:142
本文介绍了即使在数据框中对变量进行分组之后,lme4升级也会产生错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用旧版本的lme4运行线性混合模型.现在,我已经更新了lme4,我得到了以下错误:

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:

错误en [[<-.data.frame(*tmp*,i,值=整数(0)): 替换有0行,数据有4211

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

我在此网站上找到了一个答案,建议将所有分组变量放在data参数指定的数据框中.我已经做到了,但是我的代码仍然无法正常工作.

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.

这里是:

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

错误en [[<-.data.frame(*tmp*,i,值=整数(0)): 替换有0行,数据有4211

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

有人知道为什么会这样吗? 非常感谢! 娜塔莉亚·诺登(Natalia Norden)

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

推荐答案

这确实是lme4中尚未修复的错误:

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)

总的来说,我认为这甚至是没有必要的-至少glmer的最新版本会自动将分组变量(例如species)自动转换为因数-但我要感谢自己要小心/明确.如果出于某种原因我不想将分组变量永久转换为一个因数,我通常会将该变量作为一个因数版本,例如

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

,然后在公式中使用fspecies而不是species.

and then use fspecies rather than species in the formula.

就其价值而言,这在以前发布的lme4版本中也将是一个问题:对于lme4.0(向后兼容版本),

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)

工作正常,但

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

给出Error in factor(herd) : object 'herd' not found(诚然,隐秘性较小的错误消息,但仍然是错误).

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

这篇关于即使在数据框中对变量进行分组之后,lme4升级也会产生错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆