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

Viewed 43

I am trying to run a linear mixed model for a sentiment analysis with movie reviews. There are 32000 different titles in my data frame and each title has a varying number of reviews (review_number). review_number is a sequence starting from 1 for each title and is 937 at max. I calculated the emotionality and extremity for each review. The dummy critic indicates whether a review was written by a critic. I want to nest the reviews within titles. In total there are 1.2 Mio observations.

Sample:

      title critic review_number  review_emo review_extr
1 '64 - '95      0             1 -0.75908061   0.1051023
2 '64 - '95      0             2 -1.78770128  -1.2697559
3 '64 - '95      0             3  0.04826126   1.1797093
4 '64 - '95      0             4  1.56405819   0.5269304
5 '64 - '95      0             5 -0.82757559   0.1753819
6 '64 - '95      0             6  0.68578835  -0.5778072

This is the model I used:

mod <- lmer(review_emo ~ critic + review_extr + (1 | title/review_number),
              data = df)

I receive this error message:

Error: number of levels of each grouping factor must be < number of observations (problems: review_number:title)

Why is that and how can I fix it? My grouping factor should be smaller than the number of observations because there are 32000 titles and a maximum of 937 reviews per title. I checked the number of levels with nlevels(x).

I looked this problem up in various posts but I can`t tell why it does not work for me.

1 Answers

The problem is that you have only one observation per combination of title and review number; this means that the nested "review number within title" variance term is completely confounded with the residual variance.

To expand on that a little bit: (1|title/review_number) expands to (1|title) + (1|title:review_number), where the grouping variable for the second term is the interaction between title and review number.

It's hard to see how this approach would ever have worked (it would only work if there were more than observation per title/review number combination, which seems unlikely given the way the data are described).

  • You could simply drop the review_number term: you won't be missing anything, as this level of variation is already captured in the residual variation
  • you can override this check if you want (see ?lme4::lmerControl, specifically the check.nobs.vs.nlev setting). If you do, then the residual variance will be arbitrarily divided between residual variance and review-within-title variance (as any linear combination that adds up to the total variance at the observation level will have the same log-likelihood).
  • if you use the glmmTMB package you could force the residual variance to zero (or very small, to be precise: dispformula ~ 0), so that all of the observation-level variance would be assigned to the review-within-trial term. (This still won't change the log-likelihood.)
Related