Learn R Programming

bayestestR (version 0.2.2)

bayesfactor_savagedickey: Savage-Dickey density ratio Bayes Factor (BF)

Description

This method computes the ratio between the density of a single value (typically the null) of two distributions. When the compared distributions are the posterior and the prior distributions, this results is an approximation of a Bayes factor comparing the model against a model in which the parameter of choice is restricted to the point null.

See also the Bayes factors vignette.

Usage

bayesfactor_savagedickey(posterior, prior = NULL,
  direction = "two-sided", hypothesis = 0, verbose = TRUE, ...)

# S3 method for numeric bayesfactor_savagedickey(posterior, prior = NULL, direction = "two-sided", hypothesis = 0, verbose = TRUE, ...)

# S3 method for stanreg bayesfactor_savagedickey(posterior, prior = NULL, direction = "two-sided", hypothesis = 0, verbose = TRUE, effects = c("fixed", "random", "all"), ...)

# S3 method for brmsfit bayesfactor_savagedickey(posterior, prior = NULL, direction = "two-sided", hypothesis = 0, verbose = TRUE, effects = c("fixed", "random", "all"), ...)

# S3 method for emmGrid bayesfactor_savagedickey(posterior, prior = NULL, direction = "two-sided", hypothesis = 0, verbose = TRUE, ...)

# S3 method for data.frame bayesfactor_savagedickey(posterior, prior = NULL, direction = "two-sided", hypothesis = 0, verbose = TRUE, ...)

Arguments

posterior

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

prior

An object representing a prior distribution (see Details).

direction

Test type (see details). 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).

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 (point) null effect model.

Details

This method is used to compute Bayes factors based on prior and posterior distributions. 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 numerical vector, prior should also be a numerical vector.

  • When posterior is an emmGrid object based on a stanreg or brmsfit model, prior should be that model object (see example).

  • When posterior is a stanreg or brmsfit model, there is no need to specify prior, as prior samples are drawn internally.

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

One-sided Tests (setting an order restriction)

One sided tests (controlled by direction) are conducted by setting an order restriction on the prior and posterior distributions (Morey & Wagenmakers, 2013).

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

  • Wagenmakers, E. J., Lodewyckx, T., Kuriyal, H., and Grasman, R. (2010). Bayesian hypothesis testing for psychologists: A tutorial on the Savage-Dickey method. Cognitive psychology, 60(3), 158-189.

  • Wetzels, R., Matzke, D., Lee, M. D., Rouder, J. N., Iverson, G. J., and Wagenmakers, E.-J. (2011). Statistical Evidence in Experimental Psychology: An Empirical Comparison Using 855 t Tests. Perspectives on Psychological Science, 6(3), 291<U+2013>298. 10.1177/1745691611406923

  • Heck, D. W. (2019). A caveat on the Savage<U+2013>Dickey density ratio: The case of computing Bayes factors for regression parameters. British Journal of Mathematical and Statistical Psychology, 72(2), 316-333.

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

Examples

Run this code
# NOT RUN {
library(bayestestR)

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

bayesfactor_savagedickey(posterior, prior)
# }
# NOT RUN {
# rstanarm models
# ---------------
library(rstanarm)
stan_model <- stan_lmer(extra ~ group + (1 | ID), data = sleep)
bayesfactor_savagedickey(stan_model)

# emmGrid objects
# ---------------
library(emmeans)
group_diff <- pairs(emmeans(stan_model, ~group))
bayesfactor_savagedickey(group_diff, prior = stan_model)

# brms models
# -----------
library(brms)
my_custom_priors <-
  set_prior("student_t(3, 0, 1)", class = "b") +
  set_prior("student_t(3, 0, 1)", class = "sd", group = "ID")

brms_model <- brm(extra ~ group + (1 | ID),
  data = sleep,
  prior = my_custom_priors
)
bayesfactor_savagedickey(brms_model)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab