2.8 Comparing Effects Across IVs

Research question: what social factors most strongly pattern political views?

Comparing marginal effects within a model

  • In Stata, mecompare can simultaneously calculate multiple MEs, and then use metest for additional tests of equality
    • amount(trimrange) for 5th to 95th percentile change for continuous IVs
    • meinequality option for nominal IVs
  • In R, use avg_comparisons and specify each predictor of interest in a list( )
    • For a significance test of the difference in marginal effects, use the hypothesis option*
      • hypothesis = ~ pairwise will calculate all contrasts, but be careful with multiple exploratory hypothesis tests
  • To compare pure effect sizes and ignore direction, take the absolute value of the marginal effects before testing their equality

The example is adapted from Mize and Han (2025):

use "https://tdmize.github.io/data/data/cda_gss", clear
(cda_gss.dta |  GSS 1972-2021 CDA - Categorical Data Analysis | date created 202
> 3)
keep if year == 2021
(64,814 observations deleted)
drop if missing(polviews, race4, woman, class, age)
(424 observations deleted)
clonevar conpolviews = polviews
regress conpolviews c.age##c.age i.woman i.race4 i.class
      Source |       SS           df       MS      Number of obs   =     3,608
-------------+----------------------------------   F(9, 3598)      =     16.61
       Model |  339.734948         9  37.7483275   Prob > F        =    0.0000
    Residual |  8175.67969     3,598  2.27228452   R-squared       =    0.0399
-------------+----------------------------------   Adj R-squared   =    0.0375
       Total |  8515.41463     3,607   2.3608025   Root MSE        =    1.5074

-------------------------------------------------------------------------------
  conpolviews | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
--------------+----------------------------------------------------------------
          age |      0.037      0.009    4.021   0.000        0.019       0.055
              |
  c.age#c.age |     -0.000      0.000   -2.487   0.013       -0.000      -0.000
              |
        woman |
       Women  |     -0.175      0.051   -3.438   0.001       -0.275      -0.075
              |
        race4 |
       Black  |     -0.331      0.084   -3.945   0.000       -0.496      -0.167
       Other  |     -0.129      0.111   -1.161   0.246       -0.348       0.089
    Hispanic  |      0.011      0.083    0.134   0.893       -0.152       0.174
              |
        class |
working cl..  |     -0.021      0.096   -0.223   0.823       -0.209       0.166
middle class  |     -0.279      0.094   -2.952   0.003       -0.464      -0.094
 upper class  |     -0.536      0.151   -3.556   0.000       -0.831      -0.240
              |
        _cons |      2.986      0.243   12.273   0.000        2.509       3.463
-------------------------------------------------------------------------------
est store mod42b
mecompare age woman race4 class, amount(trimrange) meinequality model(mod42b)
Predicting: Linear prediction
Marginal effects (N_mod42b=3608)

                                 |  ME #   Estimate         SE      P>|z|
---------------------------------+---------------------------------------
age(5-95%)                       |                                       
                          mod42b |     1      0.784      0.082      0.000
---------------------------------+---------------------------------------
woman                            |                                       
                 Women - Men     |                                       
                          mod42b |     2     -0.175      0.051      0.001
---------------------------------+---------------------------------------
race4                            |                                       
                   ME Inequality |     3      0.172      0.053      0.001
               Black - White     |                                       
                          mod42b |     4     -0.331      0.084      0.000
               Other - White     |                                       
                          mod42b |     5     -0.129      0.111      0.246
            Hispanic - White     |                                       
                          mod42b |     6      0.011      0.083      0.893
---------------------------------+---------------------------------------
class                            |                                       
                   ME Inequality |     7      0.273      0.059      0.000
    working cla - lower clas     |                                       
                          mod42b |     8     -0.021      0.096      0.823
    middle clas - lower clas     |                                       
                          mod42b |     9     -0.279      0.094      0.003
    upper class - lower clas     |                                       
                          mod42b |    10     -0.536      0.151      0.000
library(haven)           # import Stata data and labels
library(dplyr)           # data manipulation
library(marginaleffects) # predictions and marginal effects

source("https://raw.githubusercontent.com/tdmize/Rfunctions/main/ME_helper_functions.R")

gss <- read_dta("https://tdmize.github.io/data/data/cda_gss.dta")
gss <- gss |> filter(year == 2021)

# Keep only model vars and drop any missing data on them
gss <- gss[c("polviews", "age", "woman", "race4", "class")]
gss <- gss[complete.cases(gss), ]

# Treat binary/nominal variables as factors; drop categories with no cases
gss <- gss |> mutate(conpolviews = as.numeric(polviews), woman = as_factor(woman),
  race4 = as_factor(race4), class = as_factor(class)) |>
  droplevels()

mod42b <- lm(conpolviews ~ poly(age, 2, raw = TRUE) + woman + race4 + class, data = gss)

# Trimmed range of age: 5th to 95th percentile
quantile(gss$age, c(.05, .95))
 5% 95% 
 25  79 
avg_comparisons(mod42b,
  variables = list(age = quantile(gss$age, c(.05, .95)), woman = "reference"))

  Term    Contrast Estimate Std. Error     z Pr(>|z|)    S  2.5 %  97.5 %
 age   79 - 25        0.784     0.0817  9.59   <0.001 70.0  0.624  0.9440
 woman Women - Men   -0.175     0.0509 -3.44   <0.001 10.7 -0.275 -0.0752

Type: response
avg_comparisons(mod42b, variables = list(race4 = "pairwise", class = "pairwise"),
  hypothesis = meineq(mod42b))

  Term Estimate Std. Error    z Pr(>|z|)    S  2.5 % 97.5 %
 class    0.273     0.0586 4.66  < 0.001 18.3 0.1581  0.388
 race4    0.172     0.0528 3.27  0.00109  9.8 0.0689  0.276

Type: response
metest, clear
metest 1 - abs(2), add rowname("age - woman")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
metest 1 - 3,      add rowname("age - race4")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
metest 1 - 7,      add rowname("age - class")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
                     age - class |     0.511      0.097      0.000 
metest 3 - abs(2), add rowname("race4 - woman")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
                     age - class |     0.511      0.097      0.000 
                   race4 - woman |    -0.003      0.074      0.972 
metest 3 - 7,      add rowname("race4 - class")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
                     age - class |     0.511      0.097      0.000 
                   race4 - woman |    -0.003      0.074      0.972 
                   race4 - class |    -0.101      0.079      0.202 
metest 7 - abs(2), add rowname("class - woman")
                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
                     age - class |     0.511      0.097      0.000 
                   race4 - woman |    -0.003      0.074      0.972 
                   race4 - class |    -0.101      0.079      0.202 
                   class - woman |     0.098      0.075      0.194 
metest, title("Effect size comparison of predictors of political views")
Effect size comparison of predictors of political views

                                 |  estimate         se     pvalue 
---------------------------------+--------------------------------
                     age - woman |     0.609      0.097      0.000 
                     age - race4 |     0.612      0.095      0.000 
                     age - class |     0.511      0.097      0.000 
                   race4 - woman |    -0.003      0.074      0.972 
                   race4 - class |    -0.101      0.079      0.202 
                   class - woman |     0.098      0.075      0.194 
# Effect sizes: ME of age, absolute ME of woman, ME inequality of race4 and class
effect_sizes <- function(x) {
  ineq <- meineq(mod42b)(x[x$term %in% c("race4", "class"), ])
  data.frame(term = c("age", "woman", ineq$term),
    estimate = c(x$estimate[x$term == "age"], abs(x$estimate[x$term == "woman"]),
      ineq$estimate))
}

sizes <- avg_comparisons(mod42b,
  variables = list(age = quantile(gss$age, c(.05, .95)), woman = "reference",
    race4 = "pairwise", class = "pairwise"),
  hypothesis = effect_sizes)
sizes

  Term Estimate Std. Error    z Pr(>|z|)    S  2.5 % 97.5 %
 age      0.784     0.0817 9.59  < 0.001 70.0 0.6238  0.944
 woman    0.175     0.0509 3.44  < 0.001 10.7 0.0752  0.275
 class    0.273     0.0586 4.66  < 0.001 18.3 0.1581  0.388
 race4    0.172     0.0528 3.27  0.00109  9.8 0.0689  0.276

Type: response
# Test the equality of each pair of effect sizes
hypotheses(sizes, hypothesis = ~ revpairwise)

        Hypothesis Estimate Std. Error       z Pr(>|z|)    S  2.5 % 97.5 %
 (age) - (woman)    0.60906     0.0973  6.2600   <0.001 31.3  0.418 0.7997
 (age) - (class)    0.51097     0.0972  5.2548   <0.001 22.7  0.320 0.7016
 (woman) - (class) -0.09809     0.0755 -1.2998    0.194  2.4 -0.246 0.0498
 (age) - (race4)    0.61162     0.0952  6.4215   <0.001 32.8  0.425 0.7983
 (woman) - (race4)  0.00256     0.0739  0.0347    0.972  0.0 -0.142 0.1473
 (class) - (race4)  0.10065     0.0789  1.2753    0.202  2.3 -0.054 0.2553

Age has a larger effect on political views than does gender, race-ethnicity, or social class

The effects of gender, race-ethnicity, and class are similar in size, with no significant differences in effect size

Back to top