-
Notifications
You must be signed in to change notification settings - Fork 6
/
Hl_Est_and_Confidence_Set_RDD.R
79 lines (60 loc) · 3.12 KB
/
Hl_Est_and_Confidence_Set_RDD.R
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
Confidence_Set_and_HL_Est_RDD <-
function(.Y,
.Z,
.R,
.block,
.tau_range,
.tau_length,
.sims,
.alpha = .05,
.cores = parallel::detectCores()) {
# Preliminaries
requireNamespace("dplyr", quietly = TRUE)
requireNamespace("magrittr", quietly = TRUE)
requireNamespace("doParallel", quietly = TRUE)
taus <- taus_sup <- taus_inf <- c(NULL)
cl <- parallel::makeCluster(.cores)
doParallel::registerDoParallel(cl)
# Start loop over the range of possible taus
pvals <- foreach(tau = seq(from = .tau_range[1],
to = .tau_range[2],
length.out = .tau_length)) %dopar% {
# Adjusted outcome
Y0 <- .Y - .Z * tau
if ( is.null(.block) ) {
resid_Y0 <- resid(lm(Y0 ~ .R))
t_stat_obs <- coef(lm(resid_Y0 ~ .Z))[2]
t_stats <- replicate(.sims,
coef(lm(resid_Y0 ~ sample(.Z)))[2])
}
else {
resid_Y0 <- resid(lm(Y0 ~ .R + .block))
t_stat_obs <- coef(lm(resid_Y0 ~ .Z + .block))[2]
t_stats <- replicate(.sims,
{ Z_sim <- .Z %>%
split(., .block) %>%
lapply(., sample) %>%
unsplit(., .block)
return(coef(lm(resid_Y0 ~ Z_sim + .block))[2])
}
)}
pval <- ( sum(abs(t_stats) >= abs(t_stat_obs), na.rm = T) / .sims )
pval
}
parallel::stopCluster(cl)
taus_df <- data.frame(taus = seq(from = .tau_range[1],
to = .tau_range[2],
length.out = .tau_length),
pvals = unlist(pvals))
taus_df <- taus_df[which(taus_df$pvals >= .alpha), ] %>%
arrange(taus)
HL_est <- taus_df$taus[which.max(taus_df$pvals)[1]]
# Check for irregularities
if ( any( abs( c(min(taus_df$taus), max(taus_df$taus), HL_est) ) == Inf ) ) {
stop("Warning: Tau range could be wrong\n")
}
return(list("lower_cs" = min(taus_df$taus),
"upper_cs" = max(taus_df$taus),
"HL_est" = HL_est,
"taus_df" = taus_df))
}