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:Treated_post
Treated_pre
Control_post
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
treatmentfor treated and control units.control_level (any, default 1, 0) – Values of
treatmentfor treated and control units.post_level (any, default 1, 0) – Values of
timefor post and pre periods.pre_level (any, default 1, 0) – Values of
timefor 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)