Created
October 5, 2017 19:00
-
-
Save mcfrank/ef7cb10953eb69345308a608b8f6716a to your computer and use it in GitHub Desktop.
simulation-based power analysis for chi2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(purrr) | |
n1 = 12 | |
n2 = 30 | |
nsims = 10 | |
get_chi2 <- function(x) { | |
p1 <- rbinom(1, size = n1, prob = x$p1) | |
p2 <- rbinom(1, size = n2, prob = x$p2) | |
mat <- matrix(c(p1, n1 - p1, p2, n2 - p2), byrow=TRUE, nrow = 2) | |
x$pval <- chisq.test(mat)$p.val | |
return(x) | |
} | |
sims <- expand.grid(p1 = seq(0,1,.1), | |
p2 = seq(0,1,.1), | |
sim = 1:nsims) %>% | |
as_tibble %>% | |
mutate(index = 1:n()) %>% | |
split(.$index) %>% | |
map_df(get_chi2) | |
sims %>% | |
group_by(p1, p2) %>% | |
summarise(power = mean(pval < .05, na.rm=TRUE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment