You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When treatment is adopted at different times by different units, traditional TWFE estimators can be biased. The Callaway-Sant'Anna estimator provides unbiased estimates with staggered adoption.
569
+
570
+
```python
571
+
from diff_diff import CallawaySantAnna
572
+
573
+
# Panel data with staggered treatment
574
+
# 'first_treat' = period when unit was first treated (0 if never treated)
575
+
cs = CallawaySantAnna()
576
+
results = cs.fit(
577
+
panel_data,
578
+
outcome='sales',
579
+
unit='firm_id',
580
+
time='year',
581
+
first_treat='first_treat', # 0 for never-treated, else first treatment year
582
+
aggregate='event_study'# Compute event study effects
583
+
)
584
+
585
+
# View results
586
+
results.print_summary()
587
+
588
+
# Access group-time effects ATT(g,t)
589
+
for (group, time), effect in results.group_time_effects.items():
590
+
print(f"Cohort {group}, Period {time}: {effect['effect']:.3f}")
591
+
592
+
# Event study effects (averaged by relative time)
593
+
for rel_time, effect in results.event_study_effects.items():
plot_event_study(results, title="Staggered DiD Event Study")
683
+
684
+
# From a DataFrame
685
+
df = pd.DataFrame({
686
+
'period': [-2, -1, 0, 1, 2],
687
+
'effect': [0.1, 0.05, 0.0, 2.5, 2.8],
688
+
'se': [0.3, 0.25, 0.0, 0.4, 0.45]
689
+
})
690
+
plot_event_study(df, reference_period=0)
691
+
692
+
# With customization
693
+
ax = plot_event_study(
694
+
results,
695
+
title="Dynamic Treatment Effects",
696
+
xlabel="Years Relative to Treatment",
697
+
ylabel="Effect on Sales ($1000s)",
698
+
color="#2563eb",
699
+
marker="o",
700
+
shade_pre=True, # Shade pre-treatment region
701
+
show_zero_line=True, # Horizontal line at y=0
702
+
show_reference_line=True, # Vertical line at reference period
703
+
figsize=(10, 6),
704
+
show=False# Don't call plt.show(), return axes
705
+
)
706
+
```
707
+
563
708
### Synthetic Difference-in-Differences
564
709
565
710
Synthetic DiD combines the strengths of Difference-in-Differences and Synthetic Control methods by re-weighting control units to better match treated units' pre-treatment outcomes.
0 commit comments