alt text

Interactive BayesFactor objects

Richard D. Morey

The BayesFactor package allows the computation of Bayes factors for a variety of designs. The knitr package allows the easy creation of HTML reports, such as this one. Using the two together, we can create HTML documents that contain our Bayes factor analyses, such as the one below:

data(puzzles)
bf = generalTestBF(RT ~ shape*color*ID, whichRandom="ID", data = puzzles)

If we simply print the bf object, we will see the same output we would get in R. However, unlike in R, we cannot manipulate this object because it is a part of the static webpage. If the comparison model in the denominator is uninteresting, or we would simply like to see a different comparison, we would have to manually do the necessary arithmetic.

bf
## Bayes factor analysis
## --------------
## [1] shape                                                                    : 0.6114754 ±0.01%
## [2] color                                                                    : 0.6114754 ±0.01%
## [3] shape + color                                                            : 0.3765014 ±0.83%
## [4] shape + color + shape:color                                              : 0.1403179 ±1.13%
## [5] ID                                                                       : 111516.6  ±0%
## [6] shape + ID                                                               : 318685.3  ±1.39%
## [7] color + ID                                                               : 323511.6  ±1.39%
## [8] shape + color + ID                                                       : 1356999   ±1.6%
## [9] shape + color + shape:color + ID                                         : 493697.8  ±1.97%
## [10] shape + ID + shape:ID                                                   : 28274.84  ±3.37%
## [11] shape + color + ID + shape:ID                                           : 175601.1  ±3.55%
## [12] shape + color + shape:color + ID + shape:ID                             : 66462.56  ±4.25%
## [13] color + ID + color:ID                                                   : 12069.88  ±3.99%
## [14] shape + color + ID + color:ID                                           : 56355.33  ±4.32%
## [15] shape + color + shape:color + ID + color:ID                             : 23117.15  ±4.9%
## [16] shape + color + ID + shape:ID + color:ID                                : 9475.649  ±7.96%
## [17] shape + color + shape:color + ID + shape:ID + color:ID                  : 4076.389  ±9.19%
## [18] shape + color + shape:color + ID + shape:ID + color:ID + shape:color:ID : 113900    ±8.07%
## 
## Against denominator:
##   Intercept only 
## ---
## Bayes factor type: BFlinearModel, JZS

Although this is still useful to see the numerical values of the Bayes factors, it would be useful to be able to manipulate this object in a similar way to how we can within R. The BayesFactorExtras package allows this. If we load the BayesFactorExtras package inside our knitr-compiled R markdown document, then we get an interactive table containing the Bayes factors. The table is sortable, filterable, and a new comparison model can be changed by simply clicking on a row. Hover over the ‘?’ next to the search box for more help.

library(BayesFactorExtras)

bf

[{"bf":0,"error":0,"time":"Mon Mar 16 11:03:54 2015","code":"fa772db10148","index":0,"row":"Intercept only"},{"bf":-0.4919,"error":0.0001,"time":"Mon Mar 16 11:03:50 2015","code":"fa7746189eb","index":1,"row":"shape"},{"bf":-0.4919,"error":0.0001,"time":"Mon Mar 16 11:03:50 2015","code":"fa771fa5a58c","index":2,"row":"color"},{"bf":-0.9768,"error":0.0083,"time":"Mon Mar 16 11:03:50 2015","code":"fa7779a43d00","index":3,"row":"shape + color"},{"bf":-1.9638,"error":0.0113,"time":"Mon Mar 16 11:03:50 2015","code":"fa77476f8b39","index":4,"row":"shape + color + shape:color"},{"bf":11.6219,"error":1.1322e-06,"time":"Mon Mar 16 11:03:50 2015","code":"fa776c1d6fd2","index":5,"row":"ID"},{"bf":12.672,"error":0.0139,"time":"Mon Mar 16 11:03:50 2015","code":"fa771ad079ac","index":6,"row":"shape + ID"},{"bf":12.687,"error":0.0139,"time":"Mon Mar 16 11:03:50 2015","code":"fa7773227305","index":7,"row":"color + ID"},{"bf":14.1208,"error":0.016,"time":"Mon Mar 16 11:03:50 2015","code":"fa7736d06ab2","index":8,"row":"shape + color + ID"},{"bf":13.1097,"error":0.0197,"time":"Mon Mar 16 11:03:51 2015","code":"fa772a629098","index":9,"row":"shape + color + shape:color + ID"},{"bf":10.2497,"error":0.0337,"time":"Mon Mar 16 11:03:51 2015","code":"fa77202fd179","index":10,"row":"shape + ID + shape:ID"},{"bf":12.076,"error":0.0355,"time":"Mon Mar 16 11:03:51 2015","code":"fa7751db75dc","index":11,"row":"shape + color + ID + shape:ID"},{"bf":11.1044,"error":0.0425,"time":"Mon Mar 16 11:03:51 2015","code":"fa7718fa3b72","index":12,"row":"shape + color + shape:color + ID + shape:ID"},{"bf":9.3985,"error":0.0399,"time":"Mon Mar 16 11:03:51 2015","code":"fa7799edf9c","index":13,"row":"color + ID + color:ID"},{"bf":10.9394,"error":0.0432,"time":"Mon Mar 16 11:03:52 2015","code":"fa773ae5cfdb","index":14,"row":"shape + color + ID + color:ID"},{"bf":10.0483,"error":0.049,"time":"Mon Mar 16 11:03:52 2015","code":"fa777c41987c","index":15,"row":"shape + color + shape:color + ID + color:ID"},{"bf":9.1565,"error":0.0796,"time":"Mon Mar 16 11:03:52 2015","code":"fa771e4cceae","index":16,"row":"shape + color + ID + shape:ID + color:ID"},{"bf":8.313,"error":0.0919,"time":"Mon Mar 16 11:03:53 2015","code":"fa7716924e01","index":17,"row":"shape + color + shape:color + ID + shape:ID + color:ID"},{"bf":11.6431,"error":0.0807,"time":"Mon Mar 16 11:03:54 2015","code":"fa772d35b9b0","index":18,"row":"shape + color + shape:color + ID + shape:ID + color:ID + shape:color:ID"}]
JZS
BFlinearModel
When compared against the model ...

Enter one or more search terms in the box to filter the models in the table. If more than one term is included, matching will be performed with or. Special search terms are allowed:

Code Function Example What example does
+ Require this term in search results +shape Requires all models to include “shape”
- Require this term NOT to appear in search results -shape Requires all models to exclude “shape”
# (with number) Return results with certain number of terms #2 Requires all models to have two terms
@ (with :, ::, …) Return results containing interactions of certain size @:: Requires all models to have a three-way interaction
< or > Limits sizes of Bayes factors >2 Returns models whose Bayes factor is greater than 2

Click on a row in the Bayes factor table to make that model the denominator. Sort by clicking on the arrows in the column headers.

 
...the model below... ...is preferred by...

Red rows indicate models that perform worse than the comparison model, and blue rows indicate models that perform better. Note that by default, the BayesFactorExtras package will include a link for anyone to download the BayesFactor object (see above). This object contains the data, and will allow anyone to recompute the Bayes factors in the document. If you want to disable the possibility of downloading the data, turn the option off first with:

options(BFEknitrDownload = FALSE)

Objects printed after this option is turned off will not contain a link to download the data.

The same interactive display can be had within R by using the interactBF() function on the BayesFactor object, like so:

interactBF(bf)

This document was compiled with version 0.9.11 of BayesFactor and 0.1 of BayesFactorExtras (R version 3.1.3 (2015-03-09) on x86_64-apple-darwin13.4.0)