-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy paththornton_ri.R
More file actions
65 lines (48 loc) · 1.19 KB
/
Copy paththornton_ri.R
File metadata and controls
65 lines (48 loc) · 1.19 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
library(tidyverse)
library(haven)
read_data <- function(df)
{
full_path <- paste("https://github.com/scunning1975/mixtape/raw/master/",
df, sep = "")
df <- read_dta(full_path)
return(df)
}
hiv <- read_data("thornton_hiv.dta")
# creating the permutations
tb <- NULL
permuteHIV <- function(df, random = TRUE){
tb <- df
# Number of treated in dataset
n_treated <- 2222
n_control <- nrow(tb) - n_treated
if(random == TRUE){
tb <- tb %>%
sample_frac(1) %>%
mutate(any = c(rep(1, n_treated), rep(0, n_control)))
}
te1 <- tb %>%
filter(any == 1) %>%
pull(got) %>%
mean(na.rm = TRUE)
te0 <- tb %>%
filter(any == 0) %>%
pull(got) %>%
mean(na.rm = TRUE)
ate <- te1 - te0
return(ate)
}
permuteHIV(hiv, random = FALSE)
iterations <- 1000
permutation <- tibble(
iteration = c(seq(iterations)),
ate = as.numeric(
c(permuteHIV(hiv, random = FALSE), map(seq(iterations-1), ~permuteHIV(hiv, random = TRUE)))
)
)
#calculating the p-value
permutation <- permutation %>%
arrange(-ate) %>%
mutate(rank = seq(iterations))
p_value <- permutation %>%
filter(iteration == 1) %>%
pull(rank)/iterations