17.7 Packages for Marginal Effects

Several R packages compute marginal effects for regression models, each with different features and functionalities. The primary packages include:

  1. marginaleffects – A modern, flexible, and efficient package.
  2. margins – A widely used package for replicating Stata’s margins command.
  3. mfx – A package tailored for Generalized Linear Models (glm).

These tools help analyze how small changes in explanatory variables impact the dependent variable.


17.7.2 margins Package

The margins package is a popular choice, designed to replicate Stata’s margins command in R. It provides marginal effects for each variable in a model, including interaction terms. margins define:

  1. Average Partial Effects: The contribution of each variable to the outcome scale, conditional on the other variables involved in the link function transformation of the linear predictor.
  2. Average Marginal Effects: The marginal contribution of each variable on the scale of the linear predictor.
  3. Average marginal effects represent the mean of these unit-specific partial derivatives over a given sample.

Key Features

  • Computes Average Partial Effects (APE).

  • Supports Marginal Effects at the Mean (MEM).

  • Provides visualizations of marginal effects.

Limitation:

  • Slower than marginaleffects, especially with large datasets.
library(margins)

# Fit a linear model with interactions
mod <- lm(mpg ~ cyl * hp + wt, data = mtcars)

# Compute marginal effects
summary(margins(mod))
#>  factor     AME     SE       z      p   lower   upper
#>     cyl  0.0381 0.5999  0.0636 0.9493 -1.1376  1.2139
#>      hp -0.0463 0.0145 -3.1909 0.0014 -0.0748 -0.0179
#>      wt -3.1198 0.6613 -4.7175 0.0000 -4.4160 -1.8236

# Equivalent function for summary
margins_summary(mod)
#>  factor     AME     SE       z      p   lower   upper
#>     cyl  0.0381 0.5999  0.0636 0.9493 -1.1376  1.2139
#>      hp -0.0463 0.0145 -3.1909 0.0014 -0.0748 -0.0179
#>      wt -3.1198 0.6613 -4.7175 0.0000 -4.4160 -1.8236

# Plot marginal effects
plot(margins(mod))

Marginal Effects at Representative Values

# Compute marginal effects when 'hp' = 150
margins(mod, at = list(hp = 150)) %>% summary()
#>  factor       hp     AME     SE       z      p   lower   upper
#>     cyl 150.0000  0.1009 0.6128  0.1647 0.8692 -1.1001  1.3019
#>      hp 150.0000 -0.0463 0.0145 -3.1909 0.0014 -0.0748 -0.0179
#>      wt 150.0000 -3.1198 0.6613 -4.7175 0.0000 -4.4160 -1.8236

17.7.3 mfx Package

The mfx package is specialized for GLMs, computing marginal effects for probit, logit, Poisson, and other count models.

Limitation:

  • Computes only marginal effects for each variable, not the average marginal effect.

Supported Models in mfx

Model Outcome Type Function
Probit Binary probitmfx()
Logit Binary logitmfx()
Poisson Count poissonmfx()
Negative Binomial Count negbinmfx()
Beta Rate betamfx()

Example: Poisson Regression

library(mfx)
data("mtcars")

# Fit a Poisson model and compute marginal effects
poissonmfx(formula = vs ~ mpg * cyl * disp, data = mtcars)
#> Call:
#> poissonmfx(formula = vs ~ mpg * cyl * disp, data = mtcars)
#> 
#> Marginal Effects:
#>                    dF/dx   Std. Err.       z  P>|z|
#> mpg           1.4722e-03  8.7531e-03  0.1682 0.8664
#> cyl           6.6420e-03  3.9263e-02  0.1692 0.8657
#> disp          1.5899e-04  9.4555e-04  0.1681 0.8665
#> mpg:cyl      -3.4698e-04  2.0564e-03 -0.1687 0.8660
#> mpg:disp     -7.6794e-06  4.5545e-05 -0.1686 0.8661
#> cyl:disp     -3.3837e-05  1.9919e-04 -0.1699 0.8651
#> mpg:cyl:disp  1.6812e-06  9.8919e-06  0.1700 0.8650

For more details, see the mfx vignette.

17.7.4 Comparison of Packages

Feature marginaleffects ✅ (Recommended) margins mfx
Computes AME ✅ Yes ✅ Yes ❌ No
Computes MEM ✅ Yes ✅ Yes ❌ No
Computes MER ✅ Yes ✅ Yes ❌ No
Supports GLM Models ✅ Yes ✅ Yes ✅ Yes (but limited to glm)
Works with Interactions ✅ Yes ✅ Yes ❌ No
Fast and Efficient ✅ Yes ❌ No (slower) ✅ Yes (for GLMs)
Supports Counterfactuals ✅ Yes ❌ No ❌ No