Skip to content

Commit

Permalink
[Feature] report() for BayesFactor objects (#420)
Browse files Browse the repository at this point in the history
* add

* Update report.BFBayesFactor.R

* exact=TRUE

* docs

* Update DESCRIPTION

* fix lints, build_reference_index

* NEWS, pkgdown

* update snapshots, document

* skip test, update wordlist

* delete problematic report.brms snapshot

---------

Co-authored-by: Mattan S. Ben-Shachar <mattansb@msbstats.info>
Co-authored-by: Rémi Thériault <13123390+rempsyc@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 1, 2024
1 parent ed9e9d0 commit a2c7709
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 199 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Imports:
tools,
utils
Suggests:
BayesFactor,
brms,
ivreg,
knitr,
Expand Down Expand Up @@ -95,6 +96,7 @@ Collate:
'format_model.R'
'reexports.R'
'report-package.R'
'report.BFBayesFactor.R'
'utils_combine_tables.R'
'report.lm.R'
'report.MixMod.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ S3method(print,report_table)
S3method(print,report_text)
S3method(print_html,report_sample)
S3method(print_md,report_sample)
S3method(report,BFBayesFactor)
S3method(report,Date)
S3method(report,MixMod)
S3method(report,anova)
Expand Down Expand Up @@ -167,6 +168,7 @@ S3method(report_random,glmmTMB)
S3method(report_random,lme)
S3method(report_random,merMod)
S3method(report_random,stanreg)
S3method(report_statistics,BFBayesFactor)
S3method(report_statistics,Date)
S3method(report_statistics,MixMod)
S3method(report_statistics,anova)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Minor changes

* `report` now supports reporting of Bayesian model comparison with variables of class `brms::loo_compare`.
* `report` now supports reporting of BayesFactor objects with variables of class `BFBayesFactor`.

# report 0.5.8

Expand Down
87 changes: 87 additions & 0 deletions R/report.BFBayesFactor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#' Reporting `BFBayesFactor` objects from the `BayesFactor` package
#'
#' Interpretation of the Bayes factor output from the `BayesFactor` package.
#'
#' @param x An object of class `BFBayesFactor`.
#' @param h0,h1 Names of the null and alternative hypotheses.
#' @param table A `parameters` table (this argument is meant for internal use).
#' @param ... Other arguments to be passed to [effectsize::interpret_bf] and [insight::format_bf].
#'
#' @examplesIf requireNamespace("BayesFactor", quietly = TRUE)
#' \donttest{
#' library(BayesFactor)
#'
#' rez <- BayesFactor::ttestBF(iris$Sepal.Width, iris$Sepal.Length)
#' report_statistics(rez, exact = TRUE) # Print exact BF
#' report(rez, h0 = "the null hypothesis", h1 = "the alternative")
#'
#' rez <- BayesFactor::correlationBF(iris$Sepal.Width, iris$Sepal.Length)
#' report(rez)
#' }
#'
#' @export
report.BFBayesFactor <- function(x, h0 = "H0", h1 = "H1", ...) {
if (inherits("BFlinearModel", class(x@numerator[[1]]))) {
return(report(bayestestR::bayesfactor_models(x), ...))
}

if (length(x@numerator) > 1) {
insight::format_alert(
"Multiple `BFBayesFactor` models detected - reporting for the first numerator model.",
"See help(\"get_parameters\", package = \"insight\")."
)
x <- x[1]
}

param <- parameters::parameters(x[1], ...)
bf <- param$BF
other_dir <- ifelse(bf < 1, "h0", "h1")


if (other_dir == "h1") {
other_text <- paste0(
"There is ",
effectsize::interpret_bf(bf, ...),
" ",
h1,
" over ",
h0,
" (", report_statistics(x, ...), ")."
)
} else {
other_text <- paste0(
"There is ",
effectsize::interpret_bf(1 / bf, ...),
" ",
h0,
" over ",
h1,
" (", report_statistics(x, ...), ")."
)
}
other_text
}



#' @rdname report.BFBayesFactor
#' @export
report_statistics.BFBayesFactor <- function(x, table = NULL, ...) {
if (is.null(table)) {
if (length(x@numerator) > 1) {
insight::format_alert(
"Multiple `BFBayesFactor` models detected - reporting for the first numerator model.",
"See help(\"get_parameters\", package = \"insight\")."
)
x <- x[1]
}
table <- parameters::parameters(x, ...)
}

bf <- table$BF
other_text <- ifelse(bf < 1,
insight::format_bf(1 / bf, name = "BF01", ...),
insight::format_bf(bf, name = "BF10", ...)
)
other_text
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ reference:
- report.test_performance
- report.estimate_contrasts
- report.compare.loo
- report.BFBayesFactor

- title: Report Non-Statistical Objects
desc: |
Expand Down
2 changes: 1 addition & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
APA
Args
BayesFactor
BibLaTeX
CMD
CSL
Expand Down Expand Up @@ -30,7 +31,6 @@ easystats
elpd
github
htest
https
ivreg
lifecycle
mattansb
Expand Down
37 changes: 37 additions & 0 deletions man/report.BFBayesFactor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 0 additions & 159 deletions tests/testthat/_snaps/windows/report.brmsfit.md

This file was deleted.

Loading

0 comments on commit a2c7709

Please sign in to comment.