pymargins.pairwise

pymargins.pairwise(variable: str, values, *, label_fmt: str | None = None, **fixed)

Build two scenarios and a [+1, -1] contrast for a pairwise comparison.

Parameters:
  • variable (str) – Variable name to contrast.

  • values (list or tuple of length 2) – The two levels to compare (e.g. [1, 0] or ["treated", "control"]).

  • label_fmt (str, optional) – Format string for auto-generated labels. If None, defaults to "{var}={val}".

  • **fixed – Additional variables held constant across both scenarios (e.g. female=0 for MER-style contrasts).

Returns:

  • scenarios (list[dict])

  • contrasts (list[float])

Examples

>>> scenarios, contrasts = pairwise("treatment", [1, 0])
>>> scenarios
[
    {"atexog": {"treatment": 1}, "label": "treatment=1"},
    {"atexog": {"treatment": 0}, "label": "treatment=0"},
]
>>> contrasts
[1, -1]
>>> # With fixed covariates (MER)
>>> scenarios, contrasts = pairwise("black", [1, 0], female=0, age=40)