1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Odds and probabilities}
\usepackage[utf8]{inputenc}
-->
<a target="_blank" href="https://github.com/richarddmorey/BayesFactor"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Fork me on GitHub"></a>

------
Odds and probabilities using BayesFactor
===============================
Richard D. Morey
-----------------
<div class="social">
<a target="_blank" href="https://bayesfactor.blogspot.co.uk/"><img src="extra/socialmedia/png/48x48/blogger.png" alt="BayesFactor blog" border="0"/><span class="socialtext"> Follow the BayesFactor blog</span></a>
</div>
<div class="socialsep"></div>
Share via<br/>
<span class="social"><a target="_blank" href="https://www.facebook.com/sharer.php?u=https://richarddmorey.github.io/BayesFactor/"><img src="extra/socialmedia/png/32x32/facebook.png" alt="share on facebook"/></a> <a target="_blank" href="https://twitter.com/share?text=Check%20out%20BayesFactor%20for%20Bayesian%20data%20analysis:&url=https://richarddmorey.github.io/BayesFactor/"><img src="extra/socialmedia/png/32x32/twitter.png" alt="tweet BayesFactor"/></a> <a target="_blank" href="https://www.reddit.com/submit?url=https://richarddmorey.github.io/BayesFactor/"><img src="extra/socialmedia/png/32x32/reddit.png" alt="submit to reddit" border="0" /></a> <a target="_blank" href="mailto:?subject=BayesFactor R package&body=Check out the BayesFactor software for Bayesian analysis: https://richarddmorey.github.io/BayesFactor/." title="share by email"><img src="extra/socialmedia/png/32x32/email.png" alt="share by email" border="0" /></a>
</span>
----
```{r echo=FALSE,message=FALSE,results='hide'}
options(markdown.HTML.stylesheet = 'extra/manual.css')
library(knitr)
options(digits=3)
require(graphics)
set.seed(2)
```
```{r message=FALSE,results='hide',echo=FALSE}
library(BayesFactor)
options(BFprogress = FALSE)
bfversion = BFInfo()
session = sessionInfo()[[1]]
rversion = paste(session$version.string," on ",session$platform,sep="")
```
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
```{r}
data(puzzles)
bf = anovaBF(RT ~ shape*color + ID, whichRandom = "ID", data = puzzles)
bf
```
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:
```{r}
prior.odds = newPriorOdds(bf, type = "equal")
prior.odds
```
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:
```{r}
priorOdds(prior.odds) <- c(4,3,2,1)
prior.odds
```
### Posterior odds with BayesFactor
We can multiply the prior odds by the Bayes factor to obtain posterior odds:
```{r}
post.odds = prior.odds * bf
post.odds
```
### Prior/posterior probabilities with BayesFactor
Odds objects can be converted to probabilities:
```{r}
post.prob = as.BFprobability(post.odds)
post.prob
```
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.
```{r}
post.prob / .5
```
In addition, we can select subsets of the probabilities, and the normalizing constant is adjusted to the sum of the model probabilities:
```{r}
post.prob[1:3]
```
...which can, in turn, be renormalized:
```{r}
post.prob[1:3] / 1
```
In the future, the ability to filter these objects will be added, as well as model averaging based on posterior probabilities and samples.
-------
<p>Social media icons by <a href="https://www.awicons.com/">Lokas Software</a>.</p>
*This document was compiled with version `r bfversion` of BayesFactor (`r rversion`).*
|