我试图在某些数据上运行混合效果模型,但是我认为主要是因为它是一个因素?!
示例数据:
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)
并在运行时返回错误消息:
Error in checkNlevels(reTrms$flist, n = n, control) :
number of levels of each grouping factor must be < number of observations
我已经浏览过互联网found one response
但是对于我的生活,我不理解对这个问题的回答!
答案 0 :(得分:9)
这里有两个问题:
看起来您对code
的每个值都有一个观察。这意味着您无法估计残差方差(内置于lmer
和更一般的线性混合模型)和 - code
方差 - 这两个参数都将尝试估算相同的方差组件,var(residual)
和var(code)
的任意组合加起来相同的值将代表同样适合数据。
您对meanflow
的每个值都有一个观察值;这是因为meanflow
是一个连续变量,通常不想在模型中用作分组变量。我不确定你用这个词来捕捉到什么。
如果你坚持使用lmerControl
来绕过支票,你实际上可以适应这些模型,但你不一定会得到明智的结果!
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"))
这里的方差大约相等于三分之一:
VarCorr(model2)
## Groups Name Std.Dev.
## meanflow (Intercept) 0.035354
## code (Intercept) 0.032898
## Residual 0.033590
如果我们只使用一个(仍然不合适)随机效果,
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"))
现在方差分成两半:
VarCorr(model0)
## Groups Name Std.Dev.
## meanflow (Intercept) 0.041596
## Residual 0.041596
答案 1 :(得分:0)
你可以使用 minque 包,一个线性混合模型的R包来解决你的问题:
library(minque)
OUT<-lmm(speed ~ river + length+1|meanflow+code,method=c("reml"),data=data4)
OUT[[1]]$Var
OUT[[1]]$FixedEffect
OUT[[1]]$RandomEffect
有时 lme4 无法适合某些型号。