Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] report() for BayesFactor objects #420

Merged
merged 12 commits into from
Apr 1, 2024
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
Loading