Skip to content

d2cml-ai/csdid

Repository files navigation

Difference in Difference in Python

The csdid package contains tools for computing average treatment effect parameters in a Difference-in-Differences setup allowing for

  • More than two time periods

  • Variation in treatment timing (i.e., units can become treated at different points in time)

  • Treatment effect heterogeneity (i.e, the effect of participating in the treatment can vary across units and exhibit potentially complex dynamics, selection into treatment, or time effects)

  • The parallel trends assumption holds only after conditioning on covariates

The main parameters are group-time average treatment effects. These are the average treatment effect for a particular group (group is defined by treatment timing) in a particular time period. These parameters are a natural generalization of the average treatment effect on the treated (ATT) which is identified in the textbook case with two periods and two groups to the case with multiple periods.

Group-time average treatment effects are also natural building blocks for more aggregated treatment effect parameters such as overall treatment effects or event-study-type estimands.

Getting Started

There has been some recent work on DiD with multiple time periods. The did package implements the framework put forward in

This project is based on the original did R package.

Instalation

You can install csdid from pypi with:

pip install csdid

or via github:

pip install git+https://github.com/d2cml-ai/csdid/

Dependencies

Additionally, I have created an additional library called drdid, which can be installed via GitHub.

pip install git+https://github.com/d2cml-ai/DRDID

Basic Example

The following is a simplified example of the effect of states increasing their minimum wages on county-level teen employment rates which comes from Callaway and Sant’Anna (2021).

A subset of the data is available in the package and can be loaded by

from csdid.att_gt import ATTgt
import pandas as pd
data = pd.read_csv("https://raw.githubusercontent.com/d2cml-ai/csdid/function-aggte/data/mpdta.csv")

The dataset contains 500 observations of county-level teen employment rates from 2003-2007. Some states are first treated in 2004, some in 2006, and some in 2007 (see the paper for more details). The important variables in the dataset are

  • lemp This is the log of county-level teen employment. It is the outcome variable

  • first.treat This is the period when a state first increases its minimum wage. It can be 2004, 2006, or 2007. It is the variable that defines group in this application

  • year This is the year and is the time variable

  • countyreal This is an id number for each county and provides the individual identifier in this panel data context

To estimate group-time average treatment effects, use the ATTgt().fit() method

out = ATTgt(yname = "lemp",
              gname = "first.treat",
              idname = "countyreal",
              tname = "year",
              xformla = f"lemp~1",
              data = data,
              ).fit(est_method = 'dr')

Summary table

out.summ_attgt().summary2
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Group Time ATT(g, t) Post Std. Error [95% Pointwise Conf. Band]
0 2004 2004 -0.0105 1 0.0337 -0.1009 0.0799
1 2004 2005 -0.0704 1 0.0427 -0.1851 0.0442
2 2004 2006 -0.1373 1 0.0477 -0.2653 -0.0092 *
3 2004 2007 -0.1008 1 0.0469 -0.2267 0.0251
4 2006 2004 0.0065 0 0.0334 -0.0831 0.0961
5 2006 2005 -0.0028 0 0.0289 -0.0804 0.0749
6 2006 2006 -0.0046 1 0.0265 -0.0758 0.0666
7 2006 2007 -0.0412 1 0.0295 -0.1205 0.0380
8 2007 2004 0.0305 0 0.0285 -0.0459 0.1069
9 2007 2005 -0.0027 0 0.0300 -0.0832 0.0778
10 2007 2006 -0.0311 0 0.0316 -0.1160 0.0539
11 2007 2007 -0.0261 1 0.0311 -0.1096 0.0575

In the graphs, a semicolon ; should be added to prevent printing the class and the graph information.

out.plot_attgt();

out.aggte(typec='calendar');
Overall summary of ATT's based on calendar time aggregation:
    ATT Std. Error  [95.0%  Conf. Int.] 
-0.0417     0.0231 -0.0869       0.0035 


Time Effects (calendar):
   Time  Estimate  Std. Error  [95.0% Simult.   Conf. Band  
0  2004   -0.0105      0.0333          -0.0758      0.0548  
1  2005   -0.0704      0.0412          -0.1512      0.0104  
2  2006   -0.0488      0.0274          -0.1025      0.0048  
3  2007   -0.0371      0.0238          -0.0837      0.0096  
---
Signif. codes: `*' confidence band does not cover 0
Control Group:  Never Treated , 
Anticipation Periods:  0
Estimation Method:  Doubly Robust
out.plot_aggte();