[R-sig-ME] contextual effects in 3-level models
Timothy MacKenzie
|@w|@wt @end|ng |rom gm@||@com
Thu Sep 9 18:17:45 CEST 2021
Dear Thierry,
By contextual effects, I'm referring to the Raudenbush & Bryk (2002) book
pp. 139 - 146. In this section of the book, it is recommended that
researchers dealing with covariates that vary within and between a grouping
variable's levels, separate the within and contextual effects of such
covariates.
The authors only discuss this issue in the context of 2-level models (e.g.
y ~ X + (1 | ID1) ). For example, if X is a variable that varies within and
between each level of ID1, then to get the within and contextual effect of
X, the fixed-effect part of the model will be:
y ~ X + mean_of_X_in_each_ID1_level
In the above parametrization, "mean_of_X_in_each_ID1_level" coefficient
provides the contextual effect of X with the interpretation:
Average difference in y between two observations with the same value of X
that belong to ID1 levels whose mean X values differ by 1 unit.
The question I have posed is an extension of the above for when we have two
nested grouping variables; thus a 3-level model (e.g., y ~ X + (1 | ID1 /
ID2) ).
In this 3-level case, to obtain the contextual effects for ID1 and ID2, I
wonder which option is appropriate?
#-- Option 1: Create mean of X across ID1 levels (X_ave_ID1) and mean of X
across ID2 levels *ignoring* ID1 (X_ave_ID2)
#-- Option 2: Create mean of X across ID1 levels (X_ave_ID1) and mean of X
across ID2 levels *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)
#------ 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 Thu, Sep 9, 2021 at 10:53 AM Thierry Onkelinx <thierry.onkelinx using inbo.be>
wrote:
> Dear Timothy,
>
> This won't work as your averaged X's will be highly correlated with each
> other and with the original X.
>
> I often find it easier to reason on a mathematical model. How would you
> translate your 'contextual' effects into an equation? Or at least
> clarify what a 'contextual' effect is.
>
> Best regards,
>
> ir. Thierry Onkelinx
> Statisticus / Statistician
>
> Vlaamse Overheid / Government of Flanders
> INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
> FOREST
> Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
> thierry.onkelinx using inbo.be
> Havenlaan 88 bus 73, 1000 Brussel
> www.inbo.be
>
>
> ///////////////////////////////////////////////////////////////////////////////////////////
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> ///////////////////////////////////////////////////////////////////////////////////////////
>
> <https://www.inbo.be>
>
>
> Op do 9 sep. 2021 om 17:38 schreef Timothy MacKenzie <fswfswt using gmail.com>:
>
>> 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))
>>
>> _______________________________________________
>> R-sig-mixed-models using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>>
>
[[alternative HTML version deleted]]
More information about the R-sig-mixed-models
mailing list