[R-sig-ME] contextual effects in 3-level models

Timothy MacKenzie |@w|@wt @end|ng |rom gm@||@com
Mon Sep 6 20:31:12 CEST 2021


Dear All,

Suppose X is a continuous predictor that can vary within and between
two nested grouping variables in a 3-level linear mixed model:

effect.size ~ X + (1 | studies/outcomes)

How can I obtain the within effect of X, contextual effect of X at
level 2, and contextual effect of X at level 3?

I can think of two options but wonder which one makes more sense
(below)?   For both options, I will fit:

effect.size ~ X + X_ave_study + X_ave_outcome + (1 | studies/outcomes)

Thank you,
Tim

library(dplyr)

#-- Option 1:
    data %>%
    group_by(study) %>%
    mutate(X_ave_study = mean(X)) %>%
    group_by(outcome) %>%                         ## Here mean of
outcome *ignoring* studies is computed
    mutate(X_ave_outcome = mean(X))

#-- Option 2:
  data %>%
  group_by(study) %>%
  mutate(X_ave_study = mean(X)) %>%
  group_by(outcome, .add = TRUE) %>%     ## Here mean of outcome
*within* each study is computed
  mutate(X_ave_outcome = mean(X))



More information about the R-sig-mixed-models mailing list