Skip to content

Instantly share code, notes, and snippets.

@leeper
Last active August 29, 2015 14:23
Show Gist options
  • Save leeper/b19043a446f885872751 to your computer and use it in GitHub Desktop.
Save leeper/b19043a446f885872751 to your computer and use it in GitHub Desktop.
Statistical Power
# effect sizes
d <- seq(.01, 2, length.out = 1000)
# required sample size at different power levels
n50 <- sapply(x, function(x) power.t.test(n = NULL, delta = x, power = 0.50)$n)
n80 <- sapply(x, function(x) power.t.test(n = NULL, delta = x, power = 0.80)$n) # conventional
n90 <- sapply(x, function(x) power.t.test(n = NULL, delta = x, power = 0.90)$n)
n95 <- sapply(x, function(x) power.t.test(n = NULL, delta = x, power = 0.95)$n)
n99 <- sapply(x, function(x) power.t.test(n = NULL, delta = x, power = 0.99)$n)
# plot
png("power.png", width = 600, height = 600)
plot(NA, las = 1, xlab = "Cohen's d", ylab = "n per group",
xaxt = "n", xaxs = "i", yaxs = "i", xlim = c(0, .9), ylim = c(0,500))
axis(1, seq(0, 1, by = 0.1))
abline(v=c(0.2,0.5,0.8), col = "red", lty = 3)
lines(d, n50, lwd = 1, lty = 1)
lines(d, n80, lwd = 2, lty = 1) # conventional
lines(d, n90, lwd = 1, lty = 2)
lines(d, n95, lwd = 1, lty = 3)
lines(d, n99, lwd = 1, lty = 4)
legend(.55, 300, legend = sprintf("%0.2f", c(0.99, 0.95, 0.9, 0.8, 0.5)),
lty = c(4,3,2,1,1), lwd = c(1,2,1,1,1), bty = "n", title = "Power")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment