# Modeling repeated measures with a second layer of nesting using lmer

I am doing a longitudinal repeated measures study looking at the effect of age (maturation) on my test result (Y - a type of hearing test). My outcome Y, is numeric, and the main effect is age (age.group, factor with 4 levels). Sex and ear.side (right and left) are covariates. I test both ears of each subject at each age (age.group). Each subject has an ID (sub.id), and each observation (ear) also has an ID (ear.id).

I am using lmer (from the lme4 package in R) to model the data. My simple model is lmer(Y = age.group + sex + ear.side + (age.group|ear.id), data), which models the repeated measures of each ear, allowing ears to have their own intercept and also slope as they age.

However, there are two measurements from each subject at each age.group (right and left ear), which are likely to be correlated, and I would like to model this as well, but am unsure how to go about it. Currently I have lmer(Y = age.group + sex + ear.side + (age.group|ear.id) + (1|age.group/sub.id), data), is that right? If not, how can I model that within each age group, observations from the same subject (sub.id) are likely to be correlated?

You could take a look at the question and answers here for details about clustering in this type of context

lmer(Y = age.group + sex + ear.side + (age.group|ear.id) + (1|age.group/sub.id), data)


Note that this is the same as:

lmer(Y = age.group + sex + ear.side + (1 + age.group|ear.id) + (1|age.group/sub.id), data)


where I have made it explicit that random intercepts for ear.id will be fitted.

However, (1|age.group/sub.id) is saying that observations are clustered on sub.id and age.group, with the former nested in the latter. This does not make sense. age.group should remain a fixed effect.

The 2nd level of clustering you have, is ear.id within sub.id, so the part after the "|" should read sub.id/ear.id. Putting it all together, we have:

lmer(Y = age.group + sex + ear.side + (1|sub.id/ear.id), data)


or, with random slopes:

lmer(Y = age.group + sex + ear.side + (1 + age.group|sub.id/ear.id), data)

• Thanks Robert, From that post and answer I can see that my study is nested, not crossed, and because I have a unique id for each ear (ear.id), I could use either: lmer(Y = age.group + sex + ear.side + (1|sub.id/ear.id), data), or lmer(Y = age.group + sex + ear.side + (1|sub.id) + (1|ear.id), data). – josh myers Jun 25 '18 at 4:53
• I understand now that it only matters that ear is nested within each subject. I was thinking that I needed to model the nesting at each time point but "Nested at a particular time doesn’t count" (Bates, 2009. lme4.r-forge.r-project.org/slides/2009-07-01-Lausanne/…). – josh myers Jun 25 '18 at 4:54
• @joshmyers you;ve got it :) – Robert Long Jun 25 '18 at 7:44
• My test result is also measured at a bunch of different frequencies (factor with 11 levels) in each ear. My model is actually lmer(Y = frequency * age.group + sex + ear.side + (1|sub.id/ear.id), data). I omitted the variable frequency from my question for simplicity, as I was thinking it is just a main effect, and not relevant to the random structure. But I think it may be important because frequencies are nested in ears as well. I think my model should be `lmer(Y = frequency * age.group + sex + ear.side + (1|sub.id/ear.id/frequency), data), is that correct? – josh myers Aug 22 '18 at 3:18
• I posted as a new question here – josh myers Aug 23 '18 at 2:35