Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Methods ###
- flip <- function(p){
- sample(0:1, 1, prob = c(1 - p, p))
- }
- flip_n <- function(prob, n){
- res= NULL
- for(i in 1:n){
- res = rbind(res, flip(prob))
- }
- return(res)
- }
- flip_til_sig <- function(prob, a = 0.05, n_min = 10, n_max = 100){
- res = NULL
- p = 1
- for(i in 1: n_min){
- res = rbind(res, flip(prob))
- }
- while(p > a & length(res) <= n_max){
- res = rbind(res, flip(prob))
- p = binom.test(sum(res), length(res), p = 0.5)$p.value
- }
- return(res)
- }
- ### Simulation ###
- nsim = 1e3
- res1 = replicate(nsim, flip_n(prob = 0.5, n = 100), simplify = F)
- res2 = replicate(nsim, flip_til_sig (prob = 0.5, a = 0.05, n_min = 10, n_max = 100), simplify = F)
- par(mfrow = c(2, 1))
- hist(sapply(res1, mean), breaks = 20, col = "Grey", xlab = "% Success", main = "Collect N datapoints")
- hist(sapply(res2, mean), breaks = 20, col = "Grey", xlab = "% Success", main = "Collect until stopping rule")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement