# “Random effects must be less than the number of observations” error in lmer package with categorical predictors

I know similar error messages were discussed before, but my question is different than those.

I have a model in the following form: $$Y_{ij} = \alpha_jX_i+ \beta_j + \epsilon_{ij}$$

Here $\beta_j$ is a categorical variable (Group), $\alpha_j$ is a random slope for each group, $X_i$ is a categorical variable to identify each subject, and $Y_{ij}$ is subject $i$'s contribution to group $j$.

When I write this model in lmer, I use the following model function:

contrib ~ Subj + Grp + (Subj+0|Grp)


I don't have observations for all Subj-Grp combinations. Some subjects only participate in some groups. When running this model, if my Subj is a continuous variable, then I don't get any error message. But if my Subj is a categorical variable to identify each subject, I get the following error message:

Error: number of observations  <= number of random effects for term
(0 + Subj | Grp); the random-effects parameters and the residual
variance (or scale parameter) are probably unidentifiable


Is there a way to use a lmer model for such cases with imbalanced data? Any conceptual explanation about the differences of a model with a random slope for a categorical variable vs. continuous variable would be really helpful.

Edit: as requested, this is the scenario I am trying to model. Subjects in the study are members of multiple groups. But not every subject is part of every group. So I think this is not a fully nested design (?). I measure the quality of the contribution of each subject to each group they contribute to ($Y_{ij}$ is such a measure). This measurement could be due to the properties of the subject and the group. That is why I am including categorical variables for the subject and the group. However the subject could have different quality of contribution in different groups. Hence I include a random slope $\alpha_j$. When I got the error message for subject as a categorical variable, I tried to replace it with a continuous value for the subject (average quality measurement across all groups the subject participate in). What I am interested in finding from the model is $\alpha_j$ (i.e., how much a subject vary in different groups).

I think you are mispecifying the model. The reason you can run the model when you treat individuals continuously is that lmer is estimating the slopes of the variable subj. If you think about the lmer formula, you are estimating a random effect (i.e. slope) across subjects, which is probably not meaningful. There is a great primer to lmer formulas: here. You probably can't estimate a random effect across a factor in this case (I'm actually not sure how that works out more generally - I'm not 100% sure it's statistically erroneous, so I'll leave that to someone that knows for sure). You should probably be estimating random intercepts for individuals - which can be nested in group, if you feel like that's what you're trying to do: lmer(contrib~Group+(1|Subj/Group), data=data) Or something like that.
• Thanks @Mike. I added some explanation about the data. My data is not a fully nested structure. When I tried your suggestion with lmer(contrib~Group+(1|Subj/Group), data=data), it gives another error message Error: number of levels of each grouping factor must be < number of observations. Any clarification would be useful! – dan24 Nov 14 '17 at 5:58
• No it is not longitudinal. I want to rank Subjects based on $\alpha_j$. Does it clarify your question? @Mik – dan24 Nov 14 '17 at 14:38