I would like to model a treatment effect in two different groups, controlled for some co-variates (like age and education), and I assume that a two-way repeated-measure Anova would be the right approach – if yes, I have some questions on how to model this design.
I'm a bit confused on how to do this with R (and the lme4
package), because I found different approaches for the same design. Let's say, I have following variables:
- subject
- group (control vs treatment group)
- time (t0 vs t1, i.e. two measures for each subject)
- age (co-variate)
- education (co-variate)
Am I right, that, according to this posting on Cross Validated, my model would look like this?
- model:
lmer(DV ~ group * time + age + education + (1+time|subject), mydata)
Then I found this tutorial. Following these instructions, my model would look like this?
- model:
lmer(DV ~ group * time + age + education + (1|subject) + (1|group:subject) + (1|time:subject), data=mydata)
Now I have two questions:
a) which of the two above models is correct? or do both work?
b) my data is in long format, how should my variable subject
look like? the same value for each measured person, i.e. a value appears twice in this variable (for person A in group X at t0 and person A in group X at t1 the same value), or should each row/observation be indicated by a new, unique ID?
Best Answer
I think I now know which model works, so I can answer the question for myself. Both models work, it depends on the subject-variable.
To get a better understanding of which random parts to use, I have computed four models:
fit <- lme4::lmer(DV ~ group * time + age + education + (1|lfd) + (1|group:lfd) + (1|time:lfd), data = mydata) fit2 <- lme4::lmer(DV ~ group * time + age + education + (1+time|lfd), data = mydata) fit3 <- lme4::lmer(DV ~ group * time + age + education + (1|subject), data = mydata) fit4 <- lme4::lmer(DV ~ group * time + age + education + (1|lfd), data = mydata)
All four models produce the same (fixed-effects) results. lfd
is a repeating number, which repeats an ID 4 times: once per group and once per time (so 2 groups by 2 time points are 4 groups).
subject
is a repeated ID for each group in both time points, i.e. I have just 2 groups (group A and B), not further distinguish by time
.
For me, the quintessence – after trying to better understand 2-way repeated measures with mixed models – is:
I think that you don't need to worry about nesting as long as you don't repeat subject ID's within treatment groups.
(as already mentioned in this answer, but at that time not understood by me. 😉 )