Skip to content

Commit a2c7709

Browse files
DominiqueMakowskimattansbrempsyc
authored
[Feature] report() for BayesFactor objects (#420)
* 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>
1 parent ed9e9d0 commit a2c7709

12 files changed

+139
-199
lines changed

DESCRIPTION

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Imports:
6565
tools,
6666
utils
6767
Suggests:
68+
BayesFactor,
6869
brms,
6970
ivreg,
7071
knitr,
@@ -95,6 +96,7 @@ Collate:
9596
'format_model.R'
9697
'reexports.R'
9798
'report-package.R'
99+
'report.BFBayesFactor.R'
98100
'utils_combine_tables.R'
99101
'report.lm.R'
100102
'report.MixMod.R'

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ S3method(print,report_table)
2727
S3method(print,report_text)
2828
S3method(print_html,report_sample)
2929
S3method(print_md,report_sample)
30+
S3method(report,BFBayesFactor)
3031
S3method(report,Date)
3132
S3method(report,MixMod)
3233
S3method(report,anova)
@@ -167,6 +168,7 @@ S3method(report_random,glmmTMB)
167168
S3method(report_random,lme)
168169
S3method(report_random,merMod)
169170
S3method(report_random,stanreg)
171+
S3method(report_statistics,BFBayesFactor)
170172
S3method(report_statistics,Date)
171173
S3method(report_statistics,MixMod)
172174
S3method(report_statistics,anova)

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Minor changes
44

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

78
# report 0.5.8
89

R/report.BFBayesFactor.R

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#' Reporting `BFBayesFactor` objects from the `BayesFactor` package
2+
#'
3+
#' Interpretation of the Bayes factor output from the `BayesFactor` package.
4+
#'
5+
#' @param x An object of class `BFBayesFactor`.
6+
#' @param h0,h1 Names of the null and alternative hypotheses.
7+
#' @param table A `parameters` table (this argument is meant for internal use).
8+
#' @param ... Other arguments to be passed to [effectsize::interpret_bf] and [insight::format_bf].
9+
#'
10+
#' @examplesIf requireNamespace("BayesFactor", quietly = TRUE)
11+
#' \donttest{
12+
#' library(BayesFactor)
13+
#'
14+
#' rez <- BayesFactor::ttestBF(iris$Sepal.Width, iris$Sepal.Length)
15+
#' report_statistics(rez, exact = TRUE) # Print exact BF
16+
#' report(rez, h0 = "the null hypothesis", h1 = "the alternative")
17+
#'
18+
#' rez <- BayesFactor::correlationBF(iris$Sepal.Width, iris$Sepal.Length)
19+
#' report(rez)
20+
#' }
21+
#'
22+
#' @export
23+
report.BFBayesFactor <- function(x, h0 = "H0", h1 = "H1", ...) {
24+
if (inherits("BFlinearModel", class(x@numerator[[1]]))) {
25+
return(report(bayestestR::bayesfactor_models(x), ...))
26+
}
27+
28+
if (length(x@numerator) > 1) {
29+
insight::format_alert(
30+
"Multiple `BFBayesFactor` models detected - reporting for the first numerator model.",
31+
"See help(\"get_parameters\", package = \"insight\")."
32+
)
33+
x <- x[1]
34+
}
35+
36+
param <- parameters::parameters(x[1], ...)
37+
bf <- param$BF
38+
other_dir <- ifelse(bf < 1, "h0", "h1")
39+
40+
41+
if (other_dir == "h1") {
42+
other_text <- paste0(
43+
"There is ",
44+
effectsize::interpret_bf(bf, ...),
45+
" ",
46+
h1,
47+
" over ",
48+
h0,
49+
" (", report_statistics(x, ...), ")."
50+
)
51+
} else {
52+
other_text <- paste0(
53+
"There is ",
54+
effectsize::interpret_bf(1 / bf, ...),
55+
" ",
56+
h0,
57+
" over ",
58+
h1,
59+
" (", report_statistics(x, ...), ")."
60+
)
61+
}
62+
other_text
63+
}
64+
65+
66+
67+
#' @rdname report.BFBayesFactor
68+
#' @export
69+
report_statistics.BFBayesFactor <- function(x, table = NULL, ...) {
70+
if (is.null(table)) {
71+
if (length(x@numerator) > 1) {
72+
insight::format_alert(
73+
"Multiple `BFBayesFactor` models detected - reporting for the first numerator model.",
74+
"See help(\"get_parameters\", package = \"insight\")."
75+
)
76+
x <- x[1]
77+
}
78+
table <- parameters::parameters(x, ...)
79+
}
80+
81+
bf <- table$BF
82+
other_text <- ifelse(bf < 1,
83+
insight::format_bf(1 / bf, name = "BF01", ...),
84+
insight::format_bf(bf, name = "BF10", ...)
85+
)
86+
other_text
87+
}

_pkgdown.yml

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ reference:
9292
- report.test_performance
9393
- report.estimate_contrasts
9494
- report.compare.loo
95+
- report.BFBayesFactor
9596

9697
- title: Report Non-Statistical Objects
9798
desc: |

inst/WORDLIST

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
APA
22
Args
3+
BayesFactor
34
BibLaTeX
45
CMD
56
CSL
@@ -30,7 +31,6 @@ easystats
3031
elpd
3132
github
3233
htest
33-
https
3434
ivreg
3535
lifecycle
3636
mattansb

man/report.BFBayesFactor.Rd

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/windows/report.brmsfit.md

-159
This file was deleted.

0 commit comments

Comments
 (0)