Error: number of observations <= number of random effects for LMER

I have the following code:

model <- lmer(Scores ~ Group*Time + (Time | ID), positiveaffect)

I am trying to run a linear mixed effects model with Group as a between-subjects variable and Time as a within-subjects variable , taking into account the interaction between Group and Time.

Each participant has two measurements (one from each time point: time point 1 and 2).

However, I get this error when I run the code:

Error: number of observations (=89) <= number of random effects (=90) for term (Time | ID); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable

I am not sure what I need to do to overcome this?

I would be so grateful for your help!

Minimal reproducible example:

structure(list(Group = c("SH", "SH", "SH", "SH", "SH", "SH", 
"SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", 
"SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", 
"SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", 
"SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", 
"SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "SH", "HC", 
"HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", 
"HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", "HC", 
"HC", "HC", "HC", "HC", "HC", "HC", "HC"), Time = c(1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2), Scores = c(16, 18, 24, 31, 11, 14, 23, 17, 19, 10, 
16, 22, 19, 19, 23, 20, 14, 17, 29, 20, 16, 30, 26, 18, 21, 20, 
19, 15, 39, 11, 15, 28, 35, 16, 10, 15, 17, 21, 23, 12, 17, 22, 
13, 17, 13, 17, 18, 19, 17, 16, 24, 20, 22, 19, 18, 24, 13, 24, 
28, 24, 26, 13, 28, 10, 24, 32, 34, 36, 16, 25, 28, 30, 27, 24, 
19, 19, 20, 20, 10, 22, 15, 12, 20, 26, 17, 21, 24, 27, 31, 16
), ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 31L, 32L, 33L, 
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L)), row.names = c(NA, 
-90L), class = c("tbl_df", "tbl", "data.frame"))

Hi,

I don't know this particular model, but it seems weird you're using the ID in there as this is unique for every row of data thus preventing any grouping and creating errors. Are you sure the ID should be like that?

This formula for example works:

model <- lmer(Scores ~ (Time | Group), positiveaffect)

Linear mixed model fit by REML ['lmerMod']
Formula: Scores ~ (Time | Group)
   Data: positiveaffect
REML criterion at convergence: 577.2305
Random effects:
 Groups   Name        Std.Dev. Corr 
 Group    (Intercept) 6.170         
          Time        2.782    -1.00
 Residual             6.155         
Number of obs: 89, groups:  Group, 2
Fixed Effects:
(Intercept)  
       19.6  
optimizer (nloptwrap) convergence code: 0 (OK) ; 0 optimizer warnings; 1 lme4 warnings 

PJ

So I don't need to take into account ID in the formula?

Group is a between-subjects factor, Time is within!

When I try anova(model) , no P-values are printed...why?

@pieterjanvc

You can't take it into account. You only have one observation per ID (subject), it is completely confounded with residual error. If you want time within ID, eg some kind of random slope model, you need multiple observations per ID.

Sure! I think it should be:

model <- lmer(Scores ~ Group + Time + Group*Time + (1 | ID), positiveaffect)
anova(model)

I get no output! Do you get an output?

@ron @pieterjanvc

What message is it giving you?

You are still including ID as a random effect when you have as many ID as there are observations. This cannot work.

Ron.

Hey Ron,

I have 2 observations per ID! Will it work now?

It's not giving me an error message, just no values (This is when I try the formula you suggested):

anova(model)
Analysis of Variance Table
npar Sum Sq Mean Sq F value

2 obs per ID should run.

That formula was not suggested by me - it's a while since I used lmer.

Is anything in the model object? Try summary(model) or just type model and see what comes out.

model <- lmer(Scores ~ Group + Time + Group*Time + (1 | ID), positiveaffect)
anova(model)

Gives me:

Analysis of Variance Table
npar Sum Sq Mean Sq F value
Group 1 111.074 111.074 3.5736
Time 1 94.044 94.044 3.0257
Group:Time 1 85.422 85.422 2.7483

So it is working (yay!) as I have updated the ID column!

But I want a P-value to be printed... How can I get that?

@Ron @pieterjanvc

When I try:

model <- lme(Scores ~ Group + Time + Group*Time, random= ~1|ID, positiveaffect)
anova(model)

It gives me a P-value!

Thanks so much for your help :slight_smile:

Just to confirm, that should work? @Ron @pieterjanvc

Hi again,

I'm not familiar with this particular model and not an expert in stats, so I don't dare to confirm, I was just pointing out from a programmatic / modelling standpoint :slight_smile:

It seems @ron knows much more about this than I do, I'm more into the informatics.

PJ

1 Like

Or should it be:

model <- lme(Scores ~ Group + Time + Group*Time, random= ~Time|ID, positive)

As the subject has a data point at each of the two time points!?

I believe that should work. Or, alternatively, you could try using the lmerTest package with the call to lmer.

I don't know about the ~time|ID option, as I said it's a long timesince I've done any modelling using these tools and I can't visualise quite what is being fitted here, at least without giving it a bit more time than I have available right now. But it may make a difference whether time is a continuous variable or a factor. If a continous you might want both a random slope and a random intercept.

I woudl suggest you talk to a statistician if possible, and discuss the structure of the data/study with him and what you are hoping to be able to test/investigate.

Sure thing! Thanks so much for all your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.