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

lme4-nlmemixed modelr

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

Sample

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

Best Answer

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.