Skip to content

Instantly share code, notes, and snippets.

@steveharoz
Created December 4, 2020 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveharoz/d5995144b01cd751d74d59a9df2d8a94 to your computer and use it in GitHub Desktop.
Save steveharoz/d5995144b01cd751d74d59a9df2d8a94 to your computer and use it in GitHub Desktop.
# simulate true effect sizes vs p in 0.01-0.05 range
library(tidyverse)
SUBJECT_COUNT = 16
TEST_COUNT = 16
# test if the p-value is in the specified range
inRange = function(p) {
(p > 0.01) & (p < 0.05)
}
# simulate one t-test
simulate_one = function(d) {
a = rnorm(SUBJECT_COUNT, 0)
b = rnorm(SUBJECT_COUNT, 0 + d)
t.test(a, b)$p.value
}
# simulate multiple t-tests
simulate_many = function(d) {
replicate(TEST_COUNT, simulate_one(d)) %>% inRange() %>% sum()
}
# run the simulation for a few cohen's d values
simulation_data = expand_grid(
d = c(0, 0.25, 0.5, 0.75, 1, 1.25),
rep_index = 1:10000,
ps_in_range = NA
) %>%
rowwise() %>%
mutate(ps_in_range = simulate_many(d))
#plot it
ggplot(simulation_data %>% mutate(d_label = paste("Cohen's d = ", d))) +
aes(x = ps_in_range, fill = d) +
geom_vline(xintercept = 9, color="red") +
geom_histogram(binwidth = 1) +
scale_x_continuous(expand = c(0,0), breaks = 0:16) + expand_limits(x=16) +
facet_wrap(d_label ~ ., ncol = 1, strip.position = "top") +
theme_light(18) + theme(panel.grid.major = element_blank(), panel.grid.minor.y = element_blank()) +
guides(fill = "none") +
labs(title = "How many simulated t-tests have 0.01 < p < 0.05?",
subtitle = "16 t-tests simulated 10k times for each Cohen's d",
x = NULL, y = NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment