Fork me on GitHub

BayesFactor logo


Using the 'BayesFactor' package, version 0.9.2+

Richard D. Morey

BayesFactor on facebook Find us on facebook
BayesFactor blog Follow the BayesFactor blog

Share via
share on facebook tweet BayesFactor submit to reddit submit to google plus bookmark on delicious share by email


Stable version: CRAN page - Package NEWS (including version changes)

Development version: Development page - Development package NEWS

fork on github on github

Table of Contents

Getting help

Introduction

The BayesFactor package enables the computation of Bayes factors in standard designs, such as one- and two- sample designs, ANOVA designs, and regression. The Bayes factors are based on work spread across several papers. This document is designed to show users how to compute Bayes factors using the package by example. It is not designed to present the models used in the comparisons in detail; for that, see the BayesFactor help and especially the references listed in this manual. Complete references are given at the end of this document.

If you need help or think you've found a bug, please use the links at the top of this document to contact the developers. When asking a question or reporting a bug, please send example code and data, the exact errors you're seeing (a cut-and-paste from the R console will work) and instructions for reproducing it. Also, report the output of BFInfo() and sessionInfo(), and let us know what operating system you're running.

Loading the package

The BayesFactor package must be installed and loaded before it can be used. Installing the package can be done in several ways and will not be covered here. Once it is installed, use the library function to load it:

library(BayesFactor)

This command will make the BayesFactor package ready to use.

Some useful functions

The table below lists some of the functions in the BayesFactor package that will be demonstrated in this manual. For more complete help on the use of these functions, see the corresponding help() page in R.

Function Description
ttestBF Bayes factors for one- and two- sample designs
anovaBF Bayes factors comparing many ANOVA models
regressionBF Bayes factors comparing many linear regression models
generalTestBF Bayes factors for all restrictions on a full model (0.9.4+)
lmBF Bayes factors for specific linear models (ANOVA or regression)
proportionBF Bayes factors for tests of single proportions
contingencyTableBF Bayes factors for contingency tables
posterior Sample from the posterior distribution of the numerator of a Bayes factor object
recompute Recompute a Bayes factor or MCMC chain, possibly increasing the precision of the estimate
compare Compare two models; typically used to compare two models in BayesFactor MCMC objects

Functions to manipulate Bayes factor objects

The t test section below has examples showing how to manipulate Bayes factor objects, but all these functions will work with Bayes factors generated from any function in the BayesFactor package.

Function Description
/ Divide two Bayes factor objects to create new model comparisons, or invert with 1/
t “Flip” (transpose) a Bayes factor object
c Concatenate two Bayes factor objects together, assuming they have the same denominator
[ Use indexing to select a subset of the Bayes factors
plot plot a Bayes factor object
sort Sort a Bayes factor object
is.na Determine whether a Bayes factor object contains missing values
head,tail Return the n highest or lowest Bayes factor in an object
max, min Return the highest or lowest Bayes factor in an object
which.max,which.min Return the index of the highest or lowest Bayes factor
as.vector Convert to a simple vector (denominator will be lost!)
as.data.frame Convert to data.frame (denominator will be lost!)

One- and two-sample designs (t tests)

The ttestBF function is used to obtain Bayes factors corresponding to tests of a single sample's mean, or tests that two independent samples have the same mean.

One-sample tests (and paired)

We use the sleep data set in R to demonstrate a one-sample t test. This is a paired design; for details about the data set, see ?sleep. One way of analyzing these data is to compute difference scores by subtracting a participant's score in one condition from their score in the other:

data(sleep)

## Compute difference scores
diffScores = sleep$extra[1:10] - sleep$extra[11:20]

## Traditional two-tailed t test
t.test(diffScores)
## 
##  One Sample t-test
## 
## data:  diffScores
## t = -4, df = 9, p-value = 0.003
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -2.46 -0.70
## sample estimates:
## mean of x 
##     -1.58

We can do a Bayesian version of this analysis using the ttestBF function, which performs the “JZS” t test described by Rouder, Speckman, Sun, Morey, and Iverson (2009). In this model, the true standardized difference \( \delta=(\mu-\mu_0)/\sigma_\epsilon\) is assumed to be 0 under the null hypothesis, and \(\text{Cauchy}(\text{scale}=r)\) under the alternative. The default \(r\) scale in BayesFactor for t tests is \(\sqrt{2}/2\). See ?ttestBF for more details.

bf = ttestBF(x = diffScores)
## Equivalently:
## bf = ttestBF(x = sleep$extra[1:10],y=sleep$extra[11:20], paired=TRUE)
bf
## Bayes factor analysis
## --------------
## [1] Alt., r=0.707 : 17.3 ±0%
## 
## Against denominator:
##   Null, mu = 0 
## ---
## Bayes factor type: BFoneSample, JZS

The bf object contains the Bayes factor, and shows the numerator and denominator models for the Bayes factor comparison. In our case, the Bayes factor for the comparison of the alternative versus the null is 17.259. After the Bayes factor is a proportional error estimate on the Bayes factor.

There are a number of operations we can perform on our Bayes factor, such as taking the reciprocal:

1 / bf
## Bayes factor analysis
## --------------
## [1] Null, mu=0 : 0.0579 ±0%
## 
## Against denominator:
##   Alternative, r = 0.707106781186548, mu =/= 0 
## ---
## Bayes factor type: BFoneSample, JZS

or sampling from the posterior of the numerator model:

chains = posterior(bf, iterations = 1000)
summary(chains)
## 
## Iterations = 1:1000
## Thinning interval = 1 
## Number of chains = 1 
## Sample size per chain = 1000 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##        Mean     SD Naive SE Time-series SE
## mu    -1.42  0.436   0.0138         0.0154
## sig2   2.02  1.157   0.0366         0.0395
## delta -1.11  0.427   0.0135         0.0162
## g      6.26 58.623   1.8538         1.8538
## 
## 2. Quantiles for each variable:
## 
##         2.5%    25%   50%    75%  97.5%
## mu    -2.289 -1.705 -1.43 -1.141 -0.597
## sig2   0.744  1.270  1.69  2.446  5.223
## delta -1.973 -1.383 -1.08 -0.813 -0.347
## g      0.176  0.592  1.13  2.928 33.734

The posterior function returns a object of type BFmcmc, which inherits the methods of the mcmc class from the coda package. We can thus use summary, plot, and other useful methods on the result of posterior. If we were unhappy with the number of iterations we sampled for chains, we can recompute with more iterations, and then plot the results:

chains2 = recompute(chains, iterations = 10000)
plot(chains2[,1:2])