pymargins.did

pymargins.did(treatment: str, time: str, *, treated_level=1, control_level=0, post_level=1, pre_level=0, label_fmt: str | None = None, **fixed)

Build scenarios and contrast weights for a difference-in-differences design.

The classic 2×2 DID contrast is:

(Treated_post − Treated_pre) − (Control_post − Control_pre)

which corresponds to weights [+1, -1, -1, +1] over the four scenarios ordered as:

  1. Treated_post

  2. Treated_pre

  3. Control_post

  4. Control_pre

Parameters:
  • treatment (str) – Name of the treatment indicator variable.

  • time (str) – Name of the time/post indicator variable.

  • treated_level (any, default 1, 0) – Values of treatment for treated and control units.

  • control_level (any, default 1, 0) – Values of treatment for treated and control units.

  • post_level (any, default 1, 0) – Values of time for post and pre periods.

  • pre_level (any, default 1, 0) – Values of time for post and pre periods.

  • label_fmt (str, optional) – Format string for labels. Defaults to "{treatment}={tval}, {time}={pval}".

  • **fixed – Additional variables held constant.

Returns:

  • scenarios (list[dict])

  • contrasts (dict[str, list[float]]) – Contains a single entry "did" with the contrast weights.

Examples

>>> scenarios, contrasts = did("treat", "post")
>>> m.contrasts(scenarios=scenarios, contrasts=contrasts)