BCompare groups · Guide test 313
Bayesian ANOVA model comparison tests
Compare inclusion and exclusion of factors or interactions using marginal likelihoods under specified priors.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Bayesian ANOVA (model comparison) — Which factors (and interaction) do the data support, and by how much?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Bayesian ANOVA model comparison
Group i has true mean (i − 1) × spread.
g-prior scale for the standardized group effects: medium 1/2, wide √2/2, ultrawide 1.
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how F varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .002).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The evidence
BF₁₀ = 18.6: strong evidence for H₁ (prior: g-prior, scale r = 0.5). Unlike p, the Bayes factor can favor H₀, and it moves with the prior's width.
The formula
With your numbers
- the g-prior scale of the group effects, Inverse-Gamma(½, r²/2)
- eigenvalues of the centered design and the data projected on its eigenvectors
The verdict
F(2, 33) = 7.45, p = .002, BF₁₀ = 18.63
p ≤ α = .050: reject H₀
Effect size: Bayes factor (H₁ over H₀) BF₁₀ = 18.628
Compares two models rather than testing one coefficient; with several factors read the inclusion Bayes factors as well. The scale r is a choice to report.
- Statistic
- 7.448
- p-value
- 0.0021
Bayesian ANOVA model comparison on Group 1: n = 12; Group 2: n = 12; Group 3: n = 12: F(2, 33) = 7.45, p = .002, BF₁₀ = 18.63. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0/H1: two specified ANOVA effect models, such as without versus with a treatment effect.
Assumptions
A specified likelihood and prior under each hypothesis; observations satisfy the chosen sampling model. Bayes factors measure relative predictive evidence and are not p values. Correct normal linear/mixed design, fixed/random effects and subject structure specified.
Formula
Parameters and notation
Effects use a documented reduced orthogonal parameterization; gj have scaled inverse-chi-square(1) mixing priors. State fixed/random-effect scales and which interactions/hierarchies are included.
Structure of the method
Represent factors and interactions in a specified normal linear/mixed design, integrate effect coefficients under the chosen mixture priors and compare marginal likelihoods of explicitly enumerated models. Inclusion evidence averages over a model set and is different from a single full-versus-null Bayes factor.
Estimated and nuisance parameters
Effect coefficients, residual scale and random-effect quantities are model parameters. Prior scales and model probabilities are hyperparameters/design choices. Reduced orthogonal parameterization defines the prior meaning; copying a numerical scale into a different contrast coding may change the implied model.
Settings and design choices
Specify fixed/random factors, subject structure, allowed interactions, hierarchy rules, model set and effect-specific prior scales. Record numerical integration/Monte Carlo settings and estimated error. Repeated-measures data require subject effects in the model; a one-way independent example does not illustrate a complete repeated-measures analysis.
Fitting and adaptation
Fit all compared models under compatible priors and the same data. Refine computation when numerical error could change conclusions. Adaptation means changing a justified model/prior and reporting sensitivity, not neural fine-tuning. Do not silently search unrestricted models and report one favorable factor inclusion result as prespecified evidence.
Sensitivity and robustness
Evidence depends on prior width, design imbalance, interaction hierarchy and the competing model set. A main effect can be hard to interpret when interactions are present. Sparse cells, missing repeated measurements and nonnormal residuals deserve model checks; report the compared models, factor coding and uncertainty rather than only an unlabeled Bayes factor.
How to use and calibrate
Compute marginal likelihood ratios with numerical integration/Monte Carlo; report numerical error and prior sensitivity. Inclusion Bayes factors average over an explicitly defined model set.
Example application
Evaluate evidence for a factorial effect while representing repeated-subject structure.
Interpretation and pitfalls
Watch out
Bayesian ANOVA is a model-comparison family, not one statistic. A full-versus-null BF does not isolate a particular main effect.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Bayesian ANOVA compares included models with an intercept-only reference. This example has one fixed factor; random factors require explicit whichRandom specification.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: BayesFactor
# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
d <- data.frame(value=c(rnorm(20),rnorm(20,.4),rnorm(20,.8)), group=factor(rep(c("A","B","C"),each=20)))
print(BayesFactor::anovaBF(value~group,data=d,whichModels="withmain",rscaleFixed="medium"))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
text=True, capture_output=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
result.check_returncode()
Validated — Executed successfully in the validation environment · needs BayesFactor
set.seed(2026)
d <- data.frame(value=c(rnorm(20),rnorm(20,.4),rnorm(20,.8)), group=factor(rep(c("A","B","C"),each=20)))
print(BayesFactor::anovaBF(value~group,data=d,whichModels="withmain",rscaleFixed="medium"))
Output
Bayes factor analysis -------------- [1] group : 60.42864 ±0.01% Against denominator: Intercept only --- Bayes factor type: BFlinearModel, JZS
Sources
- BayesFactor — BayesFactor · anovaBFAuthor documentation/source reviewed: BayesFactor 0.9.12-4.8.search.r-project.org
- anovaBFsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 313, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT313.