ME inequality in R
Inequality summary effects of nominal and ordinal independent variables
The ME inequality statistic from Mize and Han (2025) summarizes the effect of a nominal or ordinal independent variable in a single number. A nominal variable with L categories has L(L-1)/2 pairwise marginal effects – one for every pair of categories. ME inequality is the average absolute pairwise difference in predictions across the categories: it answers “how much does the outcome differ across the levels of this variable, overall?” It can be compared across predictors, across models, and across groups.
In R, you can calculate it with the marginaleffects package, which handles the marginal effects and the standard errors. For the weighted version, a small helper function, meineq_weights(), calculates the weights. The same statistic is available in Stata with the meinequality command.
Setup
meineq_weights() is in my Rfunctions repository on GitHub. Load it with source():
library(marginaleffects) # Marginal effects and hypotheses
source("https://raw.githubusercontent.com/tdmize/Rfunctions/main/ME_helper_functions.R")Weighted ME inequality
Weighted ME inequality is the default in Mize and Han (2025). Each pairwise comparison counts in proportion to the share of the sample in the two groups being compared. Fit the model with the nominal variable as a factor, then:
weights <- meineq_weights(mod, nominalvar)
wmean <- function(x) weighted.mean(x, weights)
avg_comparisons(mod,
variables = list(nominalvar = "pairwise"),
hypothesis = ~ I(wmean(abs(x))))avg_comparisons() calculates every pairwise difference in predictions, and hypothesis turns them into their weighted average absolute difference. meineq_weights() takes the model rather than the data, so the weights come from the model’s estimation sample. For survey models, it uses the survey-weighted share of each category.
Unweighted ME inequality
The unweighted version gives every pairwise comparison the same weight:
avg_comparisons(mod,
variables = list(nominalvar = "pairwise"),
hypothesis = ~ I(mean(abs(x))))Examples
The examples reproduce the ME inequality examples from Mize and Han (2025):
- Inequality as a summary measure (Example 4.1): the ME inequality of race-ethnicity in a linear regression of wages.
- Inequality in categorical models (Example 4.2.b): ME inequalities for several predictors in a binary logit.
- Comparing ME inequalities across models (Example 4.3.b): testing mediation, with models combined by
suest. - Comparing ME inequalities across groups (Example 4.3.a): models for two different time periods, combined by
suest.
Citation
If you use these measures, please cite:
Mize, Trenton D. and Bing Han. 2025. “Inequality and Total Effect Summary Measures for Nominal and Ordinal Variables.” Sociological Science 12:115–157.