Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

BayesFactor R Package: Functions, Install and Worked Examples

BayesFactor R package: install it, pick the right function for your design, run a t test and ANOVA, and read the Bayes factor output row by row.

Written and maintained by CASRAI Editorial Board

Last updated

BayesFactor is a free R package that computes Bayes factors for the designs most researchers actually run: t tests, ANOVA, linear regression, correlations, proportions and contingency tables. This page is for analysts who already use R and need to know which function fits their design, how to run it, and how to read the printed output. It was checked against the CRAN package page and the package’s User’s manual (dated 7 March 2026) on 2026-11-08.

What the BayesFactor package is used for in research

The CRAN title is “Computation of Bayes Factors for Common Designs”. The package description lists the designs it covers: contingency tables, one- and two-sample designs, one-way designs, general ANOVA designs, and linear regression. The current CRAN release is 0.9.12-4.8 (published 2026-03-07), it requires R 3.2.0 or later, depends on coda and Matrix, is licensed GPL-2, and contains compiled code. Richard D. Morey is the maintainer, with Jeffrey N. Rouder among the authors. The package is free to use.

A Bayes factor compares how well two models predict the observed data. This page covers the software; for what the numbers mean and how to describe the strength of evidence, see interpreting Bayes factors. For the wider contrast with p-value-based testing, see Bayesian vs frequentist statistics.

Install and load BayesFactor

The package is distributed through CRAN. In an R session the generic CRAN route is:

install.packages("BayesFactor")
library(BayesFactor)

The User’s manual says installation can be done in several ways and does not cover it; it starts from library(BayesFactor). Because the package contains compiled code (NeedsCompilation: yes on CRAN), installing from source needs a working compiler toolchain, while binary builds from CRAN do not.

Which BayesFactor function for which design

The User’s manual names one main function per design. This mapping is the quickest way to choose a starting call.

Design Function Notes from the manual
One- or two-sample t test ttestBF Default prior scale r = √2/2 (about 0.707)
ANOVA, many candidate models anovaBF Compares many ANOVA models against an intercept-only denominator
Linear regression, many candidate models regressionBF With p predictors there are 2p − 1 models
One specific linear model lmBF Use when you want a named model rather than every combination
General mixed models and mixed predictors generalTestBF Available from version 0.9.4
Correlation correlationBF Listed in the manual’s function overview
Proportions proportionBF Listed in the manual’s function overview
Contingency tables contingencyTableBF Listed in the manual’s function overview
Posterior samples, re-estimation, model comparison posterior, recompute, compare posterior returns a BFmcmc object that works with coda’s MCMC methods

Worked example: a paired t test, then a two-way ANOVA

Step 1. One-sample Bayes factor on difference scores

The manual’s first example uses R’s built-in sleep data. It computes the difference between the two drug conditions and tests those differences against zero:

  1. data(sleep)
  2. diffScores = sleep$extra[1:10] - sleep$extra[11:20]
  3. bf = ttestBF(x = diffScores)
  4. bf prints the result; 1 / bf inverts it to favour the null.

The printed result in the manual is a Bayes factor of about 17.3 for the alternative “Alt., r=0.707” against “Null, mu = 0”, labelled “Bayes factor type: BFoneSample, JZS”. Inverting it with 1 / bf gives about 0.0579, the same evidence expressed as null over alternative.

Step 2. Draw posterior samples (optional)

  1. chains = posterior(bf, iterations = 1000)
  2. summary(chains)
  3. chains2 = recompute(chains, iterations = 10000) to rerun with more iterations.

Step 3. A two-way ANOVA on ToothGrowth

  1. ToothGrowth$dose = factor(ToothGrowth$dose) (the manual also relabels the levels Low, Medium and High). A numeric dose would otherwise be treated as a continuous covariate, not a factor.
  2. bf = anovaBF(len ~ supp*dose, data = ToothGrowth)
  3. bf prints one row per model.

Reading the BayesFactor output

For the ANOVA above, the manual shows four rows, each a model compared with the same denominator:

Row Model Bayes factor vs intercept-only
[1] supp 1.2, ±0.01%
[2] dose 4.98e+12, ±0%
[3] supp + dose 2.92e+14, ±1.58%
[4] supp + dose + supp:dose 7.44e+14, ±1.01%
  • Numerator and denominator. Each value is the support for that row’s model over the line “Against denominator”. Here the denominator is “Intercept only”; in the t test it was “Null, mu = 0”.
  • The ± percentage. It is the estimation error of the computed Bayes factor itself, not a confidence interval on the effect. Rows involving more terms carry visibly larger error (1.58% and 1.01% here).
  • Comparing two rows. Bayes factors against the same denominator can be divided. The manual shows plot(bf[3:4] / bf[2]), which compares models 3 and 4 against model 2.
  • Type label. “BFlinearModel, JZS” and “BFoneSample, JZS” name the model class and the JZS prior family used.

The manual does not attach verbal evidence labels to values. To describe strength of evidence, use an explicit scale such as those in interpreting Bayes factors and say which one you used.

Priors and how to change them

Defaults matter because a Bayes factor depends on the prior. In the manual, the t test default is r = 0.707 and the fixed-effect default in rscaleFixed is r = 0.5. A single effect can be given its own scale with rscaleEffects, for example rscaleEffects = c(color = 1) in lmBF, anovaBF or generalTestBF. Slopes in regression use the Liang et al. hyper-g priors. When you change a scale, report the value you used alongside the Bayes factor. For prior choice in planning, see Bayesian sample size determination.

Troubleshooting common BayesFactor problems

anovaBF or regressionBF returns far more models than expected

The default whichModels = "withmain" only includes an interaction together with its main effects. Setting whichModels = "all" multiplies the model count: the manual gives 127 models for a three-way design and 32,767 for four-way. Use "top" (top-down analysis) or "bottom" to restrict the search. For regression, regressionBF(rating ~ ., data = attitude) returns 63 models because 26 − 1 = 63.

A numeric grouping variable is treated as a covariate

Convert it with factor() before calling anovaBF, as in the ToothGrowth example.

Repeated measures or participant effects

Declare the participant variable as a random factor: anovaBF(RT ~ shape*color + ID, data = puzzles, whichRandom = "ID"). The manual notes random factors are not themselves tested and are assumed additive.

Selecting one model from many

Index the result by its model name string, for example bf["privileges + learning + raises + critical + advance"], which the manual shows returning a factor of about 51.

Large ± error

For posterior sampling, rerun with more iterations using recompute, as in the 1,000 to 10,000 iteration example above. Report bugs at the package’s GitHub issue tracker (github.com/richarddmorey/BayesFactor/issues).

Free alternatives and related tools

Tool Relationship to BayesFactor Source
jamovi (via the jmv R package) The CRAN page lists jmv among the packages that depend on BayesFactor, so jamovi analyses can use the same engine through a point-and-click interface CRAN BayesFactor package page, read 2026-11-08

For hierarchical or multilevel models that go beyond the designs above, see Bayesian hierarchical models.

FAQ

What does the BayesFactor R package do?

It computes Bayes factors for t tests, ANOVA, linear regression, correlations, proportions and contingency tables, comparing a model of interest with a null or intercept-only model.

How do I install BayesFactor?

Run install.packages("BayesFactor") in R, then library(BayesFactor). The package is on CRAN under GPL-2.

Which function runs a Bayesian t test?

ttestBF, for one- and two-sample designs, with a default prior scale of about 0.707.

How do I turn a Bayes factor in favour of the alternative into one in favour of the null?

Take the reciprocal: 1 / bf. In the manual’s example, 17.3 becomes about 0.0579.

What does the percentage after a Bayes factor mean?

It is the estimation error of the Bayes factor calculation, shown as ±%.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about BayesFactor R Package: Functions, Install and Worked Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →