8  Exercises

Let’s try what we have covered so far on new data. The solutions are collapsed. Write your own code first, then open them.

8.1 Data: children’s dental growth (Orthodont)

This dataset is from the nlme package (included with R by default). For 27 children (16 boys, 11 girls), the distance (mm) from the pituitary to the pterygomaxillary fissure was measured at ages 8, 10, 12, and 14.

data(Orthodont, package = "nlme")
ortho <- as.data.frame(Orthodont)
ortho$Subject <- factor(as.character(ortho$Subject))
head(ortho)
  distance age Subject  Sex
1     26.0   8     M01 Male
2     25.0  10     M01 Male
3     29.0  12     M01 Male
4     31.0  14     M01 Male
5     21.5   8     M02 Male
6     22.5  10     M02 Male
  • distance: distance (mm) ← response variable
  • age: age (8, 10, 12, 14)
  • Subject: child ID ← group
  • Sex: sex (Male / Female)

8.2 Exercise 1: Visualization

Draw the relationship between age and distance for each child as a line, colored by sex. What trends do you see?

ggplot(ortho, aes(age, distance, group = Subject, colour = Sex)) +
  geom_line(alpha = 0.7) + geom_point() +
  labs(x = "Age", y = "Distance (mm)")

  • Distance increases with age
  • The starting level differs by child (a random intercept seems necessary)
  • Boys seem larger overall, and seem to grow more

8.3 Exercise 2: Random-intercept model

Fit a model that adds a by-child random intercept to distance ~ age, and find

  1. the amount of growth per year
  2. the variation between children (SD) and the residual SD
  3. the ICC
m1 <- lmer(distance ~ age + (1 | Subject), data = ortho)
summary(m1)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: distance ~ age + (1 | Subject)
   Data: ortho

REML criterion at convergence: 447

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.6645 -0.5351 -0.0129  0.4874  3.7218 

Random effects:
 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 4.472    2.115   
 Residual             2.049    1.432   
Number of obs: 108, groups:  Subject, 27

Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
(Intercept)  16.76111    0.80240 105.02536   20.89   <2e-16 ***
age           0.66019    0.06161  80.00000   10.72   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
    (Intr)
age -0.845
vc <- as.data.frame(VarCorr(m1))
icc <- vc$vcov[1] / sum(vc$vcov)
icc
[1] 0.6857391
  1. Growth of about 0.66 mm per year
  2. Between-child SD ≈ 2.11, residual SD ≈ 1.43
  3. ICC ≈ 0.69. Most of the variation is due to differences between children

8.4 Exercise 3: Centering age

The intercept of the model in Exercise 2 is “the distance at age 0”, which is outside the range of the data and cannot be interpreted. Create a variable age8 by subtracting 8 from age, and refit the model so that the intercept is “the average distance at age 8”. Does the slope estimate change?

ortho$age8 <- ortho$age - 8
m1c <- lmer(distance ~ age8 + (1 | Subject), data = ortho)
rbind(Original = fixef(m1), Centered = fixef(m1c))
         (Intercept)       age
Original    16.76111 0.6601852
Centered    22.04259 0.6601852

The slope does not change; only the intercept changes to “the value at age 8”.

8.5 Exercise 4: Random-slope model

Fit a model m2 in which the slope of age8 also varies by child, and compare it with the model m1c from Exercise 3. Is the random slope needed?

m2 <- lmer(distance ~ age8 + (age8 | Subject), data = ortho)
VarCorr(m2)
 Groups   Name        Std.Dev. Corr  
 Subject  (Intercept) 1.88663        
          age8        0.22643  0.209 
 Residual             1.31004        
anova(m1c, m2, refit = FALSE)
Data: ortho
Models:
m1c: distance ~ age8 + (1 | Subject)
m2: distance ~ age8 + (age8 | Subject)
    npar    AIC    BIC  logLik -2*log(L)  Chisq Df Pr(>Chisq)
m1c    4 455.00 465.73 -223.50    447.00                     
m2     6 454.64 470.73 -221.32    442.64 4.3658  2     0.1127

The fixed effects are the same, so we compare with REML as is (refit = FALSE). Decide by looking at the p-value and AIC. If the p-value is around 0.05, also take into account that the p-value is conservative because of the boundary problem (Chapter Chapter 6). In this data, individual differences in the slope are fairly small, and the conclusions about the fixed effects hardly change whichever model you choose.

8.6 Exercise 5: Effect of sex

Let’s examine whether there is a sex difference in how children grow.

  1. Create a model m3 that adds Sex and age8:Sex (the interaction) as fixed effects (the random effect (1 | Subject) is fine)
  2. Compare it with the model without the interaction, m3a, using a likelihood ratio test
  3. Explain the results in words
m3  <- lmer(distance ~ age8 * Sex + (1 | Subject), data = ortho)
m3a <- lmer(distance ~ age8 + Sex + (1 | Subject), data = ortho)
summary(m3)$coefficients
                 Estimate Std. Error      df   t value     Pr(>|t|)
(Intercept)    22.6156250  0.5387528 37.1373 41.977744 6.682335e-33
age8            0.7843750  0.0775011 79.0000 10.120823 6.441815e-16
SexFemale      -1.4065341  0.8440633 37.1373 -1.666385 1.040507e-01
age8:SexFemale -0.3048295  0.1214209 79.0000 -2.510520 1.409745e-02
anova(m3a, m3)   # fixed effects differ, so the models are refitted with ML
Data: ortho
Models:
m3a: distance ~ age8 + Sex + (1 | Subject)
m3: distance ~ age8 * Sex + (1 | Subject)
    npar    AIC    BIC  logLik -2*log(L)  Chisq Df Pr(>Chisq)  
m3a    5 444.86 458.27 -217.43    434.86                       
m3     6 440.64 456.73 -214.32    428.64 6.2174  1    0.01265 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. The reference category is Male (check with levels(ortho$Sex)).
  • (Intercept): average distance for boys at age 8
  • age8: growth per year for boys
  • SexFemale: difference girls − boys at age 8
  • age8:SexFemale: difference in growth rate (girls − boys). If negative, girls grow more slowly

If the p-value of the likelihood ratio test is small, we can say that “there is a sex difference in growth rate”.

ortho$pred <- predict(m3)
ggplot(ortho, aes(age, distance, colour = Sex)) +
  geom_point(alpha = 0.5) +
  geom_line(aes(y = pred, group = Subject), alpha = 0.5) +
  labs(title = "Model with sex × age interaction")

8.7 Exercise 6: Diagnostics

For the model m3 from Exercise 5, draw a residuals vs fitted plot and a Q-Q plot of the residuals, and check whether there are any problems.

dd <- data.frame(fitted = fitted(m3), resid = resid(m3), Subject = ortho$Subject)

ggplot(dd, aes(fitted, resid)) +
  geom_point() + geom_hline(yintercept = 0, linetype = 2)

ggplot(dd, aes(sample = resid)) + stat_qq() + stat_qq_line(colour = "tomato")

# check the observations with the largest residuals
ortho[order(-abs(resid(m3)))[1:4], c("Subject", "age", "distance", "Sex")]
   Subject age distance  Sex
35     M09  12     31.0 Male
49     M13   8     17.0 Male
34     M09  10     20.5 Male
52     M13  14     29.5 Male

In this data, measurements of some children (e.g., M09, M13) show fairly large residuals. It is a good idea to consider whether they may be outliers, and to check whether the conclusions change when they are removed (sensitivity analysis).

8.8 Advanced exercises

  1. In the simulation in Chapter Chapter 1, what happens to the false positive rate of lmer if you reduce the number of schools from 20 to 6? (A caution for when there are few groups)
  2. Modify sim_school() to create data in which the slope also differs by school, and check whether a random-slope model can recover the true values.
  3. In sleepstudy, try analyzing Days 0–1 (the training period) separately from day 2 onward (in fact, sleep restriction in this data starts from day 2).

Well done!