lmer错误:分组系数必须<观察数 [英] lmer error: grouping factor must be < number of observations
问题描述
我正在尝试对某些数据运行混合效果模型,但是正在努力解决其中一种固定效果,我认为主要是因为这是一个因素吗?!
I am attempting to run a mixed effect model on some data but struggling with one of the fixed effects, I think primarily due to it a factor?!
样本数据:
data4<-structure(list(code = structure(1:10, .Label = c("10888", "10889",
"10890", "10891", "10892", "10893", "10894", "10896", "10897",
"10898", "10899", "10900", "10901", "10902", "10903", "10904",
"10905", "10906", "10907", "10908", "10909", "10910", "10914",
"10916", "10917", "10919", "10920", "10922", "10923", "10924",
"10925", "10927"), class = "factor"), speed = c(0.0296315046039244,
0.0366986630049636, 0.0294297725505692, 0.048316183511095, 0.0294275666501456,
0.199924957584131, 0.0798850288176711, 0.0445886457047146, 0.0285993712316451,
0.0715158276875623), meanflow = c(0.657410742496051, 0.608271363339857,
0.663241108786611, 0.538259450171821, 0.666299529534762, 0.507156583629893,
0.762448863636364, 37.6559178370787, 50.8557196935557, 31.6601587837838
), length = c(136, 157, 132, 140, 135, 134, 144, 149, 139, 165
), river = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L
), .Label = c("c", "f"), class = "factor")), .Names = c("code",
"speed", "meanflow", "length", "river"), row.names = c(2L, 4L,
6L, 8L, 10L, 12L, 14L, 16L, 18L, 20L), class = "data.frame")
我的模型是这样的:
model1<-lmer(speed ~ river + length +(1|meanflow)+(1|code), data4)
并且运行时返回错误消息:
and when run returns error message:
Error in checkNlevels(reTrms$flist, n = n, control) :
number of levels of each grouping factor must be < number of observations
拖曳了互联网,我找到了一个答复
但对我而言,我一生都不理解对问题的回答!
but for the life of me do not understand the responses to the question!
推荐答案
您在这里遇到两个问题:
You have two problems here:
-
对于每个
code
值,您似乎都有一个观察结果.这意味着您既不能估计残差方差(它是lmer
内置的,更通常是线性混合模型),也不能估计-c0之间的方差-这两个参数都将试图估计相同的值.方差成分,以及var(residual)
和var(code)
的任何组合加起来等于相同的值,则表示对数据的拟合程度相同.
It looks like you have one observation for every value of
code
. That means that you can't estimate both a residual variance (which is built in tolmer
, and linear mixed models more generally) and an among-code
variance -- both of these parameters will be trying to estimate the same variance component, and any combination ofvar(residual)
andvar(code)
that adds up to the same value will represent an equally good fit to the data.
对于每个meanflow
值,您也有一个观察值;这是因为meanflow
是连续变量,在模型中通常不希望将其用作分组变量.我不确定您打算用这个术语来捕捉什么.
You also have one observation for every value of meanflow
; this is because meanflow
is a continuous variable, which is not usually something you want to use as a grouping variable in the model. I'm not sure what you're trying to capture with this term.
如果您坚持使用lmerControl
绕过检查,则可以实际拟合这些模型,但是不一定会得到明智的结果!
You can actually fit these models if you insist by using lmerControl
to bypass the checks, but you won't necessarily get a sensible result!
model2 <- lmer(speed ~ river + length +(1|meanflow)+(1|code), data4,
control=lmerControl(check.nobs.vs.nlev = "ignore",
check.nobs.vs.rankZ = "ignore",
check.nobs.vs.nRE="ignore"))
在这里,方差约等于三分之二:
Here the variance has been divided approximately in equal thirds:
VarCorr(model2)
## Groups Name Std.Dev.
## meanflow (Intercept) 0.035354
## code (Intercept) 0.032898
## Residual 0.033590
如果我们仅使用一种(仍然不合适的)随机效果,
If we use only one (still inappropriate) random effect,
model0 <- lmer(speed ~ river + length +(1|meanflow), data4,
control=lmerControl(check.nobs.vs.nlev = "ignore",
check.nobs.vs.rankZ = "ignore",
check.nobs.vs.nRE="ignore"))
现在,方差精确地分为两半:
Now the variance is divided exactly in halves:
VarCorr(model0)
## Groups Name Std.Dev.
## meanflow (Intercept) 0.041596
## Residual 0.041596
这篇关于lmer错误:分组系数必须<观察数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!