Chapter 7 Multilevel

This section requires the nlme and ggplot2 packages. The example uses one CONTINUOUS_OUTCOME, two fixed effects, one FACTOR_PREDICTOR and one CONTINUOUS_PREDICTOR, and one random effect FACTOR_LEVEL. Complete step by step guide available soon.

7.1 assessing the need for multilevel: random intercepts

7.1.1 same intercepts across FACTOR_LEVEL: the null model

  • interceptOnly <-gls(CONTINUOUS_OUTCOME ~ 1, data = DF, method = "ML")
  • summary(interceptOnly)

7.1.2 different intercepts across FACTOR_LEVEL: the hierarchical model

  • randomInterceptOnly <-lme(CONTINUOUS_OUTCOME ~ 1, data = DF, random = ~1|FACTOR_LEVEL, method = "ML")
  • summary(randomInterceptOnly)

7.1.3 comparing same vs different

  • anova(interceptOnly, randomInterceptOnly)
  • code chuck with graphic representation of the interceptOnly and randomInterceptOnly models:
ggplot(DF, aes(FACTOR_LEVEL, CONTINUOUS_OUTCOME)) + # randomInterceptOnly model
  stat_summary(fun.data = 'mean_se', geom = 'errorbar', width = 0.2) +
  stat_summary(fun.data = 'mean_se', geom = 'pointrange') +
  geom_point(alpha=0.10) +
  scale_x_continuous(limit = c(0.5, 10.5), breaks = seq(1,10,1)) + 
  geom_hline(yintercept = mean(DF$CONTINUOUS_OUTCOME), linetype = "dotted") # interceptOnly model

7.2 adding the fixed effects: fixed effects and random intercepts

7.2.1 effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME with different intercepts across FACTOR_LEVEL

  • randomInterceptFACTOR_PREDICTOR <-lme(CONTINUOUS_OUTCOME ~ FACTOR_PREDICTOR, random = ~1|FACTOR_LEVEL, method = "ML", data = DF)
  • summary(randomInterceptFACTOR_PREDICTOR)
  • code chuck with an tentative graphic representation of the randomInterceptFACTOR_PREDICTOR model:
ggplot(DF, aes(FACTOR_PREDICTOR, CONTINUOUS_PREDICTOR, colour = FACTOR_PREDICTOR)) + # effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME
  stat_summary(fun = mean, geom = "point") +
  stat_summary(fun.data = 'mean_se', geom = 'errorbar', width = 0.2) +
  stat_summary(fun.data = 'mean_se', geom = 'pointrange') +
  geom_point(alpha=0.2) +
  geom_hline(yintercept = mean(DF$CONTINUOUS_OUTCOME), linetype = "dotted") +
  facet_wrap( ~ FACTOR_LEVEL, nrow = 2) # different intercepts across FACTOR_LEVEL (intercepts not represented)

7.2.2 effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME controlling for CONTINUOUS_PREDICTOR with different intercepts across FACTOR_LEVEL

  • randomInterceptFAC&CONT_PREDICTORS <-lme(CONTINUOUS_OUTCOME ~ FACTOR_PREDICTOR + CONTINUOUS_PREDICTOR, random = ~1|FACTOR_LEVEL, method = "ML",data = DF)
  • summary(randomInterceptFAC&CONT_PREDICTORS)
  • code chuck with an tentative graphic representation of the randomInterceptFAC&CONT_PREDICTORS model:
ggplot(DF, aes(CONTINUOUS_PREDICTOR, CONTINUOUS_OUTCOME, colour = FACTOR_PREDICTOR)) + # effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME controlling for CONTINUOUS_PREDICTOR
  geom_point(alpha=0.4) +
  facet_wrap( ~ FACTOR_LEVEL, nrow = 2) + # different intercepts across FACTOR_LEVEL (intercepts not represented)
  geom_hline(yintercept = mean(DF$CONTINUOUS_OUTCOME), linetype = "dotted")

7.2.3 comparing adding fixed effects

  • anova(randomInterceptOnly, randomInterceptFACTOR_PREDICTOR, randomInterceptFAC&CONT_PREDICTORS)

7.3 adding random slopes: fixed effects and random slopes

7.3.1 effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME with different intercepts across FACTOR_LEVEL controlling for CONTINUOUS_PREDICTOR

  • addRandomSlope <- lme(CONTINUOUS_OUTCOME ~ FACTOR_PREDICTOR + CONTINUOUS_PREDICTOR, random = ~FACTOR_PREDICTOR|FACTOR_LEVEL, method = "ML", data = DF)
  • summary(addRandomSlope)
  • code chuck with an tentative graphic representation of the addRandomSlope model:
ggplot(DF, aes(CONTINUOUS_PREDICTOR, CONTINUOUS_OUTCOME, colour = FACTOR_PREDICTOR)) + # effect of FACTOR_PREDICTOR on CONTINUOUS_OUTCOME controlling for CONTINUOUS_PREDICTOR 
  geom_point(alpha=0.4) +
  geom_smooth(method = "lm", se = FALSE) +
  geom_hline(yintercept = mean(DF$CONTINUOUS_OUTCOME), linetype = "dotted") + 
  facet_wrap( ~ FACTOR_LEVEL, nrow = 2) # with different intercepts across FACTOR_LEVEL 

7.3.2 comparing adding random slopes

  • anova(randomInterceptFAC&CONT_PREDICTORS,addRandomSlope)