# 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](https://github.com/synth-inference/synthdid) and [Julia](https://github.com/d2cml-ai/Synthdid.jl) code for optimization and [Stata](https://github.com/Daniel-Pailanir/sdid) 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
```py
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
```python
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
```python
california_estimate = sdid(california_prop99(), unit="State", time="Year", treatment="treated", outcome="PacksPerCapita").fit().vcov(method='placebo')
california_estimate.summary().summary2
```
<div>
<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>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>ATT</th>
<th>Std. Err.</th>
<th>t</th>
<th>P>|t|</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-15.6038</td>
<td>9.6862</td>
<td>-1.6109</td>
<td>0.1072</td>
</tr>
</tbody>
</table>
</div>
Estimations without Standard Variance-Covariance Estimation
```python
california_estimate = sdid(california_prop99(), "State", "Year", "treated", "PacksPerCapita").fit()
california_estimate.summary().summary2
```
<div>
<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>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>ATT</th>
<th>Std. Err.</th>
<th>t</th>
<th>P>|t|</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>-15.6038</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
#### 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();`
```python
california_estimate.plot_outcomes();
```
![png](Readme_files/Readme_11_0.png)
```python
california_estimate.plot_weights();
```
![png](Readme_files/Readme_12_0.png)
## Quota
```python
quota_estimate = sdid(quota(), "country", "year", "quota", "womparl").fit()
```
```python
quota_estimate.vcov().summary().summary2 ## placebo
```
<div>
<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>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>ATT</th>
<th>Std. Err.</th>
<th>t</th>
<th>P>|t|</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>8.0341</td>
<td>1.8566</td>
<td>4.3272</td>
<td>0.0000</td>
</tr>
</tbody>
</table>
</div>
#### With covariates
```python
quota_cov = quota().dropna(subset="lngdp")
quota_cov_est = sdid(quota_cov, "country", 'year', 'quota', 'womparl', covariates=['lngdp']).fit()
quota_cov_est.summary().summary2
```
<div>
<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>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>ATT</th>
<th>Std. Err.</th>
<th>t</th>
<th>P>|t|</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>8.0490</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
Covariable method = 'projected'
```python
quota_cov_est.fit(cov_method="projected").summary().summary2
```
<div>
<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>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>ATT</th>
<th>Std. Err.</th>
<th>t</th>
<th>P>|t|</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>8.0590</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
</div>
```python
quota_cov_est.plot_outcomes()
```
<synthdid.synthdid.Synthdid at 0x2313747f880>
![png](Readme_files/Readme_20_1.png)
![png](Readme_files/Readme_20_2.png)
![png](Readme_files/Readme_20_3.png)
![png](Readme_files/Readme_20_4.png)
![png](Readme_files/Readme_20_5.png)
![png](Readme_files/Readme_20_6.png)
![png](Readme_files/Readme_20_7.png)
```python
quota_cov_est.plot_weights()
```
<synthdid.synthdid.Synthdid at 0x2313747f880>
![png](Readme_files/Readme_21_1.png)
![png](Readme_files/Readme_21_2.png)
![png](Readme_files/Readme_21_3.png)
![png](Readme_files/Readme_21_4.png)
![png](Readme_files/Readme_21_5.png)
![png](Readme_files/Readme_21_6.png)
![png](Readme_files/Readme_21_7.png)
Raw data
{
"_id": null,
"home_page": "https://github.com/d2cml-ai/synthdid.py",
"name": "synthdid",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "causal-inference",
"author": "D2CML Team, Alexander Quispe, Rodrigo Grijalba, Jhon Flores, Franco Caceres",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ff/91/78a40a3e6aafc1b3b8a65c7e5e2a2ffeaa831c0d70823938c11ff14c9906/synthdid-0.10.1.tar.gz",
"platform": null,
"description": "# sythdid: Synthetic Difference in Difference Estimation\r\n\r\nThis 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](https://github.com/synth-inference/synthdid) and [Julia](https://github.com/d2cml-ai/Synthdid.jl) code for optimization and [Stata](https://github.com/Daniel-Pailanir/sdid) 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.\r\n\r\n\r\n\r\n## Instalation\r\n\r\n```py\r\npip install synthdid\r\n```\r\n\r\n## Usage\r\n\r\n### Class input `Synthdid`\r\n\r\n- `outcome`: Outcome variable (numeric)\r\n- `unit`: Unit variable (numeric or string)\r\n- `time`: Time variable (numeric)\r\n- `quota`: Dummy of treatement, equal to 1 if units are treated, and otherwise 0 (numeric)\r\n\r\n### Methods:\r\n\r\n- `.fit(cov_method = [\"optimized\", \"projected\"])`\r\n- `.vcov(method = [\"placebo\", \"bootstrap\", \"jackknife\"], n_reps:int = 50)`\r\n\r\n## Example\r\n\r\n### California \r\n\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np, pandas as pd\r\n\r\nfrom synthdid.synthdid import Synthdid as sdid\r\nfrom synthdid.get_data import quota, california_prop99\r\npd.options.display.float_format = '{:.4f}'.format\r\n```\r\n\r\nEstimations with Standard Variance-Covariance Estimation\r\n\r\n\r\n```python\r\ncalifornia_estimate = sdid(california_prop99(), unit=\"State\", time=\"Year\", treatment=\"treated\", outcome=\"PacksPerCapita\").fit().vcov(method='placebo')\r\ncalifornia_estimate.summary().summary2\r\n```\r\n\r\n\r\n\r\n\r\n<div>\r\n<style scoped>\r\n .dataframe tbody tr th:only-of-type {\r\n vertical-align: middle;\r\n }\r\n\r\n .dataframe tbody tr th {\r\n vertical-align: top;\r\n }\r\n\r\n .dataframe thead th {\r\n text-align: right;\r\n }\r\n</style>\r\n<table border=\"1\" class=\"dataframe\">\r\n <thead>\r\n <tr style=\"text-align: right;\">\r\n <th></th>\r\n <th>ATT</th>\r\n <th>Std. Err.</th>\r\n <th>t</th>\r\n <th>P>|t|</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <th>0</th>\r\n <td>-15.6038</td>\r\n <td>9.6862</td>\r\n <td>-1.6109</td>\r\n <td>0.1072</td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n</div>\r\n\r\n\r\n\r\nEstimations without Standard Variance-Covariance Estimation\r\n\r\n\r\n```python\r\ncalifornia_estimate = sdid(california_prop99(), \"State\", \"Year\", \"treated\", \"PacksPerCapita\").fit()\r\ncalifornia_estimate.summary().summary2\r\n```\r\n\r\n\r\n\r\n\r\n<div>\r\n<style scoped>\r\n .dataframe tbody tr th:only-of-type {\r\n vertical-align: middle;\r\n }\r\n\r\n .dataframe tbody tr th {\r\n vertical-align: top;\r\n }\r\n\r\n .dataframe thead th {\r\n text-align: right;\r\n }\r\n</style>\r\n<table border=\"1\" class=\"dataframe\">\r\n <thead>\r\n <tr style=\"text-align: right;\">\r\n <th></th>\r\n <th>ATT</th>\r\n <th>Std. Err.</th>\r\n <th>t</th>\r\n <th>P>|t|</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <th>0</th>\r\n <td>-15.6038</td>\r\n <td>-</td>\r\n <td>-</td>\r\n <td>-</td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n</div>\r\n\r\n\r\n\r\n#### Plots\r\n\r\nTo avoid messages from matplotlib, a semicolon `;` should be added at the end of the function call.\r\n\r\nThis way:\r\n\r\n- `estimate.plot_outcomes();`\r\n- `estimate.plot_weights();`\r\n\r\n\r\n```python\r\ncalifornia_estimate.plot_outcomes();\r\n```\r\n\r\n\r\n \r\n![png](Readme_files/Readme_11_0.png)\r\n \r\n\r\n\r\n\r\n```python\r\ncalifornia_estimate.plot_weights();\r\n```\r\n\r\n\r\n \r\n![png](Readme_files/Readme_12_0.png)\r\n \r\n\r\n\r\n## Quota\r\n\r\n\r\n```python\r\nquota_estimate = sdid(quota(), \"country\", \"year\", \"quota\", \"womparl\").fit()\r\n\r\n```\r\n\r\n\r\n```python\r\nquota_estimate.vcov().summary().summary2 ## placebo \r\n```\r\n\r\n\r\n\r\n\r\n<div>\r\n<style scoped>\r\n .dataframe tbody tr th:only-of-type {\r\n vertical-align: middle;\r\n }\r\n\r\n .dataframe tbody tr th {\r\n vertical-align: top;\r\n }\r\n\r\n .dataframe thead th {\r\n text-align: right;\r\n }\r\n</style>\r\n<table border=\"1\" class=\"dataframe\">\r\n <thead>\r\n <tr style=\"text-align: right;\">\r\n <th></th>\r\n <th>ATT</th>\r\n <th>Std. Err.</th>\r\n <th>t</th>\r\n <th>P>|t|</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <th>0</th>\r\n <td>8.0341</td>\r\n <td>1.8566</td>\r\n <td>4.3272</td>\r\n <td>0.0000</td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n</div>\r\n\r\n\r\n\r\n#### With covariates\r\n\r\n\r\n```python\r\n\r\nquota_cov = quota().dropna(subset=\"lngdp\")\r\nquota_cov_est = sdid(quota_cov, \"country\", 'year', 'quota', 'womparl', covariates=['lngdp']).fit()\r\nquota_cov_est.summary().summary2\r\n```\r\n\r\n\r\n\r\n\r\n<div>\r\n<style scoped>\r\n .dataframe tbody tr th:only-of-type {\r\n vertical-align: middle;\r\n }\r\n\r\n .dataframe tbody tr th {\r\n vertical-align: top;\r\n }\r\n\r\n .dataframe thead th {\r\n text-align: right;\r\n }\r\n</style>\r\n<table border=\"1\" class=\"dataframe\">\r\n <thead>\r\n <tr style=\"text-align: right;\">\r\n <th></th>\r\n <th>ATT</th>\r\n <th>Std. Err.</th>\r\n <th>t</th>\r\n <th>P>|t|</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <th>0</th>\r\n <td>8.0490</td>\r\n <td>-</td>\r\n <td>-</td>\r\n <td>-</td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n</div>\r\n\r\n\r\n\r\nCovariable method = 'projected'\r\n\r\n\r\n```python\r\nquota_cov_est.fit(cov_method=\"projected\").summary().summary2\r\n```\r\n\r\n\r\n\r\n\r\n<div>\r\n<style scoped>\r\n .dataframe tbody tr th:only-of-type {\r\n vertical-align: middle;\r\n }\r\n\r\n .dataframe tbody tr th {\r\n vertical-align: top;\r\n }\r\n\r\n .dataframe thead th {\r\n text-align: right;\r\n }\r\n</style>\r\n<table border=\"1\" class=\"dataframe\">\r\n <thead>\r\n <tr style=\"text-align: right;\">\r\n <th></th>\r\n <th>ATT</th>\r\n <th>Std. Err.</th>\r\n <th>t</th>\r\n <th>P>|t|</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <th>0</th>\r\n <td>8.0590</td>\r\n <td>-</td>\r\n <td>-</td>\r\n <td>-</td>\r\n </tr>\r\n </tbody>\r\n</table>\r\n</div>\r\n\r\n\r\n\r\n\r\n```python\r\nquota_cov_est.plot_outcomes()\r\n```\r\n\r\n\r\n\r\n\r\n <synthdid.synthdid.Synthdid at 0x2313747f880>\r\n\r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_1.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_2.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_3.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_4.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_5.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_6.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_20_7.png)\r\n \r\n\r\n\r\n\r\n```python\r\nquota_cov_est.plot_weights()\r\n```\r\n\r\n\r\n\r\n\r\n <synthdid.synthdid.Synthdid at 0x2313747f880>\r\n\r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_1.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_2.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_3.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_4.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_5.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_6.png)\r\n \r\n\r\n\r\n\r\n \r\n![png](Readme_files/Readme_21_7.png)\r\n \r\n\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Synthdid",
"version": "0.10.1",
"project_urls": {
"Homepage": "https://github.com/d2cml-ai/synthdid.py"
},
"split_keywords": [
"causal-inference"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "91370ab7b990f831973b2d11e2b4a64895ce7c4aaa03d7db9ff4a6640b4d7885",
"md5": "1bd71f5533e0078e7c67fa0e2a5a5edb",
"sha256": "a4f4d711bab01501f50532dbf4781892114b4775d5210c413a1379ca6799e8e5"
},
"downloads": -1,
"filename": "synthdid-0.10.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1bd71f5533e0078e7c67fa0e2a5a5edb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 16803,
"upload_time": "2024-06-27T22:03:26",
"upload_time_iso_8601": "2024-06-27T22:03:26.799875Z",
"url": "https://files.pythonhosted.org/packages/91/37/0ab7b990f831973b2d11e2b4a64895ce7c4aaa03d7db9ff4a6640b4d7885/synthdid-0.10.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ff9178a40a3e6aafc1b3b8a65c7e5e2a2ffeaa831c0d70823938c11ff14c9906",
"md5": "6a91e68fee06c42a5c4179de34505a83",
"sha256": "483e23c0d12ff7a8e7225acc86c6e37bad7e8367d3b8dd4b9aa9a297bd789221"
},
"downloads": -1,
"filename": "synthdid-0.10.1.tar.gz",
"has_sig": false,
"md5_digest": "6a91e68fee06c42a5c4179de34505a83",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 16645,
"upload_time": "2024-06-27T22:03:28",
"upload_time_iso_8601": "2024-06-27T22:03:28.341401Z",
"url": "https://files.pythonhosted.org/packages/ff/91/78a40a3e6aafc1b3b8a65c7e5e2a2ffeaa831c0d70823938c11ff14c9906/synthdid-0.10.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-27 22:03:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "d2cml-ai",
"github_project": "synthdid.py",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "contourpy",
"specs": [
[
"==",
"1.0.7"
]
]
},
{
"name": "cycler",
"specs": [
[
"==",
"0.11.0"
]
]
},
{
"name": "fonttools",
"specs": [
[
"==",
"4.39.0"
]
]
},
{
"name": "importlib-resources",
"specs": [
[
"==",
"5.12.0"
]
]
},
{
"name": "kiwisolver",
"specs": [
[
"==",
"1.4.4"
]
]
},
{
"name": "matplotlib",
"specs": [
[
"==",
"3.7.1"
]
]
},
{
"name": "numpy",
"specs": [
[
"==",
"1.23.5"
]
]
},
{
"name": "packaging",
"specs": [
[
"==",
"23.0"
]
]
},
{
"name": "pandas",
"specs": [
[
"==",
"1.5.3"
]
]
},
{
"name": "Pillow",
"specs": [
[
"==",
"9.4.0"
]
]
},
{
"name": "pyparsing",
"specs": [
[
"==",
"3.0.9"
]
]
},
{
"name": "python-dateutil",
"specs": [
[
"==",
"2.8.2"
]
]
},
{
"name": "pytz",
"specs": [
[
"==",
"2022.7.1"
]
]
},
{
"name": "six",
"specs": [
[
"==",
"1.16.0"
]
]
},
{
"name": "zipp",
"specs": [
[
"==",
"3.15.0"
]
]
}
],
"lcname": "synthdid"
}