Skip to content

Instantly share code, notes, and snippets.

@JimGrange
Last active December 16, 2020 11:23
Show Gist options
  • Save JimGrange/9ba16e2aa515b8e9913cc55c07f57c63 to your computer and use it in GitHub Desktop.
Save JimGrange/9ba16e2aa515b8e9913cc55c07f57c63 to your computer and use it in GitHub Desktop.
Power analysis when true effect larger than planned effect
library(pwr)
library(effsize)
library(ggplot2)
# set "true" effect size and the effect size entered into power analysis
true_effect <- 0.2
power_effect <- 1.0
# How many participants required?
power_analysis <- pwr.t.test(d = power_effect,
sig.level = 0.05,
power = 0.8,
type = "two.sample")
sample_size <- round(power_analysis$n, 0)
# how many simulations?
n_sims <- 50
# prepare data frame
eff_sizes <- data.frame(
simulation = 1:n_sims,
mean = numeric(n_sims),
lower = numeric(n_sims),
upper = numeric(n_sims)
)
# conduct the simulations
for(i in 1:n_sims){
# simulate the data fpr 2 groups
sample_a <- rnorm(sample_size, 0 + true_effect, 1)
sample_b <- rnorm(sample_size, 0, 1)
# run the t-test
t_test <- t.test(sample_a, sample_b)
# get the effect size & confidence intervals
d <- cohen.d(sample_a, sample_b)
eff_sizes$mean[i] <- d$estimate
eff_sizes$lower[i] <- d$conf.int[1]
eff_sizes$upper[i] <- d$conf.int[2]
}
# produce the plot
ggplot(eff_sizes, aes(x = mean, y = simulation)) +
geom_vline(xintercept = true_effect, colour = "red") +
geom_segment(aes(x = lower, xend = upper, y = simulation, yend = simulation)) +
geom_point() +
theme_bw() +
labs(x = "Mean Estimate",
y = "Simulation Number") +
scale_x_continuous(limits = c(-1.5, 1.5)) +
coord_flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment