# How to model repeated measurements with mixed effect models - lme4

I have a dataset of temperature measurements within a day 6 am, 6 pm, for two groups of patients health and flue, There are repeated measurements on the same patient and same timepoint - measurements taken in the ear, mouth, and armpit. Within the group, the measurement is repeated for both time points on a patient. However, in the flue and healthy group, there are different subjects.

p1 <- data.frame(patient_id = "h1", time = rep(c("6am","6pm"),each=3), group = "health", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,36,3), stringsAsFactors = F )
p2 <- data.frame(patient_id = "h2", time = rep(c("6am","6pm"),each=3), group = "health", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,36.5,3), stringsAsFactors = F )
p3 <- data.frame(patient_id = "h3", time = rep(c("6am","6pm"),each=3), group = "health", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,36.2,3), stringsAsFactors = F )
f1 <- data.frame(patient_id = "f1", time = rep(c("6am","6pm"),each=3), group = "flue", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,37,3), stringsAsFactors = F )
f2 <- data.frame(patient_id = "f2", time = rep(c("6am","6pm"),each=3), group = "flue", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,37.5,3), stringsAsFactors = F )
f3 <- data.frame(patient_id = "f3", time = rep(c("6am","6pm"),each=3), group = "flue", body_part=rep(c("ear", "mouth", "armpit"),2), temperature = rnorm(6,38.8,3), stringsAsFactors = F )

df <- bind_rows(p1,p2,p3,f1, f2,f3)


What I want to find out are the folowing contrasts

• flue - health
• 6am - 6pm

but also contrasts:

• 6am : flue - health,
• 6pm : flue - health,

As well as these:

• flue : 6pm - 6am
• healt: 6pm - 6am

If the interaction term, after running a likelihood ratio test is significant.

This is how I modelled the data with mixed effects models:

m1 <-lmer(temperature ~ group + time + (1 | patient_id) + (1 | body_part), data=df)
m2 <- lmer(temperature ~ group + time + group * time + (1 | patient_id) + (1 | body_part), data=df)
anova(m1, m2)


QUESTION: I am wondering if using this specification I am correctly capturing the repeated measurements on the subjects over time and the repeated measurements (ear, mouth, etc) on a single subject?

And this is how I computed the contrasts for the model without interactions:

glt1time <- multcomp::glht(m1, mcp(time="Tukey"))
summary(glt1time)

glt1 <-multcomp::glht(m1, mcp(group="Tukey"))
summary(glt1)


QUESTION: Is it better to remove the (1|patient_Id) term if computing the contrast for the group factor, since there is no repeated measurement between those groups?

m3 <- lmer(temperature ~ group + time + (1|body_part), data=df)
glt3 <-multcomp::glht(m3, mcp(group="Tukey"))
summary(glt3)


The summary(glt1) and summary(glt3) differ with respect to the standard error estimate.

You seem to be on the right track. I would write the models like this to emphasize the nesting of body parts in subjects and also pay attention to the specification of the interaction between group and time:

m1 <-lmer(temperature ~ group + time +
(1|patient_id/body_part), data=df)

m2 <- lmer(temperature ~ group + time + group:time +
(1|patient_id/body_part), data=df)


Note that model m2 could also be written as:

m2 <- lmer(temperature ~ group*time +
(1|patient_id/body_part), data=df)


Your flue - health and 6am - 6pm contrasts would make sense for model m1, as they are essentially tests of main effects of group and time in that model. Model 1 assumes that that (1) the effect of group is the same at each time point (so it makes sense to test the contrast flue - heslth) and (2) the effect of time is the same in each group (so it makes sense to test the contrast 6am - 6pm).

If model m2 is better supported by your data than model m1, then the remaining contrasts make more sense, since the data will have provided evidence that (i) the effect of group is different at each time point and (ii) the effect of time is different in each group. Usually, people caution against interpreting main effects (such as those of group and time) in a model which contains a significant interaction. However, there are some situations where testing these main effects would make sense, as explained here: https://www.theanalysisfactor.com/interpret-main-effects-interaction/. You would have to plot the mean values of your response, as estimated by model m2, to determine whether you find yourself in one of those situations with model 2, which would warrant testing the contrast flue - health and/or the contrast 6am - 6pm.

• Dear Isabella, thank you. However, in your answer you are mostly talking about the fixed effects. My question is however about how to use random effects to model the repeated measures. Would you be please so kind and shed some light on the suggested random effects term (1|patient_id/body_part). Have great day. – witek Dec 21 '18 at 20:22

A couple of points:

• As you observed, the chosen random-effects structure may affect the inference for the fixed effects. For this reason, often the model-building strategy is the following: (i) Start with a full specification of your fixed-effects structure, including possible interaction and nonlinear terms (i.e., in your case, include the interaction between group and time); (ii) then select the random-effects structure starting with random intercepts, and include each time additional random effects and test whether they improve the fit of the model using likelihood ratio tests (i.e., in your case you could start with a random intercept per patient_id, then include also the nested random intercept per body_part, then consider including random slopes for time first in the patient_id, and then in the body_part Level); (iii) when you select the random effects, you can go back to the fixed-effects structure and see which effects are important.
• Regarding the specification of the nested random effects, check the last bullet in the Nested or Crossed Section of the GLMM FAQ.