How pymargins differs from marginaleffects and margins¶
This page is for users coming from the R marginaleffects package
or Stata’s margins command. The mathematics is the same — the
implementations converge because the math forces them to. The
differences are in posture.
1. Session pre-commitment¶
marginaleffects and margins let you choose vcov, scale, level,
and aggregation per call. pymargins makes those choices session-
level. The constructor is the methods section; switching any of
them requires a new session.
This is more rigid by design. See The session pre-commitment.
2. The κ fallback¶
Both R and Stata always do the delta method. They do not tell you when the delta method is suspect.
pymargins computes Skovgaard’s relative curvature κ as a routine
diagnostic and auto-falls-back to simulation when κ exceeds the
session threshold. The fallback is loud — every result records the
realized inference method and the reason it was used. See
The κ curvature diagnostic.
3. JAX-native autodiff¶
marginaleffects uses numerical derivatives. pymargins is built
on JAX: exact gradients (and exact Hessians for κ) for any model
whose predict can be expressed in JAX, and a custom-JVP bridge for
models where it cannot. See Gradient backend: autodiff vs wrapped-FD vs FD.
4. Inference scale as a first-class object¶
marginaleffects offers type= (link / response) and a couple of
hard-coded transforms. pymargins takes phi, phi_inv as
arbitrary JAX callables; the same chain-rule machinery handles
log-RR, Fisher-z, lift, or any user-defined scale. See
Inference scale (phi / phi_inv).
5. Narrower scope¶
pymargins does adjusted predictions, slopes, contrasts, and
differentiable compositions. It does not do:
model averaging,
counterfactual prediction with model re-solving,
post-estimation likelihood transformations.
If you need those, marginaleffects has broader coverage and a
gentler learning curve, and it is the right tool.
API mapping for R users¶
If you are coming from marginaleffects, the conceptual mapping is:
|
|
Notes |
|---|---|---|
|
|
Adjusted predictions (AAP / APM / APR) |
|
|
Marginal effects (AME / MEM / MER) |
|
|
Linear contrasts with joint covariance |
|
|
AAP is the default |
|
|
AME is the default |
|
|
|
|
|
Nonlinear compositions |
|
|
|
|
|
CI endpoints are back-transformed automatically |
When to pick which¶
Need |
Tool |
|---|---|
Broad model coverage, fast iteration |
|
Audit trail and pre-registration |
|
Curvature-aware fallback |
|
Custom inference scales |
|
Stata-style replications |
both (numerically agreed) |