Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created October 17, 2017 08:44
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 richarddmorey/1b408eaa608943379e01204fc4333bd5 to your computer and use it in GitHub Desktop.
Save richarddmorey/1b408eaa608943379e01204fc4333bd5 to your computer and use it in GitHub Desktop.
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