Update prior probabilities of models/hypotheses to posterior probabilities using Bayes factors.

bfactor_to_prob(bf, prior_prob = 0.5)

Arguments

bf

A numeric vector of non-negative values.

prior_prob

A numeric vector with values in the [0,1] interval. If length(bf) == 1 then prior_prob can be of any positive length, but if length(bf) > 1 then the length of prior_prob can only be 1 or equal to the length of bf.

Value

If length(bf) > 1 then bfactor_to_prob returns a numeric vector with the same length as bf, otherwise it returns a numeric vector with the same length as prior_prob. Warning messages are thrown if there are NA or NaN values in bf or in prior_prob.

Details

bfactor_to_prob turns Bayes factors into posterior probabilities using a vectorized version of the following equation from Berger and Delampady (1987) :

$$ P(H_0 | x) = \left( 1 + \frac{1 - \pi_0}{\pi_0} \, \frac{1}{B_{01}(x)} \right)^{-1} $$ where \(B_{01}(x)\) is a Bayes factor in favor of the null hypothesis (given the data \(x\)), \(\pi_0\) is the prior probability of the null hypothesis and \(1 - \pi_0\) is the prior probability of the alternative hypothesis.

If bf is a vector of Bayes factors (in favor of the null hypothesis) and prior_prob is a vector with the prior probabilities of those hypotheses then bfactor_to_prob(bf, prior_prob) updates prior_prob to posterior probabilities. The posterior probabilities of the alternative hypotheses can be obtained with 1 - bfactor_to_prob(bf, prior_prob).

The prior_prob argument is optional and is set to 0.5 by default, implying prior equiprobability of hypotheses. prior_prob can only be of length equal to length(bf), in which case each prior probability in prior_prob will be updated using the corresponding element of bf, or of length 1, in which case it will be recycled (if length(bf) > 1) and each element of bf will update the same prior_prob value.

References

Berger JO, Delampady M (1987). “Testing precise hypotheses.” Statistical Science, 2(3), 317--335.

See also

Examples

# With a Bayes factor that is indifferent between the null # and the alternative hypotheses: # -------------------------------------------------------- bfactor_to_prob(1)
#> [1] 0.5
# Same as above but the null hypothesis has high prior probability: # ----------------------------------------------------------------- bfactor_to_prob(1, .99)
#> [1] 0.99
# Posterior probability of the null hypothesis as a function # of the prior probability: # ----------------------------------------------------------------- bfactor_to_prob(1, seq(.5, 1, .1))
#> [1] 0.5 0.6 0.7 0.8 0.9 1.0
# With Bayes factors that favor the null hypothesis: # ----------------------------------------------------------------- round(bfactor_to_prob(seq(2, 50, 2.5)), 3)
#> [1] 0.667 0.818 0.875 0.905 0.923 0.935 0.944 0.951 0.957 0.961 0.964 0.967 #> [13] 0.970 0.972 0.974 0.975 0.977 0.978 0.979 0.980
# Same as above but the null hypothesis has low prior probability: # ----------------------------------------------------------------- round(bfactor_to_prob(seq(2, 50, 2.5), prior_prob = .01), 3)
#> [1] 0.020 0.043 0.066 0.088 0.108 0.128 0.147 0.165 0.182 0.198 0.214 0.230 #> [13] 0.244 0.258 0.272 0.285 0.298 0.310 0.322 0.333
# Posterior probabilities obtained with Bayes factors that # favor the alternative hypothesis: # ----------------------------------------------------------------- round(bfactor_to_prob(seq(0, 1, .05)), 3)
#> [1] 0.000 0.048 0.091 0.130 0.167 0.200 0.231 0.259 0.286 0.310 0.333 0.355 #> [13] 0.375 0.394 0.412 0.429 0.444 0.459 0.474 0.487 0.500
# Same as above but the null hypothesis has high prior probability: # ----------------------------------------------------------------- round(bfactor_to_prob(seq(0, 1, .05), prior_prob = .99), 3)
#> [1] 0.000 0.832 0.908 0.937 0.952 0.961 0.967 0.972 0.975 0.978 0.980 0.982 #> [13] 0.983 0.985 0.986 0.987 0.988 0.988 0.989 0.989 0.990
# Application: chi-squared goodness-of-fit test, # lower bound on the posterior probability of the null hypothesis: # ----------------------------------------------------------------- x <- matrix(c(12, 41, 25, 33), ncol = 2) bfactor_to_prob(bcal(chisq.test(x)[["p.value"]]), prior_prob = .9)
#> [1] 0.7499935