diff_diff.MultiPeriodDiD
- class diff_diff.MultiPeriodDiD[source]
Bases:
DifferenceInDifferencesMulti-Period Difference-in-Differences estimator.
Extends the standard DiD to handle multiple pre-treatment and post-treatment time periods, providing period-specific treatment effects as well as an aggregate average treatment effect.
- Parameters:
- results_
Estimation results after calling fit().
- Type:
Examples
Basic usage with multiple time periods:
>>> import pandas as pd >>> from diff_diff import MultiPeriodDiD >>> >>> # Create sample panel data with 6 time periods >>> # Periods 0-2 are pre-treatment, periods 3-5 are post-treatment >>> data = create_panel_data() # Your data >>> >>> # Fit the model >>> did = MultiPeriodDiD() >>> results = did.fit( ... data, ... outcome='sales', ... treatment='treated', ... time='period', ... post_periods=[3, 4, 5] # Specify which periods are post-treatment ... ) >>> >>> # View period-specific effects >>> for period, effect in results.period_effects.items(): ... print(f"Period {period}: {effect.effect:.3f} (SE: {effect.se:.3f})") >>> >>> # View average treatment effect >>> print(f"Average ATT: {results.avg_att:.3f}")
Notes
The model estimates:
Y_it = α + β*D_i + Σ_t γ_t*Period_t + Σ_{t≠ref} δ_t*(D_i × 1{t}) + ε_it
Where: - D_i is the treatment indicator - Period_t are time period dummies (all non-reference periods) - D_i × 1{t} are treatment-by-period interactions (all non-reference) - δ_t are the period-specific treatment effects - The reference period (default: last pre-period) has δ_ref = 0 by construction
Pre-treatment δ_t test the parallel trends assumption (should be ≈ 0). Post-treatment δ_t estimate dynamic treatment effects. The average ATT is computed from post-treatment δ_t only.
- __init__(robust=True, cluster=None, alpha=0.05, inference='analytical', n_bootstrap=999, bootstrap_weights='rademacher', seed=None, rank_deficient_action='warn')
Methods
__init__([robust, cluster, alpha, ...])fit(data, outcome, treatment, time[, ...])Fit the Multi-Period Difference-in-Differences model.
Get estimator parameters (sklearn-compatible).
predict(data)Predict outcomes using fitted model.
Print summary to stdout.
set_params(**params)Set estimator parameters (sklearn-compatible).
summary()Get summary of estimation results.
- fit(data, outcome, treatment, time, post_periods=None, covariates=None, fixed_effects=None, absorb=None, reference_period=None, unit=None)[source]
Fit the Multi-Period Difference-in-Differences model.
- Parameters:
data (pd.DataFrame) – DataFrame containing the outcome, treatment, and time variables.
outcome (str) – Name of the outcome variable column.
treatment (str) – Name of the treatment group indicator column (0/1). Should be a time-invariant ever-treated indicator (D_i = 1 for all periods of treated units). If treatment is time-varying (D_it), pre-period interaction coefficients will be unidentified.
time (str) – Name of the time period column (can have multiple values).
post_periods (list) – List of time period values that are post-treatment. All other periods are treated as pre-treatment.
covariates (list, optional) – List of covariate column names to include as linear controls.
fixed_effects (list, optional) – List of categorical column names to include as fixed effects.
absorb (list, optional) – List of categorical column names for high-dimensional fixed effects.
reference_period (any, optional) – The reference (omitted) time period for the period dummies. Defaults to the last pre-treatment period (e=-1 convention).
unit (str, optional) – Name of the unit identifier column. When provided, checks whether treatment timing varies across units and warns if staggered adoption is detected (suggests CallawaySantAnna instead). Does NOT affect standard error computation – use the
clusterparameter for cluster-robust SEs.
- Returns:
Object containing period-specific and average treatment effects.
- Return type:
- Raises:
ValueError – If required parameters are missing or data validation fails.