# “ random effects must be less than the number of observations” error in lmer package

I'm trying to implement a regression model with both fixed and random effects. The package I use is the lme4.

I want to find the relationship between the continuous variables X1 and X2, using the categorical variable F1 as a fixed effect.

When I try to implement the model model <- lmer(X1 ~ F1 + (1|X2)) I get the following error:

Number of levels of a grouping factor for the random effects must be less than the number of observations

My understanding for the error is that the X2 varies less than the X1 (which has some similar values). Why is this a problem?

• As hinted by others in the answers, your model specification doesn't make sense. I recommend that you study the following section of the R users' GLMM FAQ: glmm.wikidot.com/faq#toc27 – Jake Westfall May 2 '13 at 18:26

The lmer function requires multiple measures / random effect (at least for some of them). Furthermore, the grouping factor in the random effect is typically nominal values, not continuous. You say X2 is continuous yet you tell the model that the intercept is grouped by it as a random effect.

Either you just want straight linear modelling here or something else is your random effect. Is it repeated measures on subjects? If that's the case then you should have something like...

model <- lmer(X1 ~ F1 + X2 + (1|subject))


Then again, perhaps you're just confusing continuous effects with random or some other term. From your description in the comments I cannot see where you actually have what one might call a random effect. In that case you probably want something even simpler...

model <- lm(X1 ~ F1 + X2)


This predicts X1 from F1 and X2.

• No it is not repeated measures on subjects. X1 is values for subjects that recorded once and X2 other kind of values specific for each individual(measured once also). What I want is the parameter/coefficient that describes their relationship, taking into account the fixed effects. Thank you for your reply – mpg May 2 '13 at 14:02
• +1 @ John's reply. You need to have a factor variable for X2. You are practically saying to the model to have a different intercept for every X2 value but if you have $N$ different values in X2( $N$ being the # of data points in your sample) your problem is overdetermined. If X2 has some meaning (eg. BMI) and you can derive meaningful categorical values based on it, you are fine. (Don't chop stuff arbitrarily that nonsensical.) – usεr11852 May 2 '13 at 16:37