Created
January 14, 2016 12:37
-
-
Save Lakens/95c97116dfaa1f3a5672 to your computer and use it in GitHub Desktop.
Bayesian Power Analysis for an Independent t-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Bayesian Power Analysis | |
if(!require(BayesFactor)){install.packages('BayesFactor')} | |
library(BayesFactor) | |
D<-0.0 #Set the true effect size | |
n<-50 #Set sample size of your study (number in each group) | |
nSim<-100000 #Set number of simulations (it takes a while, be patient) | |
rscaleBF<-sqrt(2)/2 #Set effect size of alternative hypothesis (default = sqrt(2)/2, or 0.707) | |
threshold<-3 #Set threshold for 'support' - e.g., 3, 10, or 30 | |
bf<-numeric(nSim) | |
# create progress bar because it might take a while | |
pb <- winProgressBar(title = "progress bar", min = 0, max = nSim, width = 300) | |
for(i in 1:nSim){ #for each simulated experiment | |
setWinProgressBar(pb, i, title=paste(round(i/nSim*100, 1), "% done")) | |
x<-rnorm(n = n, mean = 0, sd = 1) | |
y<-rnorm(n = n, mean = D, sd = 1) | |
bf[i] <- exp((ttestBF(x,y, rscale = rscaleBF))@bayesFactor$bf) | |
} | |
close(pb)#close progress bar | |
supportH0 <- sum(bf<(1/threshold))/nSim | |
supportH1 <- sum(bf>threshold)/nSim | |
cat("The probability of observing support for the null hypothesis is ",supportH0) | |
cat("The probability of observing support for the alternative hypothesis is ",supportH1) | |
hist(log(bf), breaks=20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment