Finding the Bayes factor using Stan

Hi everyone,

I want to calculate the Bayes factor to do some hypothesis testing for the models that I am fitting using Stan. But based on this post, calculating Bayes factor directly using rstan seems to be not a straight forward thing.

Also I have seen several posts regarding calculating the Bayes factor using some packages like brms, rstanarm which directly use Stan.

So will it be possible to find the Bayes factor directly using Stan?
Because I may need to rely on Stan directly to fit models rather than using some built in packages.

Any suggestion will be highly helpful. Thank you.

I think both of these packages use bridgesampling, and I suggest you do too :).

@hhau Thank you for the suggestion. So is it possible to use Stan and bridgesampling package together(may be something like using the Stan objects inside bridgesampling)? Because I may need to use Stan for modelling.

Yes, you can.
You just need to use the unnormalized probability functions (e.g., use target += normal_lpdf(theta | 0, 1) instead of theta ~ normal(0,1)).

Depending on your model comparison or hypothesis testing goals, you may also be able to use the savage-dickey method, but bridgesampling is more general.

3 Likes

There is a vignette on precisely this topic: https://cran.r-project.org/web/packages/bridgesampling/vignettes/bridgesampling_example_stan.html

1 Like