-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrddnotes.R
81 lines (58 loc) · 1.37 KB
/
rddnotes.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
80
## Packages
library(rdd)
library(rdpower)
library(tidyverse)
library(tidymodels)
## Testing Package
RDestimate(dta$cvp_non_defense~f$c.80)
## Setting Seed
set.seed(12389)
## Simulating Data
x<-runif(1000,-1,1)
cov<-rnorm(1000)
y<-3+2*x+3*cov+10*(x>=0)+rnorm(1000)
## Function to Find Percentiles
percentile <- function(x) {
unique(quantile(x, probs = c(seq(.01, 1, by = .01))))
}
## Saving Percentiles (Candidate Cut-points)
g <- percentile(df$age)
## Initializing Data Frame
f <- data.frame(df$wjtest01)
## Creates the Candidate (x-c)'s
for (i in 1:length(g)) {
p <- paste0("X_", i)
c <- assign(p, (x - g[i]))
f <- data.frame(c, f)
}
r <- RDestimate(y~x, data=f)
## Drop the needed initialized column
f <- f %>%
select(-X1)
## Testing Equivalencies
q <- x-g[98]
q == f$c.98
## Initializing Variables for local linear regression loop
models <- list()
f <- f %>%
select(-c)
variables <- setdiff(names(f), c("row_id"))
## Local Linear Regression Loop
for (var in variables) {
models[[var]] = RDestimate(
as.formula(paste0("y ~ ", var)),
data = f
)
}
## Selection Function
selection_criteria <- function(x) {
d <- abs(mean(models[[x]]$est)/mean(models[[x]]$p))
print(d)
}
## Testing
selection_criteria("c.49")
## Creating Cuts Name
cuts <- colnames(f)
## Finding Optimal Cut-point
s <- sapply(cuts, selection_criteria, USE.NAMES = TRUE)
max(s)