Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Created September 28, 2018 02:38
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 johnmyleswhite/19e7bf61cfa5a6d8b0b41736cbd8abae to your computer and use it in GitHub Desktop.
Save johnmyleswhite/19e7bf61cfa5a6d8b0b41736cbd8abae to your computer and use it in GitHub Desktop.
library("pwr")
library("ggplot2")
n_sims <- 1000L
n <- 10L
mu <- 0.5
sigma <- 1
true_power <- power.t.test(
n = n,
delta = mu,
power = NULL,
sd = sigma,
type = "one.sample",
strict = TRUE
)$power
powers <- rep(0.0, n_sims)
p_values <- rep(0.0, n_sims)
for (s in 1L:n_sims) {
x <- rnorm(n, mu, sigma)
p_values[s] <- t.test(x)$p.value
powers[s] <- power.t.test(
n = n,
delta = mean(x),
power = NULL,
sd = sd(x),
type = "one.sample",
strict = TRUE
)$power
}
df <- data.frame(power = powers, p_value = p_values)
ggplot(df, aes(x = power, y = p_value)) +
geom_point() +
geom_vline(xintercept = true_power)
ggplot(df, aes(x = power)) +
geom_density() +
geom_vline(xintercept = true_power)
mean(df$power) / true_power
mean(df$power[df$p_value < 0.05]) / true_power
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment