# Random effects in repeated-measures design using lme

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:

'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")


or

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?

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)