-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscpi_illustration-multi.py
120 lines (94 loc) · 3.89 KB
/
scpi_illustration-multi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
###############################################################################
# SCPI Python Package
# Script for Empirical Illustration - Multiple Treated Units
# Authors: Matias D. Cattaneo, Yingjie Feng, Filippo Palomba and Rocio Titiunik
###############################################################################
########################################
# Load SCPI_PKG package
import pandas
import numpy
import os
from warnings import filterwarnings
from scpi_pkg.scdataMulti import scdataMulti
from scpi_pkg.scest import scest
from scpi_pkg.scpi import scpi
from scpi_pkg.scplotMulti import scplotMulti
########################################
# Load database
os.chdir("YOUR_PATH_HERE")
data = pandas.read_csv("scpi_germany.csv")
filterwarnings("ignore")
numpy.random.seed(8894)
##############################################################################
# MULTIPLE TREATED UNITS
##############################################################################
# create treatment dummy
data['status'] = 0
data.loc[(data['country'] == "West Germany") & (data['year'] >= 1991), 'status'] = 1
# Create a second placebo treated unit
data.loc[(data['country'] == "Italy") & (data['year'] >= 1992), 'status'] = 1
id_var = 'country'
time_var = 'year'
treatment_var = 'status'
outcome_var = 'gdp'
covs_adj = {'Italy': ['constant', 'trend'],
'West Germany': [['constant', 'trend'], ['constant', 'trend']]}
###############################################
# unit-time treatment effect
###############################################
aux = scdataMulti(df=data,
id_var=id_var,
treatment_var=treatment_var,
outcome_var=outcome_var,
time_var=time_var,
features={"Italy": ["gdp", "trade"],
"West Germany": ["gdp", "infrate"]},
constant={'Italy': True, 'West Germany': False},
cointegrated_data=True, cov_adj=covs_adj)
res = scest(aux, w_constr={'name': 'simplex'})
scplotMulti(res)
res_pi = scpi(aux, w_constr={'name': 'simplex'}, sims=50, cores=1)
# plot series
scplotMulti(res_pi, ptype="series")
# plot treatment effects
scplotMulti(res_pi, ptype="treatment", joint=True)
###############################################
# average unit post-treatment effect
###############################################
aux = scdataMulti(df=data,
id_var=id_var,
treatment_var=treatment_var,
outcome_var=outcome_var,
time_var=time_var,
features={"Italy": ["gdp", "trade"],
"West Germany": ["gdp", "infrate"]},
constant={'Italy': True, 'West Germany': False},
cointegrated_data=True,
cov_adj=covs_adj, effect="unit")
res = scest(aux, w_constr={'name': 'simplex'})
scplotMulti(res)
res_pi = scpi(aux, w_constr={'name': 'simplex'}, sims=50, cores=1)
# plot series
scplotMulti(res_pi, ptype="series")
# plot treatment effects
scplotMulti(res_pi, ptype="treatment", joint=True)
###############################################
# average post-treatment effect on the treated
###############################################
aux = scdataMulti(df=data,
id_var=id_var,
treatment_var=treatment_var,
outcome_var=outcome_var,
time_var=time_var,
features={"Italy": ["gdp", "trade"],
"West Germany": ["gdp", "infrate"]},
constant={'Italy': True, 'West Germany': False},
cointegrated_data=True,
cov_adj=covs_adj, effect="time")
res = scest(aux, w_constr={'name': 'simplex'})
scplotMulti(res)
res_pi = scpi(aux, w_constr={'name': 'simplex'}, sims=50, cores=1)
# plot series
scplotMulti(res_pi, ptype="series")
# plot treatment effects
scplotMulti(res_pi, ptype="treatment", joint=True)