lmer error: grouping factor must be < number of observations

扶醉桌前 提交于 2019-12-05 18:20:23

You have two problems here:

  • 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 to lmer, 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 of var(residual) and var(code) that adds up to the same value will represent an equally good fit to the data.

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

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

You can use the minque package, an R Package for Linear Mixed Model, to solve your problem:

  library(minque)
  OUT<-lmm(speed ~ river + length+1|meanflow+code,method=c("reml"),data=data4)
  OUT[[1]]$Var
  OUT[[1]]$FixedEffect
  OUT[[1]]$RandomEffect

Sometimes the lme4 is not able to fit some models.

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