Skip to content

Instantly share code, notes, and snippets.

@GrantRVD
Created July 19, 2015 18:01
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 GrantRVD/dafff23cc44215b25c15 to your computer and use it in GitHub Desktop.
Save GrantRVD/dafff23cc44215b25c15 to your computer and use it in GitHub Desktop.
Shows the distribution of power v. sample size for several effect sizes with a two-sided paired t-test
library(pwr)
# Set variables
p <- seq(.2, .9, .01)
np <- length(p)
d <- seq(.4, 2.0, .2)
nd <- length(d)
samsize <- array(numeric(np*nd), dim=c(np, nd))
for (i in 1:nd) {
for (j in 1:np) {
result <- power.t.test(n = NULL,
d = d[i],
sig.level = 0.05,
power = p[j],
alternative = "two.sided",
type = "paired"
)
samsize[j, i] <- ceiling(result$n)
}
}
# set up graph
xrange <- range(p)
yrange <- round(range(samsize))
colors <- rainbow(length(d))
plot(xrange, yrange, type="n",
xlab="Power (1 - beta)",
ylab="Sample Size (n)" )
# add power curves
for (i in 1:nd){
lines(p, samsize[,i], type="l", lwd=2, col=colors[i])
}
# add annotation (grid lines, title, legend)
abline(v=0, h=seq(0,yrange[2],50), lty=2, col="grey89")
abline(h=0, v=seq(xrange[1],xrange[2],.02), lty=2,
col="grey89")
title("Sample Size Estimation for Binaural Beat t-tests\n
Sig=0.05 (Two-tailed)")
legend("topleft", title="Effect Size", as.character(d),
fill=colors)
# Save the plot
dev.copy(png, "/home/grant/powersamsize.png")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment