# load libraries
library(tidyverse)
library(lme4)
library(sjPlot)
library(ggeffects)
library(haven)
# load HSB data
<- read_dta("data/hsb.dta") |>
hsb select(mathach, ses, schoolid) |>
group_by(schoolid) |>
mutate(grp_mean_ses = mean(ses)) |>
ungroup() |>
mutate(grp_center_ses = ses - grp_mean_ses)
22 Within, Between, and Contextual Effects
Many find it hard to keep track of within, between, and contextual effects in MLMs. This short walkthrough shows how to fit and interpret each model using the HSB data.
We first calculate school-average ses and then group mean center ses within school. This makes two variables, grp_mean_ses (level 2) and grp_center_ses (level 1).
22.1 Fitting the Models
We next fit a variety of models to compare:
<- lm(mathach ~ ses, hsb)
ols <- lm(mathach ~ ses + factor(schoolid), hsb)
fe <- lmer(mathach ~ ses + (1|schoolid), hsb)
ri <- lmer(mathach ~ grp_center_ses + (1|schoolid), hsb)
ri_within <- lmer(mathach ~ grp_mean_ses + (1|schoolid), hsb)
ri_between <- lmer(mathach ~ grp_center_ses + grp_mean_ses + (1|schoolid), hsb)
re_wb <- lmer(mathach ~ ses + grp_mean_ses + (1|schoolid), hsb)
contextual
tab_model(ols, fe, ri, ri_within, ri_between, re_wb, contextual,
p.style = "stars",
show.ci = FALSE,
show.se = TRUE,
keep = "ses",
show.dev = TRUE,
dv.labels = c("OLS",
"Fixed Effects",
"Rand. Int.",
"RI Within",
"RI Between",
"REWB",
"Mundlak"))
OLS | Fixed Effects | Rand. Int. | RI Within | RI Between | REWB | Mundlak | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Predictors | Estimates | std. Error | Estimates | std. Error | Estimates | std. Error | Estimates | std. Error | Estimates | std. Error | Estimates | std. Error | Estimates | std. Error |
ses | 3.18 *** | 0.10 | 2.19 *** | 0.11 | 2.39 *** | 0.11 | 2.19 *** | 0.11 | ||||||
grp center ses | 2.19 *** | 0.11 | 2.19 *** | 0.11 | ||||||||||
grp mean ses | 5.86 *** | 0.36 | 5.87 *** | 0.36 | 3.68 *** | 0.38 | ||||||||
Random Effects | ||||||||||||||
σ2 | 37.03 | 37.01 | 39.16 | 37.02 | 37.02 | |||||||||
τ00 | 4.77 schoolid | 8.67 schoolid | 2.64 schoolid | 2.69 schoolid | 2.69 schoolid | |||||||||
ICC | 0.11 | 0.19 | 0.06 | 0.07 | 0.07 | |||||||||
N | 160 schoolid | 160 schoolid | 160 schoolid | 160 schoolid | 160 schoolid | |||||||||
Observations | 7185 | 7185 | 7185 | 7185 | 7185 | 7185 | 7185 | |||||||
R2 / R2 adjusted | 0.130 / 0.130 | 0.235 / 0.218 | 0.077 / 0.182 | 0.044 / 0.225 | 0.123 / 0.179 | 0.167 / 0.224 | 0.167 / 0.224 | |||||||
Deviance | 295643.779 | 259918.446 | 46641.008 | 46720.415 | 46959.128 | 46563.821 | 46563.821 | |||||||
* p<0.05 ** p<0.01 *** p<0.001 |
22.2 Interpretation
22.2.1 OLS
lm(formula = mathach ~ ses, data = hsb)
Ignoring school membership, students who are 1-unit higher in SES are predicted to score 3.18 points higher in math. This is generally not a preferred model.
22.2.2 Fixed Effects
lm(formula = mathach ~ ses + factor(schoolid), data = hsb)
For students within a given school, students who are 1-unit higher in SES are predicted to score 2.19 points higher in math. Fixed effects models focus on within-school comparisons: we are looking at how students within schools relate to each other, and then averaging this relationship across all our schools to get our final estimate.
22.2.3 Random Intercepts
lmer(formula = mathach ~ ses + (1 | schoolid), data = hsb)
Students who are 1-unit higher in SES are predicted to score 2.39 points higher in math; schools that are 1-unit higher in mean SES are predicted to have mean math scores 2.39 points higher.
The random intercept model gives a precision-weighted average of the within and between effects. Looking at the other models, note that our within effect is 2.19 and our between effect is 5.86. If the RE assumption holds, these are the same in the population, so we get more precision by averaging them together. However, in social science, they are rarely the same, making this model provide a weird blend of two kinds of mechanism.
22.2.4 Random Intercepts, Within Effect
lmer(formula = mathach ~ grp_center_ses + (1 | schoolid), data = hsb)
Holding constant school, students who are 1-unit higher in SES are predicted to score 2.19 points higher in math. This is the same coefficient as the FE model, but in an RI framework. We have “controlled for school” manually by demeaning the SES variable.
22.2.5 Random Intercepts, Between
lmer(formula = mathach ~ grp_mean_ses + (1 | schoolid), data = hsb)
Schools that are 1 unit higher in mean SES are predicted to have mean math scores 5.86 points higher. This looks very large, but remember that variation in school mean SES is typically much less than the variation in student ses scores. In particular, we can calculate the standard deviation of school mean ses’es to get:
<- hsb |>
schools ::select( schoolid, grp_mean_ses ) |>
dplyrunique() |>
summarise( n = n(),
sd = sd( grp_mean_ses ) )
schools
# A tibble: 1 × 2
n sd
<int> <dbl>
1 160 0.414
22.2.6 Random Effects within and Between
lmer(formula = mathach ~ grp_center_ses + grp_mean_ses + (1 |
schoolid), data = hsb)
Holding constant school, students who are 1-unit higher in SES are predicted to score 2.19 points higher in math; schools that are 1-unit higher in mean SES are predicted to have mean math scores 5.86 points higher. We get the within and between effects in a single model!
22.2.7 Contextual/Mundlak
lmer(formula = mathach ~ ses + grp_mean_ses + (1 | schoolid),
data = hsb)
Holding constant school, students who are 1-unit higher in SES are predicted to score 2.19 points higher in math; holding constant student SES, a student that attends a school with 1-unit higher in mean SES are predicted to have mean math scores 3.68 points higher. The contextual effect is the difference in the within and between effects (note that 5.86 - 2.19 = 3.68, up to rounding), and, in principle, its significance test allows us to determine if having both is necessary.
Mathematically, the Mundlak model and REWB are identical, as you can see from the deviance statistics. You would choose one over the other depending on your preferred interpretation.
22.3 Further Reading
Check out the Raudenbush and Bryk pages on within vs. between. Also see, if desired, read Antonakis, Bastardoz, and Rönkkö (2019).