Skip to content

Instantly share code, notes, and snippets.

@ito4303
Last active March 24, 2018 00:32
Show Gist options
  • Save ito4303/44422e3eb9abe7d00b120f9788294a62 to your computer and use it in GitHub Desktop.
Save ito4303/44422e3eb9abe7d00b120f9788294a62 to your computer and use it in GitHub Desktop.
A simulation incrementing sample size until p < 0.05
library(purrr)
sim_inc_sample <- function(N_start = 20, N_end = 100,
mu = 0, sigma = 1, sig = 0.05) {
not_sig <- TRUE
N <- N_start
x1 <- rnorm(N_end, mu, sigma)
x2 <- rnorm(N_end, mu, sigma)
while(not_sig & N <= N_end) {
p_value <- t.test(x1[1:N], x2[1:N])$p.value
if (p_value < sig) {
not_sig <- FALSE
} else {
N <- N + 1
}
}
return(N)
}
set.seed(20180324)
R <- 1000
N_start <- 20
N_end <- 100
n <- purrr::map_dbl(seq_len(R), ~ sim_inc_sample(N_start, N_end))
sum(n == N_start) / R
sum(n <= N_end) / R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment