pymargins.all_pairwise

pymargins.all_pairwise(variables, values_list, *, label_fmt: str | None = None, **fixed)

Build all scenario combinations for a factorial design and return named pairwise contrasts for each stratum.

For a single variable you may pass the name directly and the levels as a flat list (all_pairwise("x", [0, 1])); for multiple variables pass lists (all_pairwise(["x", "y"], [[0, 1], ["A", "B"]])).

Parameters:
  • variables (list[str]) – Variable names (e.g. ["treatment", "sex"]).

  • values_list (list[list]) – Levels for each variable (e.g. [[1, 0], ["M", "F"]]).

  • label_fmt (str, optional) – Format string for labels. Defaults to comma-joined "var=val".

  • **fixed – Additional variables held constant.

Returns:

  • scenarios (list[dict])

  • contrasts (dict[str, list[float]]) – Named pairwise contrasts, one per non-baseline combination. The first combination (first level of each variable) is the reference.

Examples

>>> scenarios, contrasts = all_pairwise(
...     ["treatment", "sex"],
...     [[1, 0], ["M", "F"]],
... )
>>> len(scenarios)
4
>>> contrasts
{
    "treatment=0, sex=M_vs_treatment=1, sex=M": [...],
    ...
}