lmer错误:分组因子必须是<观察次数

问题描述 投票:4回答:2

我试图在某些数据上运行混合效果模型,但是在使用其中一个固定效果时,我认为这主要是因为它是一个因素?!

样本数据:

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

但是对于我的生活,我不理解对这个问题的回答!

r lme4 mixed-models nlme
2个回答
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

0
投票

你可以使用minque包,一个R Package for Linear Mixed Model来解决你的问题:

  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无法适应某些型号。

© www.soinside.com 2019 - 2024. All rights reserved.