Skip to content

Instantly share code, notes, and snippets.

@martinmodrak
Created December 6, 2021 10:31
Show Gist options
  • Save martinmodrak/9a5f595cfdcfe2d760bb40d234f177f6 to your computer and use it in GitHub Desktop.
Save martinmodrak/9a5f595cfdcfe2d760bb40d234f177f6 to your computer and use it in GitHub Desktop.
Simulation check to show halving p-values for chi-squared test _could_ be a valid statistical procedure
library(ggplot2)
N_treatment <- 709
N_control <- 699
N_sims <- 1e4
prob_event <- 0.1
# Assuming prob(event|treatment) = prob(event|control)
# The case prob(event|treatment) > prob(event|control) produces fewer false positives (type-I errors)
pos_control <- rbinom(N_sims, size = N_control, prob = prob_event)
pos_treatment <- rbinom(N_sims, size = N_treatment, prob = prob_event)
p_values <- array(NA_real_, N_sims)
false_positives <- data.frame(alpha = seq(0.01, 0.1, by = 0.01),
fp_direct = 0, fp_halved = 0)
for(i in 1:N_sims) {
cases_table <- matrix(c(pos_control[i], N_control - pos_control[i], pos_treatment[i], N_treatment - pos_treatment[i]), ncol = 2)
if(pos_treatment[i] >= pos_control[i]) {
# Effect in the null direction, don't reject, no false positives
next
} else {
p_values[i] <- chisq.test(cases_table, correct = FALSE)$p.value
reject_direct <- p_values[i] < false_positives$alpha
reject_halved <- p_values[i] / 2 < false_positives$alpha
false_positives$fp_direct <- false_positives$fp_direct + reject_direct
false_positives$fp_halved <- false_positives$fp_halved + reject_halved
}
}
ggplot(false_positives, aes(x = alpha, y = fp_direct/N_sims)) + geom_point() + geom_abline(color = "blue")
ggplot(false_positives, aes(x = alpha, y = fp_halved/N_sims)) + geom_point() + geom_abline(color = "blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment