Grouping error with lmer

时间:2016-04-15 15:04:13

标签: r dataframe lme4

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

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.

1 个答案:

答案 0 :(得分:3)

为建模功能提供方便的语法实际上并不容易,同时具有强大的实现。大多数软件包作者都假设您使用data参数,甚至可能会出现范围问题。因此,如果您使用DF$col语法指定变量,则可能会发生奇怪的事情,因为包作者很少花费大量精力使其正常工作,并且不包含大量的单元测试。

因此,如果模型函数提供data方法,强烈建议使用formula参数。如果您不遵循该实践(也可以使用其他模型函数,如lm),则会发生奇怪的事情。

在你的例子中:

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

这不仅有效,而且写作也更方便。