Skip to content
Prev 2417 / 5636 Next

[R-meta] "Categorical" moderator varying within and between studies

My apologies! I had this backwards in my head. Revised explanation below:

With gender, if you include the group-mean-centered dummy variables and the
cluster-level means, then the contextual effect will be as you described
(gender_M_btw - gender_M_wthn). However, another approach would be to leave
the dummy variables uncentered. If you do it this way, then the coefficient
on gender_M_btw corresponds exactly to the contextual effect, with no need
to subtract out the coefficient on gender_M_wthn.

R code verifying the equivalence of these approaches:

library(dplyr)
library(fastDummies)
library(lme4)

hsb <- read.csv("
https://raw.githubusercontent.com/rnorouzian/e/master/hsb.csv")

hsb2 <- hsb %>%
  mutate(gender = ifelse(female==0,"M","F")) %>%   # create 'gender? from
variable ?female?
  dummy_columns(select_columns = "gender") %>%     # create dummies for
'gender? (creates 2 but we need 1)
  group_by(sch.id) %>%                             # group by cluster id '
sch.id'
  mutate(across(starts_with("gender_"), list(wthn = ~ . - mean(.), btw = ~
mean(.))))

mg_b_w <- lmer(math ~ gender_M_wthn + gender_M_btw + (1|sch.id), data =
hsb2)

mg_b_d <- lmer(math ~ gender_M + gender_M_btw + (1|sch.id), data = hsb2)

fixef(mg_b_w)[["gender_M_btw"]] - fixef(mg_b_w)[["gender_M_wthn"]]
fixef(mg_b_d)[["gender_M_btw"]]
On Thu, Oct 29, 2020 at 1:57 PM Simon Harmel <sim.harmel at gmail.com> wrote:

            

  
  
Message-ID: <CAFUVuJxdXLPnO43kMasCLEvNvuRZiC2EgO8-dzhJAGXVFsQLLg-1174@mail.gmail.com>
In-Reply-To: <CACgv6yVAw1kTOiR=WA1vmtHBZpjhV7y57Bh8+b5E9+iUAwi7ng@mail.gmail.com>