Learn R Programming

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

The parameters of a statistical model can sometimes be difficult to interpret substantively, especially when that model includes non-linear components, interactions, or transformations. Analysts who fit such complex models often seek to transform raw parameter estimates into quantities that are easier for domain experts and stakeholders to understand, such as predictions, contrasts, risk differences, ratios, odds, lift, slopes, and so on.

Unfortunately, computing these quantities—along with associated standard errors—can be a tedious and error-prone task. This problem is compounded by the fact that modeling packages in R and Python produce objects with varied structures, which hold different information. This means that end-users often have to write customized code to interpret the estimates obtained by fitting Linear, GLM, GAM, Bayesian, Mixed Effects, and other model types. This can lead to wasted effort, confusion, and mistakes, and it can hinder the implementation of best practices.

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!

Article

Our article on marginaleffects is provisionally accepted for publication by the Journal of Statistical Software. You can read the preprint here.

To cite marginaleffects in publications please use:

Arel-Bundock V, Greifer N, Heiss A (Forthcoming). “How to Interpret Statistical Models Using marginaleffects in R and Python.” Journal of Statistical Software.

A BibTeX entry for LaTeX users is:

@Article{,
    title = {How to Interpret Statistical Models Using {marginaleffects} in {R} and {Python}},
    author = {Vincent Arel-Bundock and Noah Greifer and Andrew Heiss},
    year = {Forthcoming},
    journal = {Journal of Statistical Software},
}

Software

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 simple data frames that work with all standard R functions. The outputs 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.

Copy Link

Version

Install

install.packages('marginaleffects')

Monthly Downloads

21,335

Version

0.23.0

License

GPL (>= 3)

Maintainer

Vincent Arel-Bundock

Last Published

October 5th, 2024

Functions in marginaleffects (0.23.0)

marginaleffects

Deprecated function
hypotheses

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

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

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

Deprecated function
marginal_means

Deprecated function
get_model_matrix

Get a named model matrix
get_varcov_args

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

Get predicted values from a model object (internal function)
knit_print.marginaleffects

Print a marginaleffects object in knitr
plot_comparisons

Plot Conditional or Marginal Comparisons
reexports

Objects exported from other packages
predictions

Predictions
print.marginaleffects

Print marginaleffects objects
plot_predictions

Plot Conditional or Marginal Predictions
posterior_draws

Extract Posterior Draws or Bootstrap Resamples from marginaleffects Objects
meffects

Deprecated function
sanitize_model_specific.glimML

Method to raise model-specific warnings and errors
posteriordraws

posteriordraws() is an alias to posterior_draws()
plot_slopes

Plot Conditional or Marginal Slopes
specify_hypothesis

(EXPERIMENTAL) This experimental function will soon be deprecated. Please supply a formula or function to the hypothesis argument to conduct (group-wise) hypothesis tests.
set_coef

Internal function to set coefficients
slopes

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

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

Comparisons Between Predictions Made With Different Regressor Values
datagrid

Data grids
get_group_names

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

tinytest helper
expect_predictions

tinytest helper
datagridcf

Deprecated function
complete_levels

Create a data.frame with all factor or character levels
expect_slopes

tinytest helper
deltamethod

Deprecated function