I have a repeated-measures design where participants were measured 4 times each on 2 consecutive days. There were 2 conditions, randomly attributed to day 1 and day 2 for each participant.

So, this is how the head of my data 'dat' look:

So, this is how the head of my data 'dat' look:

'subject' is just the IDs, 'DV' is my outcome/dependent variable.

I want to know how 'day', condition ('cond'), and 'measurement' influence 'DV' using R's lme package. Even after a rather excessive search, I'm still not sure how to set the random effects...

So far, I've done something like:

m_base <- lme(DV ~ 1, random = ~1|subject/cond/measurement, data = dat, method = "ML")


m_base <- lme(DV ~ 1, random = ~1|subject/day/measurement, data = dat, method = "ML")

and then stepwise added my variables as fixed effects using update() and compared the models using anova().

Still, I'm not sure about how to set the random effects, since 'measurement' is somehow nested within both 'day' and 'cond'. Does anyone know a correct way of implementing my question?


1 Answer 1


It appears that you have a case of a partially crossed, partially nested design, because if I understand correctly, day and cond are crossed (ie neither are nested in the other), while both appear to be nested within subject. measurement is an id variable that indexes the measurement occasion on each day and within each condition, and as such should not be treated as a random factor because there is only one observation of the dependent variable for each measurement occasion. Even though they are indexed as 1-4 for each day/condition, they are different measurements (that is, measurement 1 for day 1 condition 0 and measurement 1 for day 1 condition 1 are not the same measurement) and therefore there can be no random variation in it. If you specified it as random in the way you have coded the data above, it would be a mistake.

If this is the case, then lme is unable to fit such a model, and you could use something like lme4 instead. You could specify the structure in lme4 as follows:

DV ~ 1 + (1|subject) + (1|day) + (1|cond) + (1|subject:day) + (1|subject:cond)

If measurement is a measurement of time within each day or cond and you expect some temporal effect, then you could include measurement as a fixed effect (and also potentially fit random slopes, if the data supported such a model)

However, fitting a model with random intercepts for day and cond would not be a good idea because you have only 2 of each, so you would be asking the software to estimate a variance for a normally distributed variable having only 2 observations, which does not make any sense. So a better way forward is to treat day and cond as fixed effects, and simply fit random intercepts for subject:

DV ~ day + cond + (1|subject)

The fact that day and cond were randomly assigned is not relevant.

The same comment as above applies for measurement again here. That is, you might want to fit

DV ~ day + cond + measurement + (1|subject)

and again, you could also have random slopes for day and/or cond and/or measurement if suggested by the domain theory and supported by the data.

Of course, now that we have discarded day and cond as random, you can go back to the nlme package if you wish (athough lme4 is really the successor to nlme for most cases)

  • $\begingroup$ The use of fixed effects because only 2 obs/factor makes sense to me, but doesn't this present a problem because day and condition are completely confounded within each subject? I'm looking at the DV ~ day + cond + (1|subject) line. $\endgroup$ May 4, 2021 at 15:45
  • $\begingroup$ @MichiganWater what do you mean by "completely confounded within each subject", and in what sense does it present a problem for fixed effects estimation ? In this type of situation I would usually run a bunch of simulations of data based on the experimental design, and check that I get sensible results from the model(s). I can't honestly remember if I did that when I answered this question, as it's a while ago now. $\endgroup$ May 4, 2021 at 17:33
  • $\begingroup$ I'm not sure the best way to explain it, so I'll just use an example of the first subject ID01. There'll be a difference calculated between the first 4 measurements and the second 4 measurements, but there's no way to attribute that difference to either 'day' or 'cond' because they are perfectly correlated. Looking across subjects, sometimes cond=0 occurs on day=1 and sometimes on day=2, but there's no way to break that correlation within a subject. I simulated a bit and got fixed-effect model matrix is rank deficient $\endgroup$ May 4, 2021 at 18:08
  • $\begingroup$ @MichiganWater OK I see what you mean. Without delving into this question again I can't really say much more. In principal there shouldn't be a problem but if the fixed effects model matrix is singular that points to a problem with the experimental design or maybe an issue with missing data. We were never given the whole dataset so it's possible there are other measurements for the IDs in the extract. Maybe the OP can comment (though they haven't been seen for over a month). Or if you wanted to ask a questiion with some simulated data, I could take a look. Feel free to ping me here if you do. $\endgroup$ May 4, 2021 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.