Learn R Programming

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

The marginaleffects package for R

Compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 80 classes of statistical models in R. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference.

The marginaleffects website includes a “Get started” tutorial and 25+ vignettes, case studies, and technical notes.

Installation

Install the latest CRAN release:

install.packages("marginaleffects")

Install the development version:

install.packages(
    c("marginaleffects", "insight"),
    repos = c("https://vincentarelbundock.r-universe.dev", "https://easystats.r-universe.dev"))

Restart R completely before moving on.

Citing marginaleffects

Arel-Bundock V (2023). marginaleffects: Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis Tests. R package version 0.9.0, https://vincentarelbundock.github.io/marginaleffects/.

@Manual{marginaleffects,
  title = {marginaleffects: Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis
Tests},
  author = {Vincent Arel-Bundock},
  year = {2023},
  note = {R package version 0.9.0},
  url = {https://vincentarelbundock.github.io/marginaleffects/},
}

Why?

Parameter estimates are often hard to interpret substantively, especially when they are generated by complex models with non-linear components or transformations. Many applied researchers would rather focus on simple quantities of interest, which have straightforward scientific interpretations. Unfortunately, these estimands (and their standard errors) are tedious to compute. Moreover, the different modeling packages in R often produce inconsistent objects that require special treatment.

marginaleffects offers a single point of entry to easily interpret the results of over 80 classes of models, using a simple and consistent user interface.

Benefits of marginaleffects include:

  • Powerful: It can compute predictions, comparisons (contrasts, risk ratios, etc.), slopes, and conduct hypothesis tests for 80 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 website includes 20,000+ words of vignettes and case studies.
  • Efficient: Some operations are orders of magnitude faster than with the margins package, and the memory footprint is much smaller.
  • Thin: Few dependencies.
  • Standards-compliant: marginaleffects follows “tidy” principles and returns objects that work with standard functions like summary(), head(), tidy(), and glance(). These objects are easy to program with and feed to other packages like modelsummary.
  • Valid: When possible, numerical results are checked against alternative software like Stata or other R packages. Unfortunately, it is not possible to test every model type, so users are still strongly encouraged to cross-check their results.
  • 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.

What?

The marginaleffects package allows R users to compute and plot three principal quantities of interest: (1) predictions, (2) comparisons, and (3) slopes. In addition, the package includes a convenience function to compute a fourth estimand, “marginal means”, which is a special case of averaged predictions. marginaleffects can also average (or “marginalize”) unit-level (or “conditional”) estimates of all those quantities, and conduct hypothesis tests on them.

Predictions:

The outcome predicted by a fitted model on a specified scale for a given combination of values of the predictor variables, such as their observed values, their means, or factor levels. a.k.a. Fitted values, adjusted predictions. predictions(), avg_predictions(), plot_predictions().

Comparisons:

Compare the predictions made by a model for different regressor values (e.g., college graduates vs. others): contrasts, differences, risk ratios, odds, etc. comparisons(), avg_comparisons(), plot_comparisons().

Slopes:

Partial derivative of the regression equation with respect to a regressor of interest. a.k.a. Marginal effects, trends. slopes(), avg_slopes(), plot_slopes().

Marginal Means:

Predictions of a model, averaged across a “reference grid” of categorical predictors. marginalmeans().

GoalFunction
Predictionspredictions()
avg_predictions()
plot_predictions()
Comparisonscomparisons()
avg_comparisons()
plot_comparisons()
Slopesslopes()
avg_slopes()
plot_slopes()
Marginal Meansmarginal_means()
Gridsdatagrid()
datagridcf()
Hypothesis & Equivalencehypotheses()
Bayes, Bootstrap, Simulationposterior_draws()
inferences()

Copy Link

Version

Install

install.packages('marginaleffects')

Monthly Downloads

21,335

Version

0.12.0

License

GPL (>= 3)

Maintainer

Vincent Arel-Bundock

Last Published

May 20th, 2023

Functions in marginaleffects (0.12.0)

get_vcov

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

(EXPERIMENTAL) Bootstrap and Simulation-Based Inference
get_varcov_args

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

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

Marginal Means
get_model_matrix

Get a named model matrix
get_predict

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

marginal_means() is an alias to marginal_means()
marginaleffects

marginaleffects() is an alias to slopes()
get_group_names

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

meffects() is an alias to slopes()
plot_cme

plot_slopes() is an alias to plot_slopes()
plot_predictions

Plot Conditional or Marginal Predictions
posterior_draws

Extract Posterior Draws or Bootstrap Resamples from marginaleffects Objects
plot_comparisons

Plot Conditional or Marginal Comparisons
predictions

Predictions
plot_slopes

Plot Conditional or Marginal Slopes
posteriordraws

posteriordraws() is an alias to posterior_draws()
plot_cap

plot_predictions() is an alias to plot_predictions()
plot_cco

plot_comparisons() is an alias to plot_comparisons()
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)
set_coef

Internal function to set coefficients
reexports

Objects exported from other packages
get_coef

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

Data grids
comparisons

Comparisons Between Predictions Made With Different Regressor Values
expect_predictions

tinytest helper
expect_slopes

tinytest helper
expect_marginal_means

tinytest helper
complete_levels

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

tinytest helper
deltamethod

deltamethod() is an alias to hypotheses()
get_averages

Average Estimates (aka "Margins")