Share via
Stable version: CRAN page - Package NEWS (including version changes)
Development version: Development page - Development package NEWS
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.
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.
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 |
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!) |
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.
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