Learn R Programming

bayesics (version 2.0.2)

bayes_factors: Bayes factors for lm_b, glm_b, and survfit_b

Description

Bayes factors for Bayesian regression objects using the Savage-Dickey ratio

Usage

bayes_factors(object, ...)

# S3 method for lm_b bayes_factors(object, by = "coefficient", ...)

# S3 method for glm_b bayes_factors(object, by = "coefficient", ...)

# S3 method for survfit_b bayes_factors(object, object2, ...)

Value

A tibble with Bayes factors and interpretations.

Arguments

object

lm_b, glm_b, or survfit_b object

...

Passed to methods.

by

character. Either "coefficient" or "variable". If the former, Bayes factors will be computed for each regression coefficient separately. If the latter, Bayes factors will be computed for each covariate separately.

object2

a second survfit_b object. Not used for other classes.

Details

Bayes factors are given in terms of favoring the two-tailed alternative hypothesis vs. the null hypothesis that the regression coefficient equals zero. Currently implemented for lm_b or glm_b objects. Note that for glm_b objects, if importance sampling was used, the model will be refit using fixed form variational Bayes to get the multivariate posterior density.

Interpretation is taken from Kass and Raftery.

References

James M. Dickey. "The Weighted Likelihood Ratio, Linear Hypotheses on Normal Location Parameters." Ann. Math. Statist. 42 (1) 204 - 223, February, 1971. https://doi.org/10.1214/aoms/1177693507

Kass, R. E., & Raftery, A. E. (1995). Bayes Factors. Journal of the American Statistical Association, 90(430), 773–795.

Examples

Run this code
# \donttest{
# Generate some binomial data
set.seed(2025)
N = 500
test_data = 
  data.frame(x1 = rnorm(N),
             x2 = rnorm(N),
             x3 = letters[1:5])
test_data$outcome = 
  rbinom(N,1,1.0 / (1.0 + exp(-(-2 + test_data$x1 + 2 * (test_data$x3 %in% c("d","e")) ))))


# Fit a GLM
fit <-
  glm_b(outcome ~ x1 + x2 + x3,
        data = test_data,
        family = binomial(),
        seed = 2025)

# Compute the BF for each coefficient
bayes_factors(fit)

# Compute the BF for each variable
bayes_factors(fit,
              by = "variable")
# }

Run the code above in your browser using DataLab