Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bayesian Power Analysis for an Independent t-test
#Bayesian Power Analysis
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
# 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
You can’t perform that action at this time.