Skip to contents

Bayesian ANOVA Wrapper Using BayesFactor::generalTestBF

Usage

anova_bf(
  dataset,
  design,
  rscaleFixed = "medium",
  rscaleRandom = "nuisance",
  rscaleCont = "medium",
  rscaleEffects = NULL,
  method = "auto",
  progress = FALSE,
  multicore = FALSE,
  summarize = TRUE,
  inc_ranef = FALSE,
  seed = NULL
)

Arguments

dataset

A data frame in (long) ANOVA format: the first column is the subject identifier, the last column is the dependent variable, and the columns in between are factors (between/within are inferred from design).

design

A single string specifying the design. It must contain exactly one "s" (for subjects). Letters left of "s" are between-subject factors; letters right of "s" are within-subject factors. Examples: "sAB" (two within), "ABsC" (two between, one within).

rscaleFixed

Prior scale for fixed effects. Default "medium".

rscaleRandom

Prior scale for random effects. Default "nuisance".

rscaleCont

Prior scale for continuous covariates. Default "medium".

rscaleEffects

Optional vector of prior scales for individual effects.

method

Method for computing Bayes factors; see BayesFactor::generalTestBF(). Default "auto".

progress

Logical; show progress bar? Default FALSE.

multicore

Logical; use multicore processing? Default FALSE.

summarize

Logical; if TRUE (default), return a summarized tibble; if FALSE, return the raw BayesFactor result.

inc_ranef

Logical; when summarizing, also include random-effect terms in the table (passed to mutolabr::summary_generalTestBF()).

seed

Optional numeric seed to reproduce Monte Carlo results; passed to withr::with_seed().

Value

If summarize = TRUE (default), a tibble summarizing Bayes factors for exclusion of each term from the full model, with columns:

effect

Effect removed from the full model (fixed or, optionally, random).

BF

Bayes factor for the null (exclusion) vs. alternative (full).

error

Estimated numerical error.

log10_BF

Base-10 logarithm of BF.

favor

Whether data favor the null or alternative.

evidence

Evidence category (e.g., "anecdotal", "moderate", ...).

If summarize = FALSE, returns the BayesFactor object produced by generalTestBF().

Details

Runs a Bayesian ANOVA for between-participants, within-participants, or mixed designs via BayesFactor::generalTestBF(..., whichModels = "top"), given a compact design string (e.g., "sABC", "ABsC"). One subject column, one dependent variable column, and factor columns in between are assumed.

Internally the function:

  • Validates design ("s" must appear exactly once) and checks that the number of factor columns matches the count implied by design.

  • Renames columns to "s" (subject), "fb1","fb2",... (between), "fw1","fw2",... (within), and "y" (outcome), then coerces factors.

  • Drops rows with missing values and unused factor levels.

  • Builds the fixed-effect formula including all main effects and all interactions among fb* and fw*.

  • Sets random effects to s and s:fw combinations for all within-factor subsets except the highest-order within interaction (which coincides with the residual error term in classical ANOVA). No between-factor terms are included as random effects.

  • Standardizes y to mean 0 and SD 1 prior to analysis.

The Bayes factors are computed with whichModels = "top" (full model vs. models with one term removed) and summarized via mutolabr::summary_generalTestBF().

Examples

# Example 1: between-participants design (design = "ABs")

anova_bf(data_fict_between, design = "ABs", seed = 610, progress = FALSE)
#> # A tibble: 3 × 7
#>   effect       type       BF error log10_BF favor evidence 
#>   <chr>        <chr>   <dbl> <dbl>    <dbl> <chr> <chr>    
#> 1 group        fixed   6.15  0.015   0.789  alt.  moderate 
#> 2 gender       fixed 223     0.018   2.35   alt.  extreme  
#> 3 group:gender fixed   0.882 0.02   -0.0545 null  anecdotal

# Example 2: within-participants design (design = "sAB")

anova_bf(data_fict_within, design = "sAB", seed = 610, progress = FALSE)
#> # A tibble: 3 × 7
#>   effect         type          BF error log10_BF favor evidence   
#>   <chr>          <chr>      <dbl> <dbl>    <dbl> <chr> <chr>      
#> 1 condition      fixed       1.13  0.26   0.0536 alt.  anecdotal  
#> 2 time           fixed 2760000     0.26   6.44   alt.  extreme    
#> 3 condition:time fixed      41.6   0.26   1.62   alt.  very strong

# Example 3: mixed design (between = A, within = C)

anova_bf(data_fict_mixed, design = "AsB", seed = 610, progress = FALSE)
#> # A tibble: 3 × 7
#>   effect           type     BF error log10_BF favor evidence 
#>   <chr>            <chr> <dbl> <dbl>    <dbl> <chr> <chr>    
#> 1 gender           fixed  1.45 0.075    0.160 alt.  anecdotal
#> 2 condition        fixed  4.37 0.056    0.641 alt.  moderate 
#> 3 gender:condition fixed  2.22 0.071    0.347 alt.  anecdotal