I am trying to fit a random slope model in R and my code is as follows:
lmer(data=ds, Outcome ~ treatment + (0 + treatment|ID))
I get the following error message when I try running this code:
Error: number of observations (=2035) <= number of random effects (=2035) for term (0 + treatment | ID); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable
I have 407 unique IDs and 5 treatments, so the number of observations is 407*5 = 2035. However, I don't understand why I have 2035 random effects. Being a random slope only model, I would expect that I am estimating one random slope per unique ID and so I thought there would only be 407 random effects in my model.
I also tried fitting a random slope and intercept model as follows:
lmer(data=ds, Outcome ~ treatment + (1 + treatment|ID))
However, I still got the same error message as above, that I have 2035 observations and the number of random effects = 2035. I get that the number of observations should be more than the number of random effects being estimated, but I still don't understand why there are 2035 random effects in this model either. I would expect that since I am estimating a random slope and intercept for each unique ID (2 random effects for each ID), I would have 407*2=814 random effects. Clearly, there is something I am missing here about how to calculate the number of random effects for these two models. Any help understanding this would be highly appreciated. Thanks.
treatment
is a categorical variable. This means that, due to dummy encoding, you will have several (in your case five) coefficients per ID. Your random effect can't includetreatment
because you lack the repeated measures. There are no degrees of freedom left for the residual variance. The correct random effect in your design should be(1 | ID)
. $\endgroup$