Skip to content

Commit 2466312

Browse files
Merge branch 'main' into joss-paper-draft
2 parents 75ff5b4 + 08b374d commit 2466312

15 files changed

+316
-220
lines changed

.github/workflows/R-CMD-check-strict.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

DESCRIPTION

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: report
22
Type: Package
33
Title: Automated Reporting of Results and Statistical Models
4-
Version: 0.5.8.1
4+
Version: 0.5.8.2
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",
@@ -57,14 +57,15 @@ Depends:
5757
Imports:
5858
bayestestR (>= 0.13.2),
5959
effectsize (>= 0.8.6),
60-
insight (>= 0.19.8),
61-
parameters (>= 0.21.5),
62-
performance (>= 0.10.9),
63-
datawizard (>= 0.9.1),
60+
insight (>= 0.19.10),
61+
parameters (>= 0.21.6),
62+
performance (>= 0.11.0),
63+
datawizard (>= 0.10.0),
6464
stats,
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'
@@ -107,6 +109,7 @@ Collate:
107109
'report.stanreg.R'
108110
'report.brmsfit.R'
109111
'report.character.R'
112+
'report.compare.loo.R'
110113
'report.compare_performance.R'
111114
'report.data.frame.R'
112115
'report.default.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
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)
@@ -36,6 +37,7 @@ S3method(report,bayesfactor_inclusion)
3637
S3method(report,bayesfactor_models)
3738
S3method(report,brmsfit)
3839
S3method(report,character)
40+
S3method(report,compare.loo)
3941
S3method(report,compare_performance)
4042
S3method(report,data.frame)
4143
S3method(report,default)
@@ -166,6 +168,7 @@ S3method(report_random,glmmTMB)
166168
S3method(report_random,lme)
167169
S3method(report_random,merMod)
168170
S3method(report_random,stanreg)
171+
S3method(report_statistics,BFBayesFactor)
169172
S3method(report_statistics,Date)
170173
S3method(report_statistics,MixMod)
171174
S3method(report_statistics,anova)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# report 0.5.9
2+
3+
Minor changes
4+
5+
* `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`.
7+
18
# report 0.5.8
29

310
New features

R/report.BFBayesFactor.R

Lines changed: 87 additions & 0 deletions
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+
}

R/report.compare.loo.R

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#' Reporting Bayesian Model Comparison
2+
#'
3+
#' Automatically report the results of Bayesian model comparison using the `loo` package.
4+
#'
5+
#' @param x An object of class [brms::loo_compare].
6+
#' @param index type if index to report - expected log pointwise predictive
7+
#' density (ELPD) or information criteria (IC).
8+
#' @param ... Additional arguments (not used for now).
9+
#'
10+
#' @examplesIf require("brms", quietly = TRUE)
11+
#' \donttest{
12+
#' library(brms)
13+
#'
14+
#' m1 <- brms::brm(mpg ~ qsec, data = mtcars)
15+
#' m2 <- brms::brm(mpg ~ qsec + drat, data = mtcars)
16+
#'
17+
#' x <- brms::loo_compare(brms::add_criterion(m1, "loo"),
18+
#' brms::add_criterion(m2, "loo"),
19+
#' model_names = c("m1", "m2")
20+
#' )
21+
#' report(x)
22+
#' }
23+
#'
24+
#' @details
25+
#' The rule of thumb is that the models are "very similar" if |elpd_diff| (the
26+
#' absolute value of elpd_diff) is less than 4 (Sivula, Magnusson and Vehtari, 2020).
27+
#' If superior to 4, then one can use the SE to obtain a standardized difference
28+
#' (Z-diff) and interpret it as such, assuming that the difference is normally
29+
#' distributed.
30+
#'
31+
#' @return Objects of class [report_text()].
32+
#' @export
33+
report.compare.loo <- function(x, index = c("ELPD", "IC"), ...) {
34+
# nolint start
35+
# https://stats.stackexchange.com/questions/608881/how-to-interpret-elpd-diff-of-bayesian-loo-estimate-in-bayesian-logistic-regress
36+
# nolint end
37+
# https://users.aalto.fi/%7Eave/CV-FAQ.html#12_What_is_the_interpretation_of_ELPD__elpd_loo__elpd_diff
38+
# https://users.aalto.fi/%7Eave/CV-FAQ.html#se_diff
39+
40+
# The difference in expected log predictive density (elpd) between each model
41+
# and the best model as well as the standard error of this difference (assuming
42+
# the difference is approximately normal).
43+
index <- match.arg(index)
44+
x <- as.data.frame(x)
45+
# The values in the first row are 0s because the models are ordered from best to worst according to their elpd.
46+
modnames <- rownames(x)
47+
48+
elpd_diff <- x[["elpd_diff"]]
49+
ic_diff <- -2 * elpd_diff
50+
51+
z_elpd_diff <- elpd_diff / x[["se_diff"]]
52+
z_ic_diff <- -z_elpd_diff
53+
54+
if ("looic" %in% colnames(x)) {
55+
type <- "LOO"
56+
ENP <- x[["p_loo"]]
57+
} else {
58+
type <- "WAIC"
59+
ENP <- x[["p_waic"]]
60+
}
61+
62+
if (index == "ELPD") {
63+
index_label <- sprintf("Expected Log Predictive Density (ELPD-%s)", type)
64+
} else if (type == "LOO") {
65+
index_label <- "Leave-One-Out CV Information Criterion (LOOIC)"
66+
} else {
67+
index_label <- "Widely Applicable Information Criterion (WAIC)"
68+
}
69+
70+
out_text <- sprintf(
71+
paste(
72+
"The difference in predictive accuracy, as index by %s, suggests that '%s' ",
73+
"is the best model (effective number of parameters (ENP) = %.2f), followed by"
74+
),
75+
index_label, modnames[1], ENP[1]
76+
)
77+
78+
if (index == "ELPD") {
79+
other_texts <- sprintf(
80+
"'%s' (diff = %.2f, ENP = %.2f, z-diff = %.2f)",
81+
modnames[-1],
82+
elpd_diff[-1],
83+
ENP[-1],
84+
z_elpd_diff[-1]
85+
)
86+
} else {
87+
other_texts <- sprintf(
88+
"'%s' (diff = %.2f, ENP = %.2f, z-diff = %.2f)",
89+
modnames[-1],
90+
ic_diff[-1],
91+
ENP[-1],
92+
z_ic_diff[-1]
93+
)
94+
}
95+
96+
sep <- "."
97+
nothermods <- length(other_texts)
98+
if (nothermods > 1L) {
99+
if (nothermods == 2L) {
100+
sep <- c(" and ", sep)
101+
} else {
102+
sep <- c(rep(", ", length = nothermods - 2), ", and ", sep)
103+
}
104+
}
105+
106+
other_texts <- paste0(other_texts, sep, collapse = "")
107+
108+
out_text <- paste(out_text, other_texts, collapse = "")
109+
class(text) <- c("report_text", class(text))
110+
out_text
111+
}

_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ reference:
9191
- report.stanreg
9292
- report.test_performance
9393
- report.estimate_contrasts
94+
- report.compare.loo
95+
- report.BFBayesFactor
9496

9597
- title: Report Non-Statistical Objects
9698
desc: |

inst/WORDLIST

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
APA
22
Args
3+
BayesFactor
34
BibLaTeX
45
CMD
56
CSL
67
Dom
8+
ELPD
79
ESS
810
Gabry
911
Hotfix
12+
IC
13+
Magnusson
1014
Mattan
1115
Newcombe
1216
ORCID
@@ -16,16 +20,19 @@ Rhat
1620
SEM
1721
SEXIT
1822
Shachar
23+
Sivula
24+
Vehtari
1925
amongst
2026
anova
2127
bmwiernik
2228
brms
2329
eXistence
2430
easystats
31+
elpd
2532
github
2633
htest
27-
https
2834
ivreg
35+
lifecycle
2936
mattansb
3037
pacakges
3138
participants’
@@ -42,4 +49,3 @@ unarchive
4249
versicolor
4350
virginica
4451
’s
45-
lifecycle

man/report.BFBayesFactor.Rd

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)