API reference¶
pymargins — expert-mode marginal effects for Python.
Quick reference¶
Wrap a fitted model in a Margins session, declare your analytical posture (scale, vcov, level, at), then compute predictions, slopes, and contrasts via the session’s methods:
from pymargins import Margins
m = Margins.log_scale(fitted_glm, vcov="HC3")
rr = m.contrasts(
scenarios=[
{"atexog": {"treatment": "treated"}},
{"atexog": {"treatment": "control"}},
],
contrasts=[+1, -1],
)
Public API¶
Margins— main session classMarginsResult— output of predict/dydx/contrasts/evaluateTestResult— output of result.test() and result.joint_test()DiagnosticResult— output of m.diagnose()VariableInfo— per-variable metadata used by adaptersregister_adapter— register a custom adapter for auto-detection
Internal modules (prefixed with _) hold the numerical kernels and engine; end users should not import from these directly.
Adapter authors writing a custom adapter subclass one of the base shapes
re-exported above (ModelAdapter, GLMAdapter, etc.) plus the
make_*_jvp_wrapper gradient helpers, and register it with
register_adapter. The concrete built-in adapters are available, lazily,
from the public pymargins.adapters module — you rarely need them since
Margins(model) auto-detects the right one; import explicitly only to
override detection or select a non-default scale.
Session¶
|
Wrapper around a fitted model exposing marginal-effects analysis. |
Results¶
|
Container for marginal-effects estimates with inference and diagnostics. |
|
Output of a hypothesis test on a MarginsResult. |
|
Output of session-level kappa diagnostic. |
Scenario helpers¶
|
Build two scenarios and a [+1, -1] contrast for a pairwise comparison. |
|
Build reference-level contrasts: each level vs a common baseline. |
|
Build one scenario per level of a categorical/binary variable. |
|
Cartesian product of variable values into a scenario list. |
|
Build scenarios and contrast weights for a difference-in-differences design. |
|
Return a standard pairwise difference contrast vector. |
|
Build all scenario combinations for a factorial design and return named pairwise contrasts for each stratum. |
Adapter interface¶
Abstract base class for model adapters. |
|
Base adapter for any model with prediction μ = g⁻¹(Xβ + offset). |
|
Base for models whose prediction is exactly Xβ (no link function). |
|
Base for models with smooth predict but no exposed analytical derivative. |
|
Base for non-parametric / algorithmic models with no meaningful Σ̂. |
|
|
Per-variable metadata used for at=typical averaging, contrast validation, and warning generation. |
|
Register an adapter class for auto-detection. |
Concrete adapters¶
You rarely need these — Margins(model) auto-detects the right adapter
for standard statsmodels / linearmodels / lifelines results. Import a
concrete adapter from pymargins.adapters only when you need a
non-default scale that shares a result class with another adapter (e.g.
LifelinesCoxPHSurvivalAdapter for survival probability vs the
auto-detected LifelinesCoxPHAdapter for hazard ratio), when wrapping a
framework with no native inference (SklearnBootstrapAdapter), or when
constructing an adapter explicitly to override auto-detection or pass extra
arguments such as training_data=.
pymargins.adapters — public access to concrete model adapters.
You rarely need anything here. Margins(model) auto-detects the right
adapter for standard statsmodels / linearmodels / lifelines results. Import
a concrete adapter only when:
You need a non-default scale that shares a result class with another adapter and therefore cannot be auto-detected — e.g.
LifelinesCoxPHSurvivalAdapter(survival probability) vs the auto-detectedLifelinesCoxPHAdapter(hazard ratio).You are wrapping a framework with no native inference, such as scikit-learn via
SklearnBootstrapAdapter.You want to construct an adapter explicitly to pass extra arguments (e.g.
training_data=) or to override auto-detection.
To write a custom adapter, subclass one of the base shapes exported from
the top-level package (ModelAdapter, GLMAdapter,
LinearPredictionAdapter, WrappedFDAdapter, BootstrapOnlyAdapter)
and register it with pymargins.register_adapter(). See
docs/howto/custom_adapter.md.
Imports here are lazy (PEP 562): referencing an adapter triggers the import
of its module and the corresponding optional third-party dependency, so
import pymargins stays cheap and does not require statsmodels,
lifelines, linearmodels, or scikit-learn to be installed.
Matching¶
|
Wrap a fitted |