-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrdrobust_illustration.py
117 lines (91 loc) · 4.39 KB
/
rdrobust_illustration.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
###########################################################################
## RDROBUST Python Package
## Script for Empirical Illustration
## Authors: Sebastian Calonico, Matias D. Cattaneo,
## Max H. Farrell, Ricardo Masini and Rocio Titiunik
###########################################################################
### Load RDROBUST package
from rdrobust import rdrobust,rdbwselect,rdplot
import pandas as pd
### Load data base
rdrobust_senate = pd.read_csv("rdrobust_senate.csv")
# Define the variblrs
margin = rdrobust_senate.margin
vote = rdrobust_senate.vote
### rdplot with 95% confidence intervals
rdplot(y=vote, x=margin, binselect="es", ci=95,
title="RD Plot: U.S. Senate Election Data",
y_label="Vote Share in Election at time t+2",
x_label="Vote Share in Election at time t")
### rdplot with MSE-optimal choice
rdplot(y=vote, x=margin, binselect="es",
title="RD Plot: U.S. Senate Election Data",
y_label="Vote Share in Election at time t+2",
x_label="Vote Share in Election at time t")
### rdplot with QS partitioning and mimicking variance choice
rdplot(y=vote, x=margin, binselect="qsmv",
title="RD Plot: U.S. Senate Election Data",
y_label="Vote Share in Election at time t+2",
x_label="Vote Share in Election at time t")
### rdrobust
print(rdrobust(y=vote, x=margin))
### rdrobust with all estimates
print(rdrobust(y=vote, x=margin, all=True))
## rdrobust backward compatibility
print(rdrobust(y=vote, x=margin, h=16.79369, b=27.43745))
## rdplot to show rdrobust estimate
est = rdrobust(y=vote, x=margin)
h_l, h_r = est.bws.loc['h', :].values
subset = ((-h_l<= margin) & (margin <= h_r)).values
rdplot(y=vote, x=margin, subset=subset,
binselect="esmv", kernel="triangular", h=[h_l,h_r], p=1,
title="RD Plot: U.S. Senate Election Data",
y_label="Vote Share in Election at time t+2",
x_label="Vote Share in Election at time t")
## rdrobust with covariates within the same window (i.e., using same bandwidths)
est1 = rdrobust(y=vote, x=margin)
len1 = est1.ci.iloc[2,1] - est1.ci.iloc[2,0]
covs = rdrobust_senate[['class','termshouse','termssenate']]
b_l, b_r = est.bws.loc['b', :].values
est2 = rdrobust(y=vote, x=margin, covs=covs,
h = [h_l,h_r],
b = [b_l,b_r])
len2 = est2.ci.iloc[2,1] - est2.ci.iloc[2,0]
print("CI length change: " + str(round((len2/len1-1)*100,2)) + "%")
## rdrobust with covariates with data-driven optimal bandwidths
est1 = rdrobust(y=vote, x=margin)
len1 = est1.ci.iloc[2,1] - est1.ci.iloc[2,0]
est2 = rdrobust(y=vote, x=margin, covs=covs)
len2 = est2.ci.iloc[2,1] - est2.ci.iloc[2,0]
print("CI length change: " + str(round((len2/len1-1)*100,2)) + "%")
## rdrobust with useless covariate
est1 = rdrobust(y=vote, x=margin)
len1 = est1.ci.iloc[2,1] - est1.ci.iloc[2,0]
covs = rdrobust_senate['population']
est2 = rdrobust(y=vote, x=margin, covs=covs)
len2 = est2.ci.iloc[2,1] - est2.ci.iloc[2,0]
print("CI length change: " + str(round((len2/len1-1)*100,2))+ "%")
## rdrobust check covariate "balanced"
covs = rdrobust_senate[['class','termshouse','termssenate','population']]
balance = pd.DataFrame(columns = ["RD Effect", "Robust p-val"],
index = pd.Index(["class","termshouse", "termssenate", "population"]))
for z in covs.columns:
est = rdrobust(y=covs[z], x=margin)
balance.loc[z,"RD Effect"] = est.Estimate["tau.us"].values[0]
balance.loc[z,"Robust p-val"] = est.pv.iloc[2].values[0]
print(balance)
## rdrobust with clustering
state =rdrobust_senate.state.values
print(rdrobust(y=vote, x=margin, vce="nn", cluster=state))
## rdrobust with clustering and covariates, and different bandwidth
covs = rdrobust_senate[['class','termshouse','termssenate']]
print(rdrobust(y=vote, x=margin, vce="nn", bwselect="msetwo", covs=covs, cluster=state))
## rdbwselect with all estimates
print(rdbwselect(y=vote, x=margin, all=True))
## Other examples
print(rdrobust(y=vote, x=margin, kernel="uniform", vce="hc1", cluster=state))
print(rdrobust(y=vote, x=margin, bwselect="certwo", vce="hc3"))
print(rdrobust(y=vote, x=margin, h=(12,15), b=(18,20)))
print(rdrobust(y=vote, x=margin, covs=rdrobust_senate['class'], bwselect="cerrd", scaleregul=0, rho=1))
print(rdbwselect(y=vote, x=margin, kernel="uniform", vce="hc1", cluster=state, all=True))
print(rdbwselect(y=vote, x=margin, covs=rdrobust_senate['class'], bwselect="msetwo", vce="hc2", all=True))