Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Inverse function of Bayes factor for t test
## Given a (scaled Cauchy) Bayes factor for the null against the
## alternative (Rouder et al 2009), yields the t statistic
## that would yield it. The ... arguments are passed to
## the ttest.tstat function.
bf.inv = Vectorize(function(b10, ...){
fn = Vectorize(function(t,...){
BayesFactor::ttest.tstat(t,...)[["bf"]]
}, "t")
t0 = optimize(function(t0, ...){
t = sqrt(t0 / (1 - t0))
b = fn(t, ...)
(b10 - exp(b))^2
}, c(0,1), ...)$minimum
sqrt(t0 / (1 - t0))
},"b10")
q = bf.inv(1/3, n1 = 20, rscale = sqrt(2)/2)
## Test it (should be 1/3)
BayesFactor::ttest.tstat(q, n1 = 20, rscale = sqrt(2)/2, simple = TRUE)
## Make a plot
b = seq(.05,20, len = 50)
plot(b, bf.inv(b, n1 = 20), ty = 'l', ylab = "t statistic",xlab = "Bayes factor", log = "x")
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.