-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathrdrobust_illustration.do
154 lines (128 loc) · 6.95 KB
/
rdrobust_illustration.do
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
********************************************************************************
** RDROBUST Stata Package
** Do-file for Empirical Illustration
** Authors: Sebastian Calonico, Matias D. Cattaneo, Max H. Farrell and Rocio Titiunik
********************************************************************************
** hlp2winpdf, cdn(rdrobust) replace
** hlp2winpdf, cdn(rdbwselect) replace
** hlp2winpdf, cdn(rdplot) replace
********************************************************************************
** net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata) replace
********************************************************************************
clear all
set more off
set linesize 80
mata: mata mlib index
********************************************************************************
** Summary Stats
********************************************************************************
use rdrobust_senate.dta, clear
sum vote margin class termshouse termssenate population, sep(2)
********************************************************************************
** rdplot with confidence intervals
********************************************************************************
rdplot vote margin, binselect(es) ci(95) ///
graph_options(title("RD Plot: U.S. Senate Election Data") ///
ytitle(Vote Share in Election at time t+1) ///
xtitle(Vote Share in Election at time t) ///
graphregion(color(white)))
**************************************************************************
** rdplot with MSE-optimal choice
**************************************************************************
rdplot vote margin, binselect(es) ///
graph_options(title(RD Plot - Senate Elections Data) ///
ytitle(Vote Share in Election at time t+1) ///
xtitle(Vote Share in Election at time t))
**************************************************************************
** rdplot with QS partitioning and mimicking variance choice
**************************************************************************
rdplot vote margin, binselect(qsmv) ///
graph_options(title(RD Plot - Senate Elections Data) ///
ytitle(Vote Share in Election at time t+1) ///
xtitle(Vote Share in Election at time t))
**************************************************************************
** rdrobust
**************************************************************************
rdrobust vote margin
**************************************************************************
** rdrobust with all estimates
**************************************************************************
rdrobust vote margin, all
**************************************************************************
** rdbwselect with all estimates
**************************************************************************
rdbwselect vote margin, all
********************************************************************************
** rdrobust backward compatibility
********************************************************************************
rdrobust vote margin, h(16.79369) b(27.43745)
********************************************************************************
** rdplot to show rdrobust estimate
********************************************************************************
qui rdrobust vote margin
rdplot vote margin if -e(h_l)<= margin & margin <= e(h_r), ///
binselect(esmv) kernel(triangular) h(`e(h_l)' `e(h_r)') p(1) ///
graph_options(title("RD Plot: U.S. Senate Election Data") ///
ytitle(Vote Share in Election at time t+2) ///
xtitle(Vote Share in Election at time t) ///
graphregion(color(white)))
********************************************************************************
** rdrobust with covariates within the same window (i.e., using same bandwidths)
********************************************************************************
qui rdrobust vote margin
local len = `e(ci_r_rb)' - `e(ci_l_rb)'
rdrobust vote margin, covs(class termshouse termssenate) ///
h(`e(h_l)' `e(h_r)') b(`e(b_l)' `e(b_r)')
display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%"
********************************************************************************
** rdrobust with covariates with data-driven optimal bandwidths
********************************************************************************
qui rdrobust vote margin
local len = `e(ci_r_rb)' - `e(ci_l_rb)'
rdrobust vote margin, covs(class termshouse termssenate)
display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%"
********************************************************************************
** rdrobust with useless covariate
********************************************************************************
qui rdrobust vote margin
local len = `e(ci_r_rb)' - `e(ci_l_rb)'
rdrobust vote margin, covs(population)
display "CI length change: " round(((`e(ci_r_rb)'-`e(ci_l_rb)')/`len'-1)*100,.01) "%"
********************************************************************************
** rdrobust check covariate "balanced"
********************************************************************************
local covs "class termshouse termssenate population"
local num: list sizeof covs
mat balance = J(`num',2,.)
local row = 1
foreach z in `covs' {
qui rdrobust `z' margin
mat balance[`row',1] = round(e(tau_cl),.001)
mat balance[`row',2] = round(e(pv_rb),.001)
local ++row
}
mat rownames balance = `covs'
mat colnames balance = "RD Effect" "Robust p-val"
mat lis balance
********************************************************************************
** rdrobust with clustering
********************************************************************************
rdrobust vote margin, vce(nncluster state)
********************************************************************************
** rdrobust with clustering and covariates, and different bandwidth
********************************************************************************
rdrobust vote margin, covs(class termshouse termssenate) ///
bwselect(msetwo) vce(nncluster state)
********************************************************************************
** rdbwselect with all estimates
********************************************************************************
rdbwselect vote margin, all
********************************************************************************
** Other examples
********************************************************************************
rdrobust vote margin, kernel(uniform) vce(cluster state)
rdrobust vote margin, bwselect(certwo) vce(hc3)
rdrobust vote margin, h(12 15) b(18 20)
rdrobust vote margin, covs(class) bwselect(cerrd) scaleregul(0) rho(1)
rdbwselect vote margin, kernel(uniform) vce(cluster state) all
rdbwselect vote margin, covs(class) bwselect(msetwo) vce(hc2) all