Fork me on GitHub

BayesFactor logo


Odds and probabilities using BayesFactor

Richard D. Morey

BayesFactor blog Follow the BayesFactor blog

Share via
share on facebook tweet BayesFactor submit to reddit share by email


The Bayes factor is only one part of Bayesian model comparison. The Bayes factor represents the relative evidence between two models – that is, the change in the model odds due to the data – but the odds are what are being changed. For any two models \({\cal M}_0\) and \({\cal M}_1\) and data \(y\),

\[ \frac{P({\cal M}_1\mid y)}{P({\cal M}_0\mid y)} = \frac{P(y \mid {\cal M}_1)}{P(y\mid{\cal M}_0)} \times\frac{P({\cal M}_1)}{P({\cal M}_0)}; \] that is, the posterior odds are equal to the Bayes factor times the prior odds.

Further, these odds can be converted to probabilities, if we assume that all the models sum to known probability.

Prior odds with BayesFactor

data(puzzles)
bf = anovaBF(RT ~ shape*color + ID, whichRandom = "ID", data = puzzles)
bf
## Bayes factor analysis
## --------------
## [1] shape + ID                       : 2.89 ±1.63%
## [2] color + ID                       : 2.8  ±0.85%
## [3] shape + color + ID               : 11.6 ±1.48%
## [4] shape + color + shape:color + ID : 4.24 ±3.1%
## 
## Against denominator:
##   RT ~ ID 
## ---
## Bayes factor type: BFlinearModel, JZS

With the addition of BFodds objects, we can compute prior and posterior odds. A prior odds object can be created from the structure of an existing BayesFactor object:

prior.odds = newPriorOdds(bf, type = "equal")
prior.odds
## Prior odds
## --------------
## [1] shape + ID                       : 1
## [2] color + ID                       : 1
## [3] shape + color + ID               : 1
## [4] shape + color + shape:color + ID : 1
## 
## Against denominator:
##   RT ~ ID 
## ---
## Model type: BFlinearModel, JZS

For now, the only type of prior odds is “equal”. However, we can change the prior odds to whatever we like with the priorOdds function:

priorOdds(prior.odds) <- c(4,3,2,1)
prior.odds
## Prior odds
## --------------
## [1] shape + ID                       : 4
## [2] color + ID                       : 3
## [3] shape + color + ID               : 2
## [4] shape + color + shape:color + ID : 1
## 
## Against denominator:
##   RT ~ ID 
## ---
## Model type: BFlinearModel, JZS

Posterior odds with BayesFactor

We can multiply the prior odds by the Bayes factor to obtain posterior odds:

post.odds = prior.odds * bf
post.odds
## Posterior odds
## --------------
## [1] shape + ID                       : 11.6 ±1.63%
## [2] color + ID                       : 8.4  ±0.85%
## [3] shape + color + ID               : 23.2 ±1.48%
## [4] shape + color + shape:color + ID : 4.24 ±3.1%
## 
## Against denominator:
##   RT ~ ID 
## ---
## Model type: BFlinearModel, JZS

Prior/posterior probabilities with BayesFactor

Odds objects can be converted to probabilities:

post.prob = as.BFprobability(post.odds)
post.prob
## Posterior probabilities
## --------------
## [1] shape + ID                       : 0.239  ±NA%
## [2] color + ID                       : 0.174  ±NA%
## [3] shape + color + ID               : 0.479  ±NA%
## [4] shape + color + shape:color + ID : 0.0875 ±NA%
## [5] ID                               : 0.0207 ±NA%
## 
## Normalized probability:  1  
## ---
## Model type: BFlinearModel, JZS

By default the probabilities sum to 1, but we can change this by renormalizing. Note that this normalizing constant is arbitrary, but it can be helpful to set it to specific values.

post.prob / .5
## Posterior probabilities
## --------------
## [1] shape + ID                       : 0.12   ±NA%
## [2] color + ID                       : 0.0868 ±NA%
## [3] shape + color + ID               : 0.24   ±NA%
## [4] shape + color + shape:color + ID : 0.0438 ±NA%
## [5] ID                               : 0.0103 ±NA%
## 
## Normalized probability:  0.5  
## ---
## Model type: BFlinearModel, JZS

In addition, we can select subsets of the probabilities, and the normalizing constant is adjusted to the sum of the model probabilities:

post.prob[1:3]
## Posterior probabilities
## --------------
## [1] shape + ID         : 0.239 ±NA%
## [2] color + ID         : 0.174 ±NA%
## [3] shape + color + ID : 0.479 ±NA%
## 
## Normalized probability:  0.892  
## ---
## Model type: BFlinearModel, JZS

…which can, in turn, be renormalized:

post.prob[1:3] / 1
## Posterior probabilities
## --------------
## [1] shape + ID         : 0.268 ±NA%
## [2] color + ID         : 0.195 ±NA%
## [3] shape + color + ID : 0.537 ±NA%
## 
## Normalized probability:  1  
## ---
## Model type: BFlinearModel, JZS

In the future, the ability to filter these objects will be added, as well as model averaging based on posterior probabilities and samples.


Social media icons by Lokas Software.

This document was compiled with version 0.9.12-4.4 of BayesFactor (R version 4.2.1 (2022-06-23 ucrt) on x86_64-w64-mingw32).