Two-Way Repeated-Measure design with lme4

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?

1. 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?

1. 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?

• Ok, I could answer myself at least point b). When I use a subject-variable, where each "row" of the data frame gets a new ID, ' lme4' throws an error saying that number of grouping levels must be less than number of observations. So the variable subject-ID needs identical values for each subject (person) at t0 and t1 (in between subjects). – Daniel Mar 28 '15 at 8:06

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. ;-) )

• This blog posting provides a very good overview of making decisions on model designs and how to apply lmer or lme function. – Daniel Apr 25 '15 at 11:32
• Let me rephrase my question... What does "lfd" stand for? I see your explanation about its being a repeating number... Can you show the head(data)? – Antoni Parellada Oct 1 '15 at 20:24
• lfd is a repeated number, which is unique in group A at time 0, group B at time 0, group A at time 1 and group B at time 1. So, each value of lfd appear four times (thus, subject Peter from group A and subject Mary from group B have the same value both at t0 and t1). Think of having group A at t0, 500 subjects, numbered from 1 to 500. group B at t0, also numbered from 1 to 500. group A at t1 numbered from 1 to 500 and group B at t1 numbered from 1 to 500. – Daniel Oct 2 '15 at 8:50