Metadata-Version: 2.1
Name: synthdid
Version: 0.10.0
Summary: Synthdid
Home-page: https://github.com/d2cml-ai/synthdid.py
Author: D2CML Team, Alexander Quispe, Rodrigo  Grijalba, Jhon Flores, Franco Caceres
License: MIT
Keywords: causal-inference
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Topic :: Scientific/Engineering
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: appdirs ==1.4.3
Requires-Dist: contourpy ==1.0.7
Requires-Dist: cycler ==0.11.0
Requires-Dist: fonttools ==4.39.0
Requires-Dist: importlib-resources ==5.12.0
Requires-Dist: kiwisolver ==1.4.4
Requires-Dist: matplotlib ==3.7.1
Requires-Dist: numpy ==1.23.5
Requires-Dist: packaging ==23.0
Requires-Dist: pandas ==1.5.3
Requires-Dist: patsy ==0.5.3
Requires-Dist: pillow ==9.4.0
Requires-Dist: pipenv-setup ==2.0.0
Requires-Dist: pipfile ==0.0.2
Requires-Dist: pyparsing ==3.0.9
Requires-Dist: python-dateutil ==2.8.2
Requires-Dist: pytz ==2022.7.1
Requires-Dist: six ==1.16.0
Requires-Dist: statsmodels ==0.13.5
Requires-Dist: toml ==0.10.0
Requires-Dist: zipp ==3.15.0
Requires-Dist: attrs ==19.1.0 ; python_version >= "2.7" and python_version not in "3.0, 3.1, 3.2, 3.3"
Requires-Dist: click ==7.0 ; python_version >= "2.7" and python_version not in "3.0, 3.1, 3.2, 3.3"
Requires-Dist: scipy ==1.10.1 ; python_version >= "3.10" and python_version < "3.12" and platform_system != "Windows" or platform_machine != "x86"
Requires-Dist: black ==19.3b0 ; python_version >= "3.6"

# 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&gt;|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&gt;|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&gt;|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&gt;|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&gt;|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)
    

