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

Timothy MacKenzie |@w|@wt @end|ng |rom gm@||@com
Thu Sep 9 17:37:43 CEST 2021


Dear Colleagues,

I'm revising my question for better clarity. Suppose we have two
nested grouping variables (ID1 and ID2). The data structure looks like
below.

To obtain the contextual effects for ID1 and ID2, I wonder which
option is appropriate?

#-- Option 1: Create mean of X across ID1 (X_ave_ID1) and mean of X
across ID2 *ignoring* ID1 (X_ave_ID2)
#-- Option 2: Create mean of X across ID1 (X_ave_ID1) and mean of X
across ID2    *within*  ID1 (X_ave_ID2)

For both options, then, I will fit (in lme4::lmer()):

y ~ X + X_ave_ID1 + X_ave_ID2 + (1 | ID1 / ID2)

Thank you,
Tim

#------ DATA  STRUCTURE AND R CODE:

ID1    ID2                   X                      y
1       1                       0.474111397    1.9534671
1       1                      -0.712228120   0.9355230
1       2                      -0.009957293   1.1088756
1       2                      -1.237918646   0.8675550
2       1                      -0.554944765   2.7831133
2       1                      -0.320668268   0.1479290
2       2                       1.066993108   0.1688187
2       2                     -1.084870417    1.0536264

library(dplyr)

#-- Option 1:
    data %>%
    group_by(ID1) %>%
    mutate(X_ave_ID1 = mean(X)) %>%
    group_by(ID2) %>%
    mutate(X_ave_ID2 = mean(X))

#-- Option 2:
  data %>%
  group_by(ID1) %>%
  mutate(X_ave_ID1 = mean(X)) %>%
  group_by(ID2, .add = TRUE) %>%
  mutate(X_ave_ID2 = mean(X))

On Mon, Sep 6, 2021 at 1:31 PM Timothy MacKenzie <fswfswt using gmail.com> wrote:
>
> 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