Skip to content

Instantly share code, notes, and snippets.

@Lakens
Last active August 29, 2015 14:06
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 Lakens/1afffd4c8237f94b821b to your computer and use it in GitHub Desktop.
Save Lakens/1afffd4c8237f94b821b to your computer and use it in GitHub Desktop.
Bayes Factors and p-values for independent t-test
#####
p1 <-numeric(200) #set up empty container for all p-values
bf1 <-numeric(200) #set up empty container for all bf
p2 <-numeric(200) #set up empty container for all p-values
bf2 <-numeric(200) #set up empty container for all bf
p3 <-numeric(200) #set up empty container for all p-values
bf3 <-numeric(200) #set up empty container for all bf
t <- 1.96 #set start point t-value
for (i in 1:201)
{
t<-t+0.01
bf1[i]<-exp(-ttest.tstat(t,20,20,rscale=1)$bf) # calc BF
p1[i]<-2*pt(-abs(t),df=40-2) #calc p-value
bf2[i]<-exp(-ttest.tstat(t,50,50,rscale=1)$bf)
p2[i]<-2*pt(-abs(t),df=100-2)
bf3[i]<-exp(-ttest.tstat(t,100,100,rscale=1)$bf)
p3[i]<-2*pt(-abs(t),df=200-2)
}
#Below is a lot of syntax to draw the plot
plot(bf3,type='l', xaxt="n", xlab="t-values", ylab="Bayes Factor")
tvalues<-seq(1.96, 3.96, by=0.01)
axis(1, 1:201, side = 1, at = c(1, 50, 100, 150, 200), labels = c("1.96", "2.46", "2.96", "3.46", "3.96"))
legend(150,1.4, # places a legend at the appropriate place
c("N = 100","N = 50", "N = 20"), # puts text in the legend
lty=c(1,1), # gives the legend appropriate symbols (lines)
lwd=c(2.5,2.5,2.5),col=c("black", "red","blue")) # format lines and colors
#draw some horizontal and vertical lines
abline(h=0.1, col="azure3", lty=5)
abline(h=0.33, col="azure4", lty=5)
abline(v=7, col="blue", lty=3)
abline(v=3, col="red", lty=3)
abline(v=2, lty=3)
lines(bf2, col='red')
lines(bf1, ,col='blue')
#Draw p-value plot
plot(p3,type='l', xaxt="n", xlab="t-values", ylab="p-values")
tvalues<-seq(1.96, 3.96, by=0.01)
axis(1, 1:201, side = 1, at = c(1, 50, 100, 150, 200), labels = c("1.96", "2.46", "2.96", "3.46", "3.96"))
legend(150,0.045, # places a legend at the appropriate place
c("N = 100","N = 50", "N = 20"), # puts text in the legend
lty=c(1,1), # gives the legend appropriate symbols (lines)
lwd=c(2.5,2.5,2.5),col=c("black", "red","blue")) # format lines and colors
lines(p2, col='red')
lines(p1, col='blue')
abline(h=0.05, col="azure4", lty=5)
abline(h=0.01, col="azure3", lty=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment