# Error in R lme4 package: number of levels of each grouping factor must be < number of observations

I’m trying to perform a linear mixed effects analyses using the lme4 package on R. I need your help because of a recurrent error… I have 17 profiles (that participants had to rate) as repeated measures (they’ve seen every profil1,2,3 but only one version of them everytime, either 1a or 1b or 1c as in a classic latin squares design). I’m doing a score difference between profil-a type and profil-b ratings as fixed continuous effects as long as fixed categorical effects “ovul”.

DF$X<-rowMeans(cbind(DF$profil_1a,DF$profil_2a,DF$profil_3a,DF$profil_4a,DF$profil_5a,DF$profil_6a,DF$profil_10a,DF$profil_11a,DF$profil_12a,DF$profil_13a,DF$profil_14a,DF$profil_15a,DF$profil_16a, DF$profil_1b,DF$profil_2b,DF$profil_3b,DF$profil_4b,DF$profil_5b,DF$profil_6b,DF$profil_10b,DF$profil_11b,DF$profil_12b,DF$profil_13b,DF$profil_14b,DF$profil_15b,DF$profil_16b, DF$profil_1c,DF$profil_2c,DF$profil_3c,DF$profil_4c,DF$profil_5c,DF$profil_6c,DF$profil_10c,DF$profil_11c,DF$profil_12c,DF$profil_13c,DF$profil_14c,DF$profil_15c,DF$profil_16c),na.rm=TRUE)
DF$likertfort<-rowMeans(cbind(DF$profil_1a,DF$profil_2a,DF$profil_3a,DF$profil_4a,DF$profil_5a,DF$profil_6a,DF$profil_10a,DF$profil_11a,DF$profil_12a,DF$profil_13a,DF$profil_14a,DF$profil_15a,DF$profil_16a),na.rm=TRUE)
DF$likertmoyen<-rowMeans(cbind(DF$profil_1b,DF$profil_2b,DF$profil_3b,DF$profil_4b,DF$profil_5b,DF$profil_6b,DF$profil_10b,DF$profil_11b,DF$profil_12b,DF$profil_13b,DF$profil_14b,DF$profil_15b,DF$profil_16b),na.rm=TRUE)
DF$Wdif1<-(DF$likertfort-DF$likertmoyen) mean(DF$Wdif1, na.rm=TRUE)
DF$ovul<- -0.5 *(DF$ovul=="Oui") + 0.5 *(DF$ovul=="Non")  When I insert the scenario (DF$X) and the subject as random variables as in:

DF.model = lmer(DF$Wdif1 ~ DF$ovul +(1|subject) + (1|X), data=DF)


I got the error: Error: number of levels of each grouping factor must be < number of observations. I understand the model is overparametrized but is this fixable? Or my number of participants (n=38) is just too small to perform such a model? Btw, when I perform without the random parameter "subject" it works apparently. Thanks in advance for all your help, j

• Can you please describe your experiment and your dataset a bit more clearly? To my understanding, every participant rated 17 scenarios. But what is profil? And what is ovul and X in your formula? And what is drug, that you don't seem to include in your analysis? – JonB Sep 24 '15 at 11:55
• Sorry Jon, indeed there is some bugs. I'm trying to edit my post but I can't do you how to do that? Yes pp (women) had rated 17 profiles and taking into account if there were ovulating or not. X is the rowmeans of all the profiles rating – cladou Sep 24 '15 at 12:11
• ok I edited it. Is that clearer? – cladou Sep 24 '15 at 12:26
• I'm sorry, but I can't see what you have edited? – JonB Sep 24 '15 at 12:40
• should be good now :) – cladou Sep 24 '15 at 12:48

You have 38 women who have done 17 profiles each. Each profile can be of three different types (a, b, or c), randomly assigned to each woman. The women can either be ovulating or not (binary variable) and X is the mean of all 17 profile ratings for each subject.

You now want to assess the impact of ovulation on score for profiles a and b. You can do this with a mixed model approach and it requires that you restructure your data so that each profile rating is one row. If there are 17*3 profiles it means that each woman will have 51 rows, but 34 of them will have missing data for the score variable because each woman only rated 17 profiles. The other variables, in this case only ovulation, will be the same for all of the 51 rows. This is called converting data from wide form to long form, and you can do it in this way:

library(reshape)
DF.long <- melt(DF, id.vars=c("subject", "ovul", value.name="rating", variable.name="profile")

# id.vars are the variables that you want to keep per subject, the others will be converted.
# rating is the rating score for each trial
# profile is the name of the profile for each trial


Now, you will need to convert all names in the rating variable, "profile_1a", "profile_2a" etc to "a", "profile_1b", "profile_2b" to b and so on. When you're done, you're ready to analyze. The first method is to analyze a and b separately. You will need to use subject as a random effect to take the repeated measurements into account:

mod_a <- lmer(rating ~ ovul + (1|subject), data=subset(DF_long, profile=="a")
mod_b <- lmer(rating ~ ovul + (1|subject), data=subset(DF_long, profile=="b")


You now have the effect of ovul on a and b for the two models. If you want to see if ovul has a different effect on a than on b, you can specify an interaction model:

mod_a <- lmer(rating ~ profile * ovul + (1|subject), data=subset(DF_long, profile=="a" | profile=="b")


You will now get one value for profile, that tells you the difference between the profiles, and another value for ovul, that tells you the average effect of ovul. The interaction term profile:ovul will tell you the difference between the profles in the effect of ovul on score.

• Greats thanks so much! My data are already shape in the way that each woman get 51 rows but with missing values due to the randomization of the profile types. I'm gonna try your code now! – cladou Sep 25 '15 at 10:31
• So I tried to formalize things according to the way my data are. I do not really get yet the way you implement the variable "rating". Cause in my case, what I did is subset profil1a, profila2a, etc. to "a" like you said and same for profile_b type. Then, to test the impact of ovul on profile-a type, I tried mod_a <- lmer(DF$a ~ DF$ovul + (1|DF\$subject)) but I get another error "Erreur : (p <- ncol(X)) == ncol(Y) is not TRUE". Is there anything wrong in my coding? Or do I need to use the subset absolutely? – cladou Sep 25 '15 at 11:17
• Can you show a sample of your data in code in your original post? – JonB Sep 25 '15 at 11:48
• Sorry for the delay Jon, I've added the sample! – cladou Sep 27 '15 at 12:06
• It doesn't look like you have 51 rows per subject, but only one row and 51 profile variables. Anyway, you shouldn't try to predict the profile variable but rather the value, the rating, of the profile. If you use the code I provided, you will get one variable called rating that should be your dependent variable, and a variable called profile that (after renaming them to "a", "b", and "c") should be a variable used for subsetting the data. Can you please show your data in the long form with 51 rows per subject? – JonB Sep 27 '15 at 13:07