Grouping error with lmer

旧巷老猫 提交于 2021-01-27 10:35:10

问题


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:


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.

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

In your example:

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

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



来源:https://stackoverflow.com/questions/36650695/grouping-error-with-lmer

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