lmer error: grouping factor must be < number of observations

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

Leave a Comment