Flip sort to calculate p-value
Closed this issue · 1 comments
iangow commented
Line 44 in 3c84896
I think this needs to be arrange(desc(ate))
to get the one-tailed p-value correct. Doesn't matter much (still insignificant). Just FYI, here's alternative (generalized) code (similar to that in #20):
library(tidyverse)
library(haven)
read_data <- function(df)
{
full_path <- paste("https://raw.github.com/scunning1975/mixtape/master/",
df, sep = "")
df <- read_dta(full_path)
return(df)
}
ri <-
read_data("ri.dta") %>%
mutate(id = row_number())
actual_treated <-
ri %>%
filter(d==1) %>%
pull(id)
combo <-
tibble(treated = combn(nrow(ri), sum(ri$d), simplify = FALSE),
permutation = 1:length(treated)) %>%
crossing(ri %>% select(id, y)) %>%
rowwise() %>%
mutate(d = is.element(id, treated))
ates <-
combo %>%
group_by(permutation) %>%
summarize(te1 = sum(d * y, na.rm = TRUE),
te0 = sum((1 - d) * y, na.rm = TRUE)) %>%
mutate(ate = te1 - te0) %>%
arrange(desc(ate)) %>%
mutate(rank = row_number(),
p_value = rank/nrow(.))
actual_permutation <-
combo %>%
rowwise() %>%
filter(setequal(actual_treated, treated)) %>%
select(permutation) %>%
distinct() %>%
pull()
ates %>%
filter(permutation == actual_permutation)
#> # A tibble: 1 x 6
#> permutation te1 te0 ate rank p_value
#> <int> <dbl> <dbl> <dbl> <int> <dbl>
#> 1 1 34 30 4 25 0.357
Created on 2021-02-27 by the reprex package (v1.0.0)
kylebutts commented
Thanks!