-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfullOutcomeSim.Rmd
More file actions
173 lines (144 loc) · 6.37 KB
/
fullOutcomeSim.Rmd
File metadata and controls
173 lines (144 loc) · 6.37 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
title: "LRD paper Appendix D, Tables from simulations presented in lrd paper"
author: "Adam C Sales & Ben B Hansen"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,error=FALSE,message=FALSE,warning=FALSE)
knitr::opts_knit$set(root.dir = '.')
```
General dependencies.
```{r}
library('knitr')
library('kableExtra')
source('R/simCIhet.r')
source('R/functions.r')
source('R/ddsandwich.R')
source('R/displaySim.r')
```
Initialization. Note that `nreps=0` corresponds to no simulations,
just print results from previously saved simulations.
In order to re-run the simulations, the `nreps`
variable should have been set to a positive integer before initiating this script.
##Level/Power Simulation
The following code (optionally, if `nreps>0`) runs the simulation
reported in Table 3 of "Limitless Regression Discontinuity"
To run the simulations in parallel, using the `parallel` package in
`R`,
register a cluster, called `cl` with the desired number of nodes, with
code similar to the following:
```{r eval=FALSE,echo=TRUE}
library(parallel)
cl <- makeCluster(5)
```
```{r runLoadOutSim, warning=FALSE, message=FALSE}
if (!exists('nreps') ) nreps <- 0
nreps
if (nreps) {
library('robustbase')
library('rdd')
library('RItools')
library('sandwich')
library('nnet')
clust <- FALSE
if(require('parallel')) if(exists('cl')) if(inherits(cl,"cluster")) clust <- TRUE
if(clust){
clusterEvalQ(cl,{
library('robustbase')
library('rdd')
library('RItools')
library('sandwich')
library('nnet')
source('R/functions.r')
source('R/simCIhet.r')
source('R/ddsandwich.R')
})
} else cl <- NULL
set.seed(201609)
st <- system.time(outcomeSim <- totalOutcomeSim(nreps,cl))
save(outcomeSim, file=paste0('./outcomeSim',Sys.Date(),'.RData'))
cat(paste0(date(), ', nreps=', nreps, '\n'),
paste(c(names(st),'\n', collapse=T)),
st,
file='fullOutcomeSim-runtime.txt', append=TRUE)
} else{ ### load the most recent simulation
sims <- sort(grep('outcomeSim',list.files('.'),value=TRUE),decreasing=TRUE)
load(paste0('./',sims[1]))
}
```
This code creates Table 3:
```{r table3}
capture.output({
displayCIsimHet(outcomeSim,tau=0,
caption=paste('Empirical bias and 95\\% confidence interval coverage (\\%) and width for the analyses of',prettyNum(ncol(outcomeSim[[1]]),big.mark=','),'simulated datasets using either permutation tests, limitless or local OLS methods. The average treatment effect was zero in all conditions; in six conditions the effect was uniquely zero, and in three it was distributed as $t_3$.'),
label='tab:level')
},file="./tab-levelSimulation.tex")
```
Here are the results, for all cases run:
```{r fullOutSim}
allRes <- dispAllSimp(outcomeSim)
rownames(allRes) <- NULL
save(allRes,file=paste0('./levelSimResults',Sys.Date(),'.RData'))
```
###Full Results: Bias
```{r biasTab}
kable(subset(allRes,meas=='Bias',select=-meas),caption=paste('Empirical bias for the analyses of ',ncol(outcomeSim[[1]]),'simulated datasets using either permutation tests, limitless or local OLS methods. Results for all conditions'),digits=2,format='html')%>%
kable_styling(full_width=FALSE)%>%
# group_rows("n=50",1,24)%>%group_rows("n=250",25,48)%>%group_rows("n=2500",49,72)%>%
collapse_rows(columns = 1:4)#, valign = "center")
```
###95% CI Coverage
```{r coverTab}
kable(subset(allRes,meas=='Coverage',select=-meas),caption=paste('Empirical 95% confidence interval coverage for the analyses of ',ncol(outcomeSim[[1]]),'simulated datasets using either permutation tests, limitless or local OLS methods. Results for all conditions'),digits=2,format='html')%>%
kable_styling(full_width=FALSE)%>%
# group_rows("n=50",1,24)%>%group_rows("n=250",25,48)%>%group_rows("n=2500",49,72)%>%
collapse_rows(columns = 1:4)#, valign = "center")
```
###95% CI Width
```{r widthTab}
kable(subset(allRes,meas=='Width',select=-meas),caption=paste('Empirical 95% confidence interval width for the analyses of ',ncol(outcomeSim[[1]]),'simulated datasets using either permutation tests, limitless or local OLS methods. Results for all conditions'),digits=2,format='html')%>%
kable_styling(full_width=FALSE)%>%
# group_rows("n=50",1,24)%>%group_rows("n=250",25,48)%>%group_rows("n=2500",49,72)%>%
collapse_rows(columns = 1:4)#, valign = "center")
```
##Polynomial Simulation
```{r runLoadPolySim, warning=FALSE, message=FALSE}
if (!exists('nreps') ) nreps <- 0
nreps
if (nreps) {
set.seed(201609)
st2 <- system.time(totalPoly <- totalPolySim(nreps,cl))
save(totalPoly,file=paste0("./totalPolySim",Sys.Date(),".RData"))
cat(paste0(date(), ', nreps=', nreps, '\n'),
paste(c(names(st),'\n', collapse=T)),
st,
file='totalPolySim-runtime.txt', append=TRUE)
} else{
psims <- sort(grep('totalPolySim',list.files('.'),value=TRUE),decreasing=TRUE)
load(paste0('./',psims[1]))
}
```
The following gives the results in Table 4 of the paper, in addition
to the break-down of RMSE into bias and variance, and analogous
results for normally-distributed errors.
```{r}
tab.paper <- prntTab(totalPoly,5,full=FALSE,md=FALSE)
capture.output(
polyLatex5(tab.paper,full=FALSE,caption=paste0('Results from ',prettyNum(ncol(totalPoly[[1]]),big.mark=','),' simulations of polynomial specifications for RDD analysis, using limitless, OLS, or local linear regression. Data-generating models (DGM) were as depicted in Figure~\\ref{fig:dgms}, with $t_{3}$ errors; sample size for all runs was 500; there was no treatment effect.'),label='tab:poly'),
file="./tab-polynomialSimulation.tex")
tab <- prntTab(totalPoly,5,full=TRUE,md=FALSE)
#rownames(tab) <- rep(c('level','RMSE','bias','sd'),6)
colnames(tab) <- gsub('(sh|ik)\\.est.','deg=',colnames(tab))#c(rep(paste0('deg=',1:4),2),'')
colnames(tab)[ncol(tab)] <- 'n/a'
kable(tab,format='html',caption='Full results for polynomial simulation',digits=2)%>%
kable_styling()%>% column_spec( 6,border_right=TRUE)%>%column_spec(11,border_right=TRUE)%>%
add_header_above(c(" " = 1, "Limitless" = 5, "OLS" = 5, "Loc. Lin." = 1))%>%
#group_rows("$t_3$ Error",1,12)%>%group_rows("N(0,1) Error",13,24)%>%
group_rows("linear",1,3)%>%group_rows('antiSym',4,6)%>%group_rows('sine',7,9)#%>%
#group_rows("linear",13,16)%>%group_rows('antiSym',17,20)%>%group_rows('oneSide',21,24)
```
Session information
```{r}
sessionInfo()
```