# In R, “Error: number of levels of each grouping factor must be < number of observations”

I ran an experiment where subjects exercised in different climatic conditions. The response variable is the subjects' internal (core) temperatures.

There were 30 subjects and some participated in more than one trial. Each trial had a different ambient temperature, relative humidity, etc. Because of the non-independence of some of the subjects' data due to participating more than once, I decided to use a linear mixed effects model with Subject as the random effect. I also think I need to nest Subject within Trial due to the fact that different climatic conditions effect the subjects' temperatures.

The response variable is the subjects' temperatures and the fixed effects are the subjects' mass, age, and body fat percentage.

Here is my model, in package lme4 in R:

>mod=lmer(Temp ~ Mass + Age + Fat + (1|Subject/Trial), data=data, REML=FALSE)


I get the error:

Error: number of levels of each grouping factor must be < number of observations

I have 43 observations and 6 different trials. Does this mean I just didn't collect enough data to be able to fit this model? Or is there another explanation/anything I can do?

• Please check the GLMM FAQ page. As I interpret the section on Model Specification, it looks like you have Trials nested within Subjects, not Subjects within Trials. Don't know whether that will solve your problem. – EdM Jan 25 '20 at 17:58
• Thank you, you are absolutely correct! Unfortunately it doesn't fix the problem. – J. E. Jan 27 '20 at 10:21
• That's what I suspected. With some Subjects restricted to only 1 trial there is no way to parcel out the within-Trial Subject random effects from the between-Trial random effects as your nested model would require. If the updated answer from @ErikRuzek suggesting crossed random effects provides the solution, please accept that answer. Note that with only 6 Trials you might treat Trial as a fixed effect instead, as that's on the borderline of where many make the choice between fixed and random modeling. – EdM Jan 27 '20 at 14:51

mod <- lmer(Temp ~ Mass + Age + Fat + (1|Subject) + (1|Trial), data=data)

If you have coded your data so that trial 1 is the same condition across subjects (i.e., it represents the same combination of ambient temp and relative humidity) and similarly trial 2 represents the same combination of conditions, etc., then this model syntax will be right whether your data are indeed purely nested or cross-classified. Make sure and check the summary(mod) output where it tells you the Ns in each grouping. If it is off, then check your coding of trial to make sure that trials with the same conditions are consistent across individuals. With 30 subjects and the relative small number of data points, you should use REML, which has better properties than full maximum likelihood with small samples.