Skip to content

d2cml-ai/synthdid.py

Repository files navigation

sythdid: Synthetic Difference in Difference Estimation

This package implements the synthetic difference-in-differences estimation procedure, along with a range of inference and graphing procedures, following the work of the author. The package draws on R and Julia code for optimization and Stata code for implementation in contexts with staggered adoption over multiple treatment periods (as well as in a single adoption period as in the original code). The package extends the functionality of the original code, allowing for estimation in a wider range of contexts. Overall, this package provides a comprehensive toolkit for researchers interested in using the synthetic difference-in-differences estimator in their work.

Instalation

pip install synthdid

Usage

Class input Synthdid

  • outcome: Outcome variable (numeric)
  • unit: Unit variable (numeric or string)
  • time: Time variable (numeric)
  • quota: Dummy of treatement, equal to 1 if units are treated, and otherwise 0 (numeric)

Methods:

  • .fit(cov_method = ["optimized", "projected"])
  • .vcov(method = ["placebo", "bootstrap", "jackknife"], n_reps:int = 50)

Example

California

import matplotlib.pyplot as plt
import numpy as np, pandas as pd

from synthdid.synthdid import Synthdid as sdid
from synthdid.get_data import quota, california_prop99
pd.options.display.float_format = '{:.4f}'.format

Estimations with Standard Variance-Covariance Estimation

california_estimate = sdid(california_prop99(), unit="State", time="Year", treatment="treated", outcome="PacksPerCapita").fit().vcov(method='placebo')
california_estimate.summary().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>
ATT Std. Err. t P>|t|
0 -15.6038 9.6862 -1.6109 0.1072

Estimations without Standard Variance-Covariance Estimation

california_estimate = sdid(california_prop99(), "State", "Year", "treated", "PacksPerCapita").fit()
california_estimate.summary().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>
ATT Std. Err. t P>|t|
0 -15.6038 - - -

Plots

To avoid messages from matplotlib, a semicolon ; should be added at the end of the function call.

This way:

  • estimate.plot_outcomes();
  • estimate.plot_weights();
california_estimate.plot_outcomes();

png

california_estimate.plot_weights();

png

Quota

quota_estimate = sdid(quota(), "country", "year", "quota", "womparl").fit()
quota_estimate.vcov().summary().summary2 ## placebo 
<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>
ATT Std. Err. t P>|t|
0 8.0341 1.8566 4.3272 0.0000

With covariates

quota_cov = quota().dropna(subset="lngdp")
quota_cov_est = sdid(quota_cov, "country", 'year', 'quota', 'womparl', covariates=['lngdp']).fit()
quota_cov_est.summary().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>
ATT Std. Err. t P>|t|
0 8.0490 - - -

Covariable method = 'projected'

quota_cov_est.fit(cov_method="projected").summary().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>
ATT Std. Err. t P>|t|
0 8.0590 - - -
quota_cov_est.plot_outcomes()
<synthdid.synthdid.Synthdid at 0x2313747f880>

png

png

png

png

png

png

png

quota_cov_est.plot_weights()
<synthdid.synthdid.Synthdid at 0x2313747f880>

png

png

png

png

png

png

png

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages