Learn R Programming

bayestestR (version 0.4.0)

bayesfactor_restricted: Bayes Factors (BF) for Order Restricted Models

Description

This method computes Bayes factors for comparing a model with an order restrictions on its parameters with the fully unrestricted model. Note that this method should only be used for confirmatory analyses.

For info on specifying correct priors for factors with more than 2 levels, see the Bayes factors vignette.

For more info, see the Bayes factors vignette.

Usage

bayesfactor_restricted(posterior, hypothesis, prior = NULL,
  verbose = TRUE, ...)

# S3 method for stanreg bayesfactor_restricted(posterior, hypothesis, prior = NULL, verbose = TRUE, effects = c("fixed", "random", "all"), ...)

# S3 method for brmsfit bayesfactor_restricted(posterior, hypothesis, prior = NULL, verbose = TRUE, effects = c("fixed", "random", "all"), ...)

# S3 method for emmGrid bayesfactor_restricted(posterior, hypothesis, prior = NULL, verbose = TRUE, ...)

Arguments

posterior

A stanreg / brmsfit object, emmGrid or a data frame - representing a posterior distribution(s) from (see Details).

hypothesis

A character vector specifying the restrictions as logical conditions (see examples below).

prior

An object representing a prior distribution (see Details).

verbose

Toggle off warnings.

...

Currently not used.

effects

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

Value

A data frame containing the Bayes factor representing evidence against the un-restricted model.

Details

This method is used to compute Bayes factors for order-restricted models vs un-restricted models by setting an order restriction on the prior and posterior distributions (Morey & Wagenmakers, 2013).

(Though it is possible to use bayesfactor_restricted to test interval restrictions, it is more suitable for testing order restrictions (see examples)).

When posterior is a model (stanreg, brmsfit), posterior and prior samples are extracted for each parameter, and Savage-Dickey Bayes factors are computed for each parameter.

NOTE: For brmsfit models, the model must have been fitted with custom (non-default) priors. See example below.

Setting the correct prior

It is important to provide the correct prior for meaningful results.

  • When posterior is a data.frame, prior should also be a data.frame, with matching column order.

  • When posterior is a stanreg or brmsfit model:

    • prior can be set to NULL, in which case prior samples are drawn internally.

    • prior can also be a model equvilant to posterior but with samples from the priors only.

  • When posterior is an emmGrid object:

    • prior should be the stanreg or brmsfit model used to create the emmGrid objects.

    • prior can also be an emmGrid object equvilant to posterior but created with a model of priors samples only.

Interpreting Bayes Factors

A Bayes factor greater than 1 can be interpereted as evidence against the null, at which one convention is that a Bayes factor greater than 3 can be considered as "substantial" evidence against the null (and vice versa, a Bayes factor smaller than 1/3 indicates substantial evidence in favor of the null-hypothesis) (Wetzels et al. 2011).

References

  • Morey, R. D., & Wagenmakers, E. J. (2014). Simple relation between Bayesian order-restricted and point-null hypothesis tests. Statistics & Probability Letters, 92, 121-124.

  • Morey, R. D., & Rouder, J. N. (2011). Bayes factor approaches for testing interval null hypotheses. Psychological methods, 16(4), 406.

  • Morey, R. D. (Jan, 2015). Multiple Comparisons with BayesFactor, Part 2 <U+2013> order restrictions. Retrived from https://richarddmorey.org/category/order-restrictions/.

Examples

Run this code
# NOT RUN {
library(bayestestR)
prior <- data.frame(
  X = rnorm(100),
  X1 = rnorm(100),
  X3 = rnorm(100)
)

posterior <- data.frame(
  X = rnorm(100, .4),
  X1 = rnorm(100, -.2),
  X3 = rnorm(100)
)

hyps <- c(
  "X > X1 & X1 > X3",
  "X > X1"
)

bayesfactor_restricted(posterior, hypothesis = hyps, prior = prior)
# }
# NOT RUN {
# rstanarm models
# ---------------
library(rstanarm)
fit_stan <- stan_glm(mpg ~ wt + cyl + am,
  data = mtcars
)
hyps <- c(
  "am > 0 & cyl < 0",
  "cyl < 0",
  "wt - cyl > 0"
)
bayesfactor_restricted(fit_stan, hypothesis = hyps)

# emmGrid objects
# ---------------
library(emmeans)

# replicating http://bayesfactor.blogspot.com/2015/01/multiple-comparisons-with-bayesfactor-2.html
disgust_data <- read.table(url("http://www.learnbayes.org/disgust_example.txt"), header = TRUE)

contrasts(disgust_data$condition) <- contr.bayes # see vignette
fit_model <- stan_glm(score ~ condition, data = disgust_data, family = gaussian())

em_condition <- emmeans(fit_model, ~condition)
hyps <- c("lemon < control & control < sulfur")

bayesfactor_restricted(em_condition, prior = fit_model, hypothesis = hyps)
# > # Bayes Factor (Order-Restriction)
# >
# >                          Hypothesis P(Prior) P(Posterior) Bayes Factor
# >  lemon < control & control < sulfur     0.17         0.75         4.49
# > ---
# > Bayes factors for the restricted movel vs. the un-restricted model.
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab