Learn R Programming

⚠️There's a newer version (0.25.1) of this package.Take me there.

Parameter estimates are often hard to interpret substantively, especially when they are generated by complex models with non-linear components, interactions, or transformations. Instead of reporting unintuitive parameters, data analysts would rather focus on simple quantities of interest, which have straightforward scientific interpretations. Unfortunately, meaningful estimands—and their standard errors—are often tedious to compute, and the terminology used to describe them varies tremendously across fields.

These problems are compounded by the fact that modeling packages produce objects with very different structures and which hold different information. This means that end-users often have to write customized code to interpret the output of different models. This can lead to wasted effort, confusion, and mistakes, and it can hinder the implementation of best practices.

Marginal Effects Zoo: The Book

This free online book introduces a conceptual framework to clearly define statistical quantities of interest, and shows how to estimate those quantities using the marginaleffects package for R and Python. The techniques introduced herein can enhance the interpretability of over 100 classes of statistical and machine learning models, including linear, GLM, GAM, mixed-effects, bayesian, categorical outcomes, XGBoost, and more. With a single unified interface, users can compute and plot many estimands, including:

  • Predictions (aka fitted values or adjusted predictions)
  • Comparisons such as contrasts, risk differences, risk ratios, odds, etc.
  • Slopes (aka marginal effects or partial derivatives)
  • Marginal means
  • Linear and non-linear hypothesis tests
  • Equivalence tests
  • Uncertainty estimates using the delta method, bootstrapping, simulation, or conformal inference.
  • Much more!

The Marginal Effects Zoo book includes over 30 chapters of tutorials, case studies, and technical notes. It covers a wide range of topics, including how the marginaleffects package can facilitate the analysis of:

  • Experiments
  • Observational data
  • Causal inference with G-Computation
  • Machine learning models
  • Bayesian modeling
  • Multilevel regression with post-stratification (MRP)
  • Missing data
  • Matching
  • Inverse probability weighting
  • Conformal prediction

Get started by clicking here!

marginaleffects: The Package

The marginaleffects package for R and Python offers a single point of entry to easily interpret the results of over 100 classes of models, using a simple and consistent user interface. Its benefits include:

  • Powerful: It can compute and plot predictions; comparisons (contrasts, risk ratios, etc.); slopes; and conduct hypothesis and equivalence tests for over 100 different classes of models in R.
  • Simple: All functions share a simple and unified interface.
  • Documented: Each function is thoroughly documented with abundant examples. The Marginal Effects Zoo website includes 20,000+ words of vignettes and case studies.
  • Efficient: Some operations can be up to 1000 times faster and use 30 times less memory than with the margins package.
  • Valid: When possible, numerical results are checked against alternative software like Stata or other R packages.
  • Thin: The R package requires relatively few dependencies.
  • Standards-compliant: marginaleffects follows “tidy” principles and returns objects that work with standard functions like summary(), tidy(), and glance(). These objects are easy to program with and feed to other packages like ggplot2 or modelsummary.
  • Extensible: Adding support for new models is very easy, often requiring less than 10 lines of new code. Please submit feature requests on Github.
  • Active development: Bugs are fixed promptly.

How to help

The marginaleffects package and the Marginal Effects Zoo book will always be free. If you like this project, you can contribute in four ways:

  1. Make a donation to the Native Women’s Shelter of Montreal or to Give Directly, and send me (Vincent) a quick note. You’ll make my day.
  2. Submit bug reports, documentation improvements, or code contributions to the Github repositories of the R version or the Python version of the package.
  3. Cite the marginaleffects package in your work and tell your friends about it.
  4. Create a new entry for the Meme Gallery!

Copy Link

Version

Install

install.packages('marginaleffects')

Monthly Downloads

21,335

Version

0.17.0

License

GPL (>= 3)

Maintainer

Vincent Arel-Bundock

Last Published

December 9th, 2023

Functions in marginaleffects (0.17.0)

get_model_matrix

Get a named model matrix
inferences

(EXPERIMENTAL) Bootstrap, Conformal, and Simulation-Based Inference
get_group_names

Get levels of the outcome variable in grouped or multivariate models
marginal_means

Marginal Means
marginaleffects

marginaleffects() is an alias to slopes()
get_predict

Get predicted values from a model object (internal function)
get_varcov_args

Take a summary() style vcov argument and convert it to insight::get_varcov()
get_vcov

Get a named variance-covariance matrix from a model object (internal function)
hypotheses

(Non-)Linear Tests for Null Hypotheses, Joint Hypotheses, Equivalence, Non Superiority, and Non Inferiority
marginalmeans

marginal_means() is an alias to marginal_means()
plot_cap

plot_predictions() is an alias to plot_predictions()
plot_comparisons

Plot Conditional or Marginal Comparisons
plot_slopes

Plot Conditional or Marginal Slopes
plot_cco

plot_comparisons() is an alias to plot_comparisons()
posterior_draws

Extract Posterior Draws or Bootstrap Resamples from marginaleffects Objects
plot_predictions

Plot Conditional or Marginal Predictions
predictions

Predictions
meffects

meffects() is an alias to slopes()
plot_cme

plot_slopes() is an alias to plot_slopes()
posteriordraws

posteriordraws() is an alias to posterior_draws()
sanitize_model_specific.glimML

Method to raise model-specific warnings and errors
print.marginaleffects

Print marginaleffects objects
slopes

Slopes (aka Partial derivatives, Marginal Effects, or Trends)
reexports

Objects exported from other packages
set_coef

Internal function to set coefficients
expect_predictions

tinytest helper
datagrid

Data grids
comparisons

Comparisons Between Predictions Made With Different Regressor Values
expect_slopes

tinytest helper
expect_marginal_means

tinytest helper
expect_margins

tinytest helper
deltamethod

deltamethod() is an alias to hypotheses()
get_averages

Average Estimates (aka "Margins")
get_coef

Get a named vector of coefficients from a model object (internal function)
complete_levels

Create a data.frame with all factor or character levels