-
Notifications
You must be signed in to change notification settings - Fork 8
/
rdlocrand_illustration.do
128 lines (81 loc) · 4.48 KB
/
rdlocrand_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
********************************************************************************
** RDLOCRAND Stata Package
** Empirical Illustration
** Authors: Matias D. Cattaneo, Rocio Titiunik and Gonzalo Vazquez-Bare
** Last update: 2022-06-22
********************************************************************************
* net install rdlocrand, from(https://raw.githubusercontent.com/rdpackages/rdlocrand/master/stata) replace
********************************************************************************
use rdlocrand_senate.dta, clear
global covariates presdemvoteshlag1 population demvoteshlag1 ///
demvoteshlag2 demwinprv1 demwinprv2 dopen dmidterm
********************************************************************************
** Summary Stats
********************************************************************************
describe $covariates
summarize demmv $covariates
********************************************************************************
** rdwinselect
********************************************************************************
** Replicate first table of Stata journal article (deprecated default options - not recommended)
rdwinselect demmv $covariates, cutoff(0) obsstep(2)
** Window selection with default options
rdwinselect demmv $covariates, cutoff(0)
** Window selection with asymmetric windows
rdwinselect demmv $covariates, cutoff(0) wasym
** Window selection setting window length and increments (replicate CFT)
rdwinselect demmv $covariates, wmin(.5) wstep(.125) reps(10000)
** Window selection using large sample approximation and plotting p-values
quietly rdwinselect demmv $covariates, wmin(.5) wstep(.125) ///
nwin(80) approximate plot
********************************************************************************
** rdrandinf
********************************************************************************
** Randomization inference using recommended window
rdrandinf demvoteshfor2 demmv, wl(-.75) wr(.75)
** Randomization inference using recommended window, all statistics
rdrandinf demvoteshfor2 demmv, wl(-.75) wr(.75) statistic(all)
** Randomization inference using recommended window using rdwinselect
rdrandinf demvoteshfor2 demmv, statistic(all) covariates($covariates) ///
wmin(.5) wstep(.125) level(0.16) quietly rdwreps(10000)
** Randomization inference using recommended window, linear adjustment
rdrandinf demvoteshfor2 demmv, statistic(all) wl(-.75) wr(.75) p(1)
** Randomization inference under interference
rdrandinf demvoteshfor2 demmv, wl(-.75) wr(.75) interfci(.05)
********************************************************************************
** rdsensitivity
********************************************************************************
rdsensitivity demvoteshfor2 demmv, wlist(.75(.25)2) tlist(0(1)20) verbose
** Obtain 95 percent confidence interval for window [-.75 ; .75]
rdsensitivity demvoteshfor2 demmv, wlist(.75(.25)2) tlist(0(1)20) nodots ci(-.75 .75)
** Replicate contour plot
rdsensitivity demvoteshfor2 demmv, wlist(.75(.25)10) tlist(0(1)20) nodots ///
saving(graphdata)
preserve
use graphdata, clear
twoway contour pvalue t w, ccuts(0(0.05)1)
restore
preserve
use graphdata, clear
twoway contour pvalue t w, ccuts(0(0.05)1) ccolors(gray*0.01 gray*0.05 ///
gray*0.1 gray*0.15 gray*0.2 gray*0.25 gray*0.3 gray*0.35 ///
gray*0.4 gray*0.5 gray*0.6 gray*0.7 gray*0.8 gray*0.9 gray ///
black*0.5 black*0.6 black*0.7 black*0.8 black*0.9 black) ///
xlabel(.75(1.25)10) ylabel(0(2)20, nogrid) graphregion(fcolor(none))
restore
** rdsensitivity to calculate CI from within rdrandinf
rdrandinf demvoteshfor2 demmv, wl(-.75) wr(.75) ci(.05 3(1)20)
********************************************************************************
** rdrbounds
********************************************************************************
rdrbounds demvoteshfor2 demmv, expgamma(1.5 2 3) wlist(.5 .75 1) reps(1000)
** Bernoulli and fixed margins p-values
rdrbounds demvoteshfor2 demmv, expgamma(1.5 2 3) wlist(.5 .75 1) reps(1000) fmpval
********************************************************************************
** rdrandinf with eval options
********************************************************************************
qui sum demmv if abs(demmv)<=.75 & demmv>=0 & demmv!=. & demvoteshfor2!=.
local mt = r(mean)
qui sum demmv if abs(demmv)<=.75 & demmv<0 & demmv!=. & demvoteshfor2!=.
local mc = r(mean)
rdrandinf demvoteshfor2 demmv, wl(-.75) wr(.75) p(1) evall(`mc') evalr(`mt')