Causal inference & treatment effects¶
The classes and functions of the row "Causal inference & treatment effects" of Analytical tools, with what calls return (the results and their methods: forecasts, impulse responses, tests). Import them from pymetrix (from pymetrix import ...) unless a heading names another module. Parameters and return types are in each signature; the text says what each does.
Help topics: Treatment effects and policy evaluation, Synthetic difference-in-differences, Staggered difference-in-differences, Shift-share (Bartik) instruments.
Functions¶
bacon_decomposition
¶
bacon_decomposition(data: Dataset, outcome: str, treatment: str) -> BaconDecomposition
Goodman-Bacon's decomposition of the two-way fixed effects DiD estimate into 2x2 comparisons and their weights.
did
¶
Difference-in-differences: outcome on the 0/1 treatment (1 where the policy is in force) with unit and
period fixed effects, errors clustered by unit. The group means and the trend chart use the regression's rows
(the active sample, less rows with missing values).
event_study
¶
event_study(data: Dataset, outcome: str, treatment: str, window: tuple[int, int] = (-5, 5), reference: int = -1, controls: Sequence[str] = ()) -> EventStudy
Effects by period relative to each unit's first treated period, window (first, last) periods around it,
relative to reference; periods beyond the window are gathered at its ends. Never-treated units are the
comparison.
rdd
¶
rdd(data: Dataset, outcome: str, running: str, cutoff: float = 0.0, bandwidth: float | None = None) -> RDD
A sharp regression discontinuity: the jump in outcome where running reaches cutoff, by local linear
regression with a triangular kernel; bandwidth by cross-validation unless given.
shift_share
¶
shift_share(data: Dataset, outcome: str, treatment: str, shares: Sequence[str], shocks: Any, controls: Sequence[str] = (), weights: str | None = None, shock_clusters: Mapping[str, Any] | None = None, se: Literal['akm', 'bhj', 'robust', 'cluster'] = 'akm', alpha: float = 0.05, region_clusters: str | None = None) -> FittedShiftShare
Shift-share IV: outcome on the endogenous treatment, instrumented by sum_k share_k shock_k, with a
constant and controls. shares are the series of exposure shares (one per industry), shocks one per
share (a mapping from share name, or a list in the same order); weights a series of regression weights;
shock_clusters maps each share to a cluster of industries for AKM and BHJ errors; se chooses the
standard error the coefficient carries; region_clusters a series grouping the regions (states, commuting
zones) for errors clustered across regions beside the shock-level ones (se="cluster" to carry them).
staggered_did
¶
staggered_did(data: Dataset, outcome: str, treatment: str, method: Literal['callaway_santanna', 'sun_abraham'] = 'callaway_santanna', control: Literal['never', 'notyet'] = 'never', window: tuple[int, int] | None = None, draws: int = 499, seed: int = 0, controls: Any = (), estimator: Literal['dr', 'or', 'ipw'] = 'dr') -> StaggeredDiD
Effects of a staggered treatment by event time, cohort and overall: Callaway and Sant'Anna's group-time
effects, or Sun and Abraham's interaction-weighted event study (see the module notes). controls (each unit's
values in the first period) make parallel trends conditional on them, by estimator: doubly robust, outcome
regression or inverse probability weighting. window=(first, last) limits the event times shown; the
estimates (and the cohorts' and overall effects) are always made on the full specification. Sun and Abraham's
estimator compares with the never-treated units, or with none the last-treated cohort, and refuses
control="notyet"; the result's control records the group used.
synthetic_control
¶
synthetic_control(data: Dataset, outcome: str, treated: Any, start: Any, donors: Sequence[Any] | None = None) -> SyntheticControl
The effect on unit treated from period start: its outcome against a weighted average of the
donors (default: every other unit), weighted to match its path before start.
synthetic_did
¶
synthetic_did(data: Dataset, outcome: str, treatment: str, se: Literal['auto', 'jackknife', 'placebo'] = 'auto', placebos: int = 200, seed: int = 0) -> SyntheticDiD
Synthetic difference-in-differences of outcome for the units treatment turns on, all at the same period
and for good (a block design); a balanced panel.
treatment_effect
¶
treatment_effect(data: Dataset, outcome: str, treatment: str, controls: Sequence[str], estimand: Literal['ATE', 'ATT'] = 'ATE', method: Literal['ipw', 'aipw'] = 'ipw', draws: int = 200, seed: int = 0) -> TreatmentEffect
The average effect of the 0/1 treatment on outcome (ATE, or on the treated, ATT), assuming selection
on the controls: inverse probability weighting with normalised weights, or method="aipw" (doubly
robust: also a regression of the outcome on the covariates in each group). Bootstrap standard errors.
Results¶
BaconDecomposition
dataclass
¶
BaconDecomposition(outcome: str, treatment: str, twfe: float, comparisons: DataFrame, provenance: Any = None)
The two-way fixed effects estimate as a weighted average of its 2x2 comparisons (Goodman-Bacon 2021).
DiD
dataclass
¶
DiD(fit: Any, outcome: str, treatment: str, means: DataFrame | None, by_time: DataFrame | None = None, provenance: Any = None, two_by_two: bool = False)
A difference-in-differences estimate by two-way fixed effects.
EventStudy
dataclass
¶
EventStudy(fit: Any, outcome: str, relative: tuple[int, ...], reference: int, provenance: Any = None)
Effects by period relative to adoption, from a two-way fixed effects regression on leads and lags.
FittedShiftShare
dataclass
¶
FittedShiftShare(outcome: str, treatment: str, controls: tuple[str, ...], industries: tuple[str, ...], beta: float, se_robust: float, se_akm: float, se_bhj: float, akm0_interval: tuple[float, float], first_stage: float, first_stage_f: float, se: str, shocks: Series, instrument: Series, shares: DataFrame, exposure: Series, rotemberg_weights: DataFrame, shock_frame: DataFrame, nobs: int = 0, shock_clusters: int | None = None, alpha: float = 0.05, provenance: Any = None, se_cluster: float | None = None, region_clusters: int | None = None)
A shift-share IV estimate, with standard errors robust to shock-level correlation and the instrument's pieces.
RDD
dataclass
¶
RDD(outcome: str, running: str, cutoff: float, bandwidth: float, estimate: float, se: float, n_left: int, n_right: int, sensitivity: DataFrame, x: ndarray, y: ndarray, chosen_by: str = '', provenance: Any = None)
A sharp regression discontinuity: the jump in the outcome at the cutoff, by local linear regression.
StaggeredDiD
dataclass
¶
StaggeredDiD(outcome: str, treatment: str, method: str, control: str, periods: list[Any], event: DataFrame, cohorts: DataFrame, att: DataFrame, total: tuple[float, float] = (nan, nan), units: int = 0, provenance: Any = None, already_treated: int = 0)
Group-time or interaction-weighted effects of a staggered treatment, by event time, cohort and overall.
SyntheticControl
dataclass
¶
SyntheticControl(outcome: str, treated: Any, start: Any, weights: Series, actual: Series, synthetic: Series, placebo_gaps: DataFrame, ratio: float, p_value: float, provenance: Any = None)
A synthetic control: the treated unit against a weighted combination of untreated units (Abadie, Diamond and Hainmueller 2010), with in-space placebos.
SyntheticDiD
dataclass
¶
SyntheticDiD(outcome: str, att: float, se: float, did: float, sc: float, unit_weights: Series, time_weights: Series, se_method: str, treated_units: tuple[Any, ...], start: Any, zeta: float, paths: DataFrame, provenance: Any = None)
A synthetic difference-in-differences estimate, with DiD and the synthetic control beside it.
plot
¶
The treated units' average against the weighted controls (shifted by the pre-period gap), with the start marked.
TreatmentEffect
dataclass
¶
TreatmentEffect(outcome: str, treatment: str, controls: tuple[str, ...], estimand: str, method: str, estimate: float, se: float, propensity: Series, draws: int = 0, treated: Series | None = None, provenance: Any = None)
An average treatment effect under selection on observables, by inverse probability weighting or doubly robust (augmented) weighting.