pymargins.reference

pymargins.reference(variable: str, levels, *, ref_level=None, label_fmt: str | None = None, **fixed)

Build reference-level contrasts: each level vs a common baseline.

Parameters:
  • variable (str) – Variable name.

  • levels (list or tuple) – All levels of the variable. The first element is used as the reference unless ref_level is provided.

  • ref_level (any, optional) – Which level to treat as the reference/baseline. Defaults to the first element of levels.

  • label_fmt (str, optional) – Format string for labels. Defaults to "{var}={val}".

  • **fixed – Additional variables held constant.

Returns:

  • scenarios (list[dict]) – One scenario per level (reference level first).

  • contrasts (dict[str, list[float]]) – Named contrast vectors, one per non-reference level.

Examples

>>> scenarios, contrasts = reference("region", ["north", "south", "east"])
>>> contrasts
{
    "south_vs_north": [ -1, +1,  0],
    "east_vs_north" : [ -1,  0, +1],
}