混合模型中的因子/水平误差 [英] Factor/level error in mixed model
问题描述
我正在对类似于此数据的东西运行混合模型:
I am running a mixed model on something akin to this data:
df<-data.frame(stage=c("a","a","a","a","b","b","b","b","c","c","c","c"),
nematode=c("fn","fn","bn","bn","fn","fn","bn","bn","fn","fn","bn","bn"),
id2=c(1,2,3,4,1,2,3,4,1,2,3,4),
value=c(1,0,0,2,3,1,1,2,0,0,0,2))
我要拟合的模型是:
stage.id <- function(x) round(summary(glmer(value ~ stage + (1 | id2),family="poisson", data = x))$coefficients[2, c(1, 2, 4)], 3)
models.id0 <- ddply(tree2, .(stage, nematode), stage.id)
但是,当我运行此命令时,我不断收到错误消息:
However, when I run this, I continually get an error:
contrasts<-
中的错误(*tmp*
,值= contr.funs [1 + isOF [nn]]): 对比只能应用于具有2个或更多级别的因子
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
这对我来说没有意义,因为我在每个因子(df $ stage和df $ nematode)上都使用了nlevels()命令,它们分别为3和2.有什么不对劲的地方吗?
which doesn't make sense to me given that I've used the nlevels() command on each of the factors (df$stage and df$nematode) and they are 3 and 2, respectively. Any sense what might be awry?
推荐答案
您已经将stage
作为模型中的固定效果,但是您正在尝试为stage
和nematode
的每种组合拟合模型(ddply(tree2, .(stage, nematode), ...)
).因此,在每个数据块中,只有一个stage
值,这会导致错误.
You have stage
as a fixed effect in your model, but you're trying to fit the model for every combination of stage
and nematode
(ddply(tree2, .(stage, nematode), ...)
). Thus in each chunk of the data there's only a single stage
value, which causes the error.
您可以:
- 仅适用于
nematode
值,即ddply(tree2,.(nematode), ...)
- 将
stage
留在模型之外,即拟合模型value ~ 1 + (1 | id2)
- apply only over
nematode
values, i.e.ddply(tree2,.(nematode), ...)
- leave
stage
out of your model, i.e. fit the modelvalue ~ 1 + (1 | id2)
根据您的评论(我的目标是比较每种线虫的阶段.即,对于每种线虫(例如,bn
),阶段是否不同"),您希望使用前一种解决方案(仅适用于nematode
值).
According to your comment ("my goal is to compare the stages for each of the types of nematodes. I.e., for each type of nematode (e.g., bn
) do the stages differ"), you want the former solution (apply only over nematode
values).
这篇关于混合模型中的因子/水平误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!