diff-diff
Difference-in-Differences causal inference in Python. Callaway-Sant'Anna, Synthetic DiD, Honest DiD, event studies. sklearn-like API, validated against R.
What is diff-diff?
Difference-in-Differences causal inference in Python. Callaway-Sant'Anna, Synthetic DiD, Honest DiD, event studies. sklearn-like API, validated against R.
Overview of diff-diff
What it is
diff-diff is a Python library for Difference-in-Differences (DiD) causal inference. It provides sklearn-like estimators with statsmodels-style outputs, built for econometricians, marketing analysts, and data scientists running campaign-lift, policy, and staggered-rollout analyses.
Key features
- Includes estimators: DifferenceInDifferences, TwoWayFixedEffects, CallawaySantAnna, SunAbraham, ImputationDiD, TwoStageDiD, SyntheticDiD, SyntheticControl, SpilloverDiD, ChaisemartinDHaultfoeuille, and others.
- Provides a practitioner workflow (Baker et al. 2025) with 8 steps for rigorous DiD analysis, including assumption testing, sensitivity analysis, and robustness checks.
- Offers a
get_llm_guide()function that returns a concise API reference or the full 8-step workflow, bundled in the wheel with no network access required. - Includes
BusinessReportandDiagnosticReportexperimental preview classes that produce plain-English output and a structuredto_dict()schema from fitted estimator results. - Supports survey data aggregation via
aggregate_survey()for rolling respondent-level microdata into a geographic-period panel with inverse-variance precision weights. - Validated against R packages
did,synthdid, andfixest.
Use cases
- Measuring campaign lift in marketing.
- Evaluating product launches or policy rollouts with staggered adoption.
- Analyzing brand awareness surveys with complex survey design and staggered rollouts.
- Conducting causal inference with few treated units using synthetic DiD or synthetic control.
Getting started
bashpip install diff-diff``````pythonimport pandas as pdfrom diff_diff import DifferenceInDifferencesdata = pd.DataFrame({ 'outcome': [10, 11, 15, 18, 9, 10, 12, 13], 'treated': [1, 1, 1, 1, 0, 0, 0, 0], 'post': [0, 0, 1, 1, 0, 0, 1, 1],})did = DifferenceInDifferences()results = did.fit(data, outcome='outcome', treatment='treated', post='post')print(results)
License and pricing
License: MIT. Pricing: open source.
這是什麼
diff-diff 是一個用於差異中的差異(DiD)因果推論的 Python 函式庫。它提供類似 sklearn 的估計器與 statsmodels 風格的輸出,專為計量經濟學家、行銷分析師和資料科學家設計,用於執行活動提升、政策與分階段推出的分析。
主要功能
- 包含多種估計器:DifferenceInDifferences、TwoWayFixedEffects、CallawaySantAnna、SunAbraham、ImputationDiD、TwoStageDiD、SyntheticDiD、SyntheticControl、SpilloverDiD、ChaisemartinDHaultfoeuille 等。
- 提供實務工作流程(Baker et al. 2025),包含 8 個步驟進行嚴謹的 DiD 分析,包括假設檢定、敏感度分析和穩健性檢查。
- 提供
get_llm_guide()函式,可回傳簡潔的 API 參考或完整的 8 步驟工作流程,並內建於 wheel 中,無需網路連線。 - 包含
BusinessReport和DiagnosticReport實驗性預覽類別,可從已配適的估計器結果產生純英文輸出和結構化的to_dict()架構。 - 支援透過
aggregate_survey()進行調查資料聚合,將受訪者層級的微資料彙整為地理-時間面板,並附帶反變異數精確權重。 - 已針對 R 套件
did、synthdid和fixest進行驗證。
使用情境
- 衡量行銷活動的提升效果。
- 評估產品推出或分階段實施的政策。
- 分析具有複雜調查設計和分階段推出的品牌知名度調查。
- 在處理組數量較少時,使用合成 DiD 或合成控制進行因果推論。
快速開始
bashpip install diff-diff``````pythonimport pandas as pdfrom diff_diff import DifferenceInDifferencesdata = pd.DataFrame({ 'outcome': [10, 11, 15, 18, 9, 10, 12, 13], 'treated': [1, 1, 1, 1, 0, 0, 0, 0], 'post': [0, 0, 1, 1, 0, 0, 1, 1],})did = DifferenceInDifferences()results = did.fit(data, outcome='outcome', treatment='treated', post='post')print(results)
授權與價格
授權:MIT。價格:開源。
Based on the project's public README and repository metadata.