Created
January 24, 2019 22:31
-
-
Save mcfrank/d67a03b38ff71baa15ce6bb0c2874af3 to your computer and use it in GitHub Desktop.
Simulation for manybabies 2 pilot
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(ggthemes) | |
n_trials <- c(2,4,8) | |
p_anticipation <- seq(0,1,.1) | |
n_subs <- c(12,24,36,48) | |
n_sims <- 1000 | |
d <- expand.grid(n_trials = n_trials, | |
p_anticipation = p_anticipation, | |
n_subs = n_subs, | |
sim = 1:n_sims) %>% | |
as_tibble %>% | |
mutate(idx = 1:n()) %>% | |
split(.$idx) %>% | |
map_df(function(params) { | |
data <- rbinom(n = params$n_subs, size = params$n_trials, prob = params$p_anticipation) | |
params$m <- mean(data/params$n_trials) | |
return(params) | |
}) %>% | |
group_by(p_anticipation, n_subs, n_trials) %>% | |
summarise(ci_lower = quantile(m, .025), | |
ci_upper = quantile(m, .975), | |
m = mean(m)) | |
ggplot(d, aes(x = p_anticipation, y = m, col = factor(n_subs))) + | |
geom_line() + | |
geom_pointrange(aes(ymin = ci_lower, | |
ymax = ci_upper)) + | |
scale_color_ptol(name = "N")+ | |
facet_grid(n_trials~n_subs) + | |
xlab("True mean anticipation") + | |
ylab("Simulated mean anticipation") + | |
theme(legend.position = "bottom") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment