Skip to content

Instantly share code, notes, and snippets.

@tbates
Created October 14, 2012 20:54
Show Gist options
  • Save tbates/3889794 to your computer and use it in GitHub Desktop.
Save tbates/3889794 to your computer and use it in GitHub Desktop.
distribution of p-values at high power
library(MASS)
library(ggplot2)
# move n around to alter sample size
# move r around to alter effect size
n = 1000; r = .5
desiredCovMatrix = matrix(c(1,r,r, 1) ,nrow=2, ncol=2);
count = 1000 # number of replications
out = rep(NA,count) # array to store the results
for (i in 1:count) {
xy = MASS::mvrnorm (n, mu = c(0,0), Sigma = desiredCovMatrix); # simulate data
xy = data.frame(xy); names(xy) <- c("x", "y");
result = cor.test(~x+y, data=xy)
out[i] = result$p.value
}
ggplot2::qplot(out, binwidth=.0001)
max(out) # .01 for n= 100; essentially 0 for n = 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment