library(BayesFactor)
## Loading required package: coda
## Loading required package: lattice
## Loading required package: Matrix
## ************
## Welcome to BayesFactor 0.9.11. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
data(puzzles)
bf = anovaBF(RT ~ shape*color + ID, whichRandom = "ID", data = puzzles)
bf
## Bayes factor analysis
## --------------
## [1] shape + ID                       : 2.799916 ±1.41%
## [2] color + ID                       : 2.906147 ±1.39%
## [3] shape + color + ID               : 11.97147 ±1.62%
## [4] shape + color + shape:color + ID : 4.252932 ±1.99%
## 
## 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”, but we can discuss more ways of generating these. In the near future, support for changing these element by element will be added.

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                       : 2.799916 ±1.41%
## [2] color + ID                       : 2.906147 ±1.39%
## [3] shape + color + ID               : 11.97147 ±1.62%
## [4] shape + color + shape:color + ID : 4.252932 ±1.99%
## 
## Against denominator:
##   RT ~ ID 
## ---
## Model type: BFlinearModel, JZS

This posterior odds object can be converted to probabilities:

post.prob = as.BFprobability(post.odds)
post.prob
## Posterior probabilities
## --------------
## [1] shape + ID                       : 0.1221046 ±NA%
## [2] color + ID                       : 0.1267374 ±NA%
## [3] shape + color + ID               : 0.5220771 ±NA%
## [4] shape + color + shape:color + ID : 0.1854708 ±NA%
## [5] ID                               : 0.0436101 ±NA%
## 
## Normalized probability:  1  
## ---
## Model type: BFlinearModel, JZS

By default the probabilities sum to 1, but we can change this by renormalizing:

post.prob / .5
## Posterior probabilities
## --------------
## [1] shape + ID                       : 0.0610523  ±NA%
## [2] color + ID                       : 0.06336868 ±NA%
## [3] shape + color + ID               : 0.2610386  ±NA%
## [4] shape + color + shape:color + ID : 0.09273541 ±NA%
## [5] ID                               : 0.02180505 ±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.1221046 ±NA%
## [2] color + ID         : 0.1267374 ±NA%
## [3] shape + color + ID : 0.5220771 ±NA%
## 
## Normalized probability:  0.7709191  
## ---
## Model type: BFlinearModel, JZS

…which can, in turn, be renormalized:

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

Soon, filtering functions will be added, and the odds/probability objects will be joined with the posterior sampling routines to enable completely painless model averaging (just sample the posterior probability object!).