Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis Tests
Overview
Compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and machine learning 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. Details can be found in Arel-Bundock, Greifer, and Heiss (2024) doi:10.18637/jss.v111.i09.
Breaking changes:
- The
byfunargument is no longer supported. Usehypothesisfor custom aggregations. - Removed
autodiff(), JAX differentiation, and thereticulatedependency. Remove calls toautodiff(). The new analytic Jacobians are faster and have broader coverage (see Performance below).
New features:
- New
vcov = "unconditional"standard errors for average predictions, comparisons, and slopes account for sampling variation in the covariate distribution.vcovUnconditional()supports HC0 (default), HC1, one-way clustering, built-in contrasts, weighted targets,lm,glm,survey::svyglm(), and validatedfixest::feols()cases. Unsupported multiple-imputation,hypotheses(), fixed-effectfixest, censored, and survival cases produce informative errors and recommend bootstrap inference where appropriate.vcov = vcovUnconditionalis equivalent tovcov = vcovUnconditional(). Thanks to @pedrohcgs for the code contribution in #1737; @snhansen for theoretical guidance and independent testing; and @ngreifer and @grantmcdermott for discussion in #1240 and #1737. - New
set_modeldata()attaches training data to model objects.get_modeldata()uses it first and warns once per session about retrieval problems frominsight::get_data(). - New
components(x, "jacobian_method")reports the differentiation path.
New models:
- Added support for
svyVGAM::svy_vglm()(#1730, @kkranker),glmtoolbox::glmgee()andglmtoolbox::gnm()(#1148, @luifrancgom),nestedLogit::nestedLogit()(#1675, @strengejacke),mmrm::mmrm()(#1000, @kkmann, @lang-benjamin, and @danielinteractive), andordinal::clmm2()(#1003). - Improved the
typeerror foraftmodels.
Performance:
- Eligible estimands now use exact analytic Jacobians for
lm,glm,MASS::glm.nb(),MASS::rlm(),brglm2::brglmFit(),quantreg::rq(),ivreg,geepack::geeglm(),survey::svyglm(),rms::ols(), and binaryrms::lrm()models. Newly exact standard errors may differ from previous builds by about 1e-7.
Show all changes in 1.0.0Hide the rest of the changes in 1.0.0
- Many
comparisons()andslopes()calls are at least three times faster on large datasets. - Built-in ratios, lifts, slopes, and elasticities now use exact derivatives.
- Linear
hypothesis=expressions and supported formula shortcuts compile to contrast matrices, and post-hochypotheses()uses the same exact path for linear expressions, including constants. Nonlinear hypotheses compose with upstream Jacobians instead of re-differentiating the full pipeline. Issue #1735. Thanks to @nremenyi for #1750. - Bayesian posterior draws processing is much more efficient.
- Set
options(marginaleffects_analytic_jacobian = FALSE)to disable analytic Jacobians and force the numerical fallback, which is useful for validation and debugging.
Covariance and inference:
- User-supplied covariance matrices must cover all coefficients. Named matrices are reordered and validated; unnamed matrices warn about positional matching. Errors from
vcovfunctions are now propagated. vcov = "stata"now maps to HC1, matching Stata,estimatr, andmodelsummary; usevcov = "HC2"for the previous behavior.multcomp=and simulated Wald inference now respect the hypothesis null and direction. Bonferroni confidence intervals now match Bonferroni p values.- Exact zero standard errors are reported as
NA, along with their associated inferential quantities. This distinguishes structural or constant estimands from effects estimated with very small uncertainty. - Per-observation
dfvectors are rejected with aggregation or hypotheses;df = NULLremains valid. Equivalence tests support vector-valued degrees of freedom and reject reversed bounds. - Weighted average comparisons no longer return
NaNwhen a group's first weight is zero; groups with zero total weight remain undefined.
Hypotheses and contrasts:
- Numeric
variables=functions and data frames honorlo/hiandlow/highregardless of order and may return data frames, data tables, or tibbles. Logical comparisons honor their requested direction. hypothesisformulas group bydatagrid()variables whenbyis omitted and can resolve external variables such as~ I(drop(t(www) %*% x)) | am. Thanks to Matt Thomas for #1705 and @mattansb for #1707.- Hypothesis functions may declare
newdataandby; empty results now error. Joint tests prefer exact name matches, reject ambiguous or missing matches, and report null values correctly. Matrix hypotheses preserve posterior and bootstrap draws. ~meandevand~meanotherdevnow useO(np)time and memory instead of densen x nmatrices, reducing memory use from 573 MB for 10,000 estimates.numderiv = list("richardson", side = ...)is now accepted; the validator previously rejectedsideand accepted the nonexistentsizeargument.
Data, grids, and grouping:
- Grouped comparisons and slopes support
by = "rowid", avoid collisions with data columns namedpredicted,term, orcontrast, preserve posterior and bootstrap draws when groups are incomplete, and retaincomparison = "lift"rows with duplicate labels. Thanks to @coughlanja for #1669. - Formula terms are parsed to recognize
factor(x, levels = ...),as.numeric(x), andas.logical(x), preventing invaliddatagrid()values andNApredictions. datagrid()now preserves counterfactual row identifiers across groups, supports groupeddata.tableinputs, honorsresponse, validates grouping columns, and treats numericfixestfixed effects as categorical. Thanks to @arcruz0 for #1685.newdatashortcuts now handle logical, integer, binary, and missing values as documented. Existingrowidcolumns are no longer modified by reference.avg_predictions()with factorvariablesuses all factor levels when the original model data is unavailable. Thanks to @trose64 for report #1703.
Model compatibility:
- Corrected standard errors for
survival::survreg()models with estimated scale, namedsystemfit()equations,mhurdlescale parameters, and zero coefficients innnet::multinom(). - Repeated standard-error calculations for
nls()models are now reproducible. betaregmodels without a precision formula now align the precision parameter names used byget_coef()andget_vcov(), allowing user-supplied covariance matrices.lmandglmestimates work whenqr$qrwas removed from the fitted object; linear predictions fall back to the model matrix whenstats::predict()fails. Thanks to @trose64 for report #1748.vcov = "HC0"no longer changes response-scale point estimates forglmmTMBmodels with random effects; robust covariance extraction now restores the internal TMB state. Thanks to @nremenyi for the report.- Fixed
miramodels fitted with formula transformations andsubset=, and enabledpredictions(<mira>, newdata = datagrid(...))formiceobjects. Thanks to @dmongin for #1682. flexsurvprediction standard errors now synchronize transformed coefficients and delegate toflexsurv::predict()whenvcov = TRUE. Thanks to @colinorourke for report #1670.hypotheses()no longer errors onsurvey::svyglm()objects when no averaging is required. Thanks to @bdwilliamson for Issue #1723.
Bootstrap and conformal inference:
- Bootstrap and
refit()reject models fitted with external weight vectors. Bootstrap refits no longer resolve unrelated global data, and fractional weighted bootstrap uses model-stored data. Thanks to @U8NWXD for #1713. - Bootstrap inference with a user-supplied
estimatoruses theconf_levelfrom the estimator result. - Conformal intervals now use the correct finite-sample quantile, warn and return infinite bounds when calibration data are insufficient, respect transformed outcomes, and preserve absolute residuals for squared-residual CV+ scores.
Predictions and comparisons:
- Comparison rows are now dropped when either prediction is missing.
transform=handlesNaNconfidence bounds and reports wrong-length function results clearly.
Plots, printing, and diagnostics:
- Plot functions avoid duplicate lines, handle missing response labels and duplicated rounded condition values, and support
points=andrug=withcbind()responses. - Printing handles
NAvalues indf.print(style = "tinytable")preservesTerm:andComparison:notes when their columns are dropped and bases that decision on the full object rather than the displayed rows. - Fixed diagnostics and validation in
expect_margins()andexpect_slopes(). - Models with
NAcoefficients now trigger a once-per-session rank-deficiency warning. Thanks to @andymilne for #1744.
From NEWS.md in the release
Install
Health
- OK13 OK2026-08-05
- NOTE12 OK, 1 NOTE2026-08-01
- OK12 OK2026-04-25
- NOTE13 OK, 1 NOTE2026-03-10
Documentation
- Help pages
- 36 (19 internal)
- Examples that run
- 82%9 run in a plain check (6 of them under a condition) · 2 never run
- Exported functions with examples
- 37%
- Documented parameters
- 73%
- Return-value docs
- 67%
- Help pages citing references
- 8%
- Model to Meaning HTML
Downloads
Repository
- PRs merged (all time)
- 84%
- Median days to close, 50 most recently updated closed issues
- 5
- Median days to close, 50 most recently updated closed PRs
- 1
Stars over time
Forks over time
Releases over time
Issues over time
PRs over time
Commits over time
Commit identities over time
6 development-tooling and community-health practices detected across 5 families in the upstream repository
Checks run against github.com/vincentarelbundock/marginaleffects on 2026-10-22.
The R CMD check workflow runs on Linux, macOS and Windows
GitHub Pages site last deployed 2025-05-19
Show all practicesHide 1 practice
This repository shows signs of AI coding tools.
- Claude Codefirst seen 2026-03-10Pull request it wrote: opened by a project memberCommits: at least 9 crediting it in the message
- Claude Opus 56 commits · Aug to Sep 2026
- Claude Fable 51 commit · Aug 2026
- Claude Fable 5.11 commit · Oct 2026
- Claude Opus 4.61 commit · Mar 2026
Dependencies
Installing marginaleffects adds 7 packages (4 with compiled code, 8.0 MB of source tarballs). Heaviest: data.table 5.7 MB, insight 1.2 MB, rlang 766 KB.
CRAN will archive these unless their check problems are fixed by the date shown. This package needs each one to install.
- insightfix by Nov 14
Show the 48 most downloaded packagesHide the 48 most downloaded packages
Code & Tests
People & History
Pick two releases to compare their code metrics.
- 1.0.0Latest2026-09-03 · current release · diff ↗ ·
removed 1 export
autodiffadded 3 exports
get_modeldata, set_modeldata, vcovUnconditional - RR 4.6.0 released · 2026-04-24
- 0.32.02026-02-14 · diff ↗
- 0.31.0
- 0.30.0
- 0.29.0
- 0.28.02025-06-25 · diff ↗
- 0.27.02025-06-04 · diff ↗
- 0.26.02025-05-19 · diff ↗
- RR 4.5.0 released · 2025-04-11
- 0.25.12025-03-29 · diff ↗
- 0.25.02025-02-01 · diff ↗ ·
removed 6 exports
datagridcf, deltamethod, marginal_means, marginaleffects, marginalmeans, meffectsadded 1 export
get_dataset - 0.24.0
- 0.23.02024-10-05 · diff ↗
- 0.22.02024-08-31 · diff ↗
- 0.21.02024-06-14 · diff ↗
Show 37 earlier eventsHide 37 earlier events
- 0.20.12024-05-08 · diff ↗
- 0.20.0
- RR 4.4.0 released · 2024-04-24
- 0.19.02024-04-14 · diff ↗
- 0.18.0
- 0.17.02023-12-09 · diff ↗
- 0.16.02023-10-20 · diff ↗
- 0.15.12023-09-17 · diff ↗
- 0.15.02023-09-11 · diff ↗
- 0.14.02023-08-15 · diff ↗
- 0.13.02023-06-19 · diff ↗
- 0.12.02023-05-20 · diff ↗
- 0.11.22023-05-13 · diff ↗
- RR 4.3.0 released · 2023-04-21
- 0.11.12023-03-31 · diff ↗
- 0.11.0
- 0.10.02023-02-22 · diff ↗
- 0.9.02023-02-01 · diff ↗ ·
removed 3 exports
counterfactual, mean_or_mode, typicaladded 15 exports
avg_comparisons, avg_predictions, avg_slopes, expect_marginal_means, expect_margins, expect_predictions, expect_slopes, hypotheses, inferences, marginal_means, plot_comparisons, plot_predictions, plot_slopes, posterior_draws, slopes - 0.8.12022-11-23 · diff ↗
- 0.8.02022-11-02 · diff ↗
- 0.7.12022-09-25 · diff ↗
- 0.7.0
- 0.6.0
- unarchivedReturned to CRAN2022-05-17
- 0.5.02022-05-17 · diff ↗
- archivedRemoved from CRAN2022-05-15check issues were not corrected in time
- RR 4.2.0 released · 2022-04-22
- 0.4.12022-03-28 · diff ↗
- 0.4.0
- 0.3.42022-03-03 · diff ↗
- 0.3.32022-01-26 · diff ↗
- 0.3.22022-01-18 · diff ↗
- 0.3.12022-01-09 · diff ↗
- 0.3.0
- 0.2.0
- 0.1.02021-09-29
- RR 4.1.0 released · 2021-05-18
Package metadata
- First published
- 2021-09-29
- Total releases
- 45 / 5 yrs
- License
- GPL (>= 3) OSI
- Copyright
- inst/COPYRIGHTS
- Minimum R
- ≥ 3.6.0
- Download size
- 1.6 MB
Cite
Cite this package
Run in R for the authors' entry:
citation("marginaleffects")The package provides a citation file, so citation() returns the authors' own entry.
It points to 10.18637/jss.v111.i09.
Cite the R Observatory
For a number measured here: a download total, a coverage figure, an archival date.
From data release v2026-10-25, which the citation names so these numbers can be found later. More on citing and the projects behind them.