lmer的分组错误 [英] Grouping error with lmer

查看:113
本文介绍了lmer的分组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数据框:

I have a data frame with the following structure:

> t <- read.csv("combinedData.csv")[,1:7]
> str(t)
'data.frame':   699 obs. of  7 variables:
 $ Awns               : int  0 0 0 0 0 0 0 0 1 0 ...
 $ Funnel             : Factor w/ 213 levels "MEL001","MEL002",..: 1 1 2 2 2 3 4 4 4 4 ...
 $ Plant              : int  1 2 1 3 8 1 1 2 3 5 ...
 $ Line               : Factor w/ 8 levels "a","b","c","cA",..: 2 2 1 1 1 3 1 1 1 1 ...
 $ X                  : int  1 2 3 4 7 8 9 10 11 12 ...
 $ ID                 : Factor w/ 699 levels "MEL_001-1b","MEL_001-2b",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ BobWhite_c10082_241: int  2 2 2 2 2 2 0 2 2 0 ...

我想构建一个混合效果模型.我在数据框中知道要包含的随机效应(漏斗)是一个因素,但它不起作用:

I want to construct a mixed effect model. I know in my data frame that the random effect I want to include (Funnel) is a factor, but it does not work:

> lmer(t$Awns ~ (1|t$Funnel) + t$BobWhite_c10082_241)
Error: couldn't evaluate grouping factor t$Funnel within model frame: try adding grouping factor to data frame explicitly if possible

事实上,无论我想包含哪种随机效果,这种情况都会发生,例如植物:

In fact this happens whatever I want to include as a random effect e.g. Plant:

> lmer(t$Awns ~ (1|t$Plant) + t$BobWhite_c10082_241)
Error: couldn't evaluate grouping factor t$Plant within model frame: try adding grouping factor to data frame explicitly if possible

为什么R给我这个错误?我唯一能回答的是,馈入的随机效应不是DF中的因素.但是,如str所示,df $ Funnel当然是.

Why is R giving me this error? The only other answer I could google fu is that the random effect fed in wasn't a factor in the DF. But as str shows, df$Funnel certainly is.

推荐答案

为建模函数提供方便的语法并同时具有可靠的实现实际上并不那么容易.大多数程序包作者都假定您使用 data 参数,即使这样,也会出现作用域问题.因此,如果使用 DF $ col 语法指定变量,则可能会发生奇怪的事情,因为程序包作者很少花费大量精力来使此工作正确地进行,并且为此不包含很多单元测试.

It is actually not so easy to provide a convenient syntax for modeling functions and at the same time have a robust implementation. Most package authors assume that you use the data parameter and even then scoping issues can occur. Thus, strange things can happen if you specify variables with DF$col syntax since package authors rarely spend a lot of effort to make this work correctly and don't include a lot of unit tests for this.

因此,如果模型函数提供了 formula 方法,则强烈建议使用 data 参数.如果您不遵循该惯例(以及其他模型功能,如 lm ),可能会发生奇怪的事情.

It is therefore strongly recommended to use the data parameter if the model function offers a formula method. Strange things can happen if you don't follow that praxis (also with other model functions like lm).

在您的示例中:

lmer(Awns ~ (1|Funnel) + BobWhite_c10082_241, data = t) 

这不仅有效,而且编写起来也更加方便.

This not only works, but is also more convenient to write.

这篇关于lmer的分组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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