Learn R Programming

bayestestR (version 0.2.0)

bayesfactor: Bayes Factors (BF)

Description

This function compte the Bayes factors (BFs) that are appropriate to the input. For vectors or single models, it will compute Savage-Dickey's BFs. For multiple models, it will return the BF corresponding to comparison between models and if a comparison is passed, it will compute the inclusion BF.

For a complete overview of these functions, read the Bayes factor vignette.

Usage

bayesfactor(..., prior = NULL, direction = "two-sided",
  hypothesis = 0, effects = c("fixed", "random", "all"),
  verbose = TRUE, denominator = 1, match_models = FALSE,
  prior_odds = NULL)

Arguments

...

A numeric vector, model object(s), or the output from bayesfactor_models.

prior

Vector representing a prior distribution (if posterior is a vector), or a data frame with column names matching posterior's (if posterior is a data frame). Otherwise ignored.

direction

Test type. One of 0, "two-sided" (default, two tailed), -1, "left" (left tailed) or 1, "right" (right tailed).

hypothesis

Value to be tested against (usually 0 in the context of null hypothesis testing).

effects

Should results for fixed effects, random effects or both be returned? Only applies to mixed models. May be abbreviated.

verbose

Toggle off warnings.

denominator

Either an integer indicating which of the models to use as the denominator, or a model to use as a denominator. Ignored for BFBayesFactor.

match_models

If FALSE (default), Inclustion BFs are computed by comparing all models with an effect against all models without the effect. If TRUE, Inclusion BFs are computed by comparing all models with an effect against models without the effect AND without any higher-order interactions with the effect.

prior_odds

optional vector of prior odds for the models. See BayesFactor::priorOdds

Value

Some type of Bayes factor, depending on the input. See bayesfactor_savagedickey, bayesfactor_models or bayesfactor_inclusion

Examples

Run this code
# NOT RUN {
library(bayestestR)

# Vectors
prior <- distribution_normal(1000, mean = 0, sd = 1)
posterior <- distribution_normal(1000, mean = .5, sd = .3)

bayesfactor(posterior, prior = prior)
# }
# NOT RUN {
# rstanarm models
# ---------------
library(rstanarm)
model <- stan_lmer(extra ~ group + (1 | ID), data = sleep)
bayesfactor(model)
# }
# NOT RUN {
# Frequentist models
# ---------------
m0 <- lm(extra ~ 1, data = sleep)
m1 <- lm(extra ~ group, data = sleep)
m2 <- lm(extra ~ group + ID, data = sleep)

comparison <- bayesfactor(m0, m1, m2)
comparison

bayesfactor(comparison)
# }

Run the code above in your browser using DataLab