Comparing ME inequalities across groups
Example 4.3.a of Mize and Han (2025), in R: models fit on different samples
When different samples are used for each model – different groups, or different time periods – fit a model to each sample and combine them with suest. Here the question is whether religious-tradition differences in willingness to let a gay man speak in public were larger before 1980 than after 2010. The same example in Stata is on the meinequality page.
Load and prepare the data
library(haven) # Read Stata data
library(marginaleffects) # Marginal effects and hypotheses
library(suest) # Combine models
gss <- read_dta("https://tdmize.github.io/data/data/cda_gss.dta")
vars <- c("spkhomo", "reltrad", "age", "woman", "year")
gss <- gss[complete.cases(gss[vars]), vars]
gss[c("spkhomo", "reltrad", "woman")] <- lapply(gss[c("spkhomo", "reltrad", "woman")], as_factor)Fit and combine the models
premod <- glm(spkhomo ~ reltrad + age + woman,
family = binomial("logit"), data = gss, subset = year < 1980)
postmod <- glm(spkhomo ~ reltrad + age + woman,
family = binomial("logit"), data = gss, subset = year >= 2010)
c(nobs(premod), nobs(postmod))
fit <- suest(premod, postmod, model_names = c("Before 1980", "2010 and later"))[1] 5593 8226
Unweighted ME inequality in each period
suest_newdata() stacks the two estimation samples, so each model’s effects are averaged over its own sample. A small function takes the mean absolute pairwise difference within each model:
meineq <- function(x) {
groups <- unique(x$group)
est <- sapply(groups, function(g) mean(abs(x$estimate[x$group == g])))
data.frame(term = groups, estimate = est)
}
ineq <- avg_comparisons(fit,
variables = list(reltrad = "pairwise"),
newdata = suest_newdata(fit),
hypothesis = meineq)
ineq Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
Before 1980 0.1556 0.01086 14.3 <0.001 152.3 0.1343 0.1768
2010 and later 0.0722 0.00661 10.9 <0.001 89.7 0.0592 0.0851
Type: response
Test the difference
hypotheses(ineq, hypothesis = difference ~ revpairwise) Hypothesis Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
(Before 1980) - (2010 and later) 0.0834 0.0127 6.56 <0.001 34.1 0.0585 0.108
The ME inequality is significantly larger before 1980 than after 2010: religious traditions have converged on this question. The unweighted version compares the marginal effects alone; the weighted version would also reflect the change in the religious makeup of the GSS between the two periods.