Skip to content

Commit

Permalink
Add report.compare.loo
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Mar 25, 2024
1 parent a53f206 commit 040775e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Collate:
'report.stanreg.R'
'report.brmsfit.R'
'report.character.R'
'report.compare.loo.R'
'report.compare_performance.R'
'report.data.frame.R'
'report.default.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ S3method(report,bayesfactor_inclusion)
S3method(report,bayesfactor_models)
S3method(report,brmsfit)
S3method(report,character)
S3method(report,compare.loo)
S3method(report,compare_performance)
S3method(report,data.frame)
S3method(report,default)
Expand Down
59 changes: 59 additions & 0 deletions R/report.compare.loo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Reporting Bayesian Model Comparison
#'
#' Automatically report the results of Bayesian model comparison using the `loo` package.
#'
#' @param x An object of class [loo_compare()].
#'
#' @examplesIf require("brms", quietly = TRUE)
#' \donttest{
#' library(brms)
#'
#' m1 <- brms::brm(mpg ~ qsec, data=mtcars)
#' m2 <- brms::brm(mpg ~ qsec + drat, data=mtcars)
#'
#' x <- brms::loo_compare(brms::add_criterion(m1, "loo"),
#' brms::add_criterion(m2, "loo"),
#' model_names=c("m1", "m2"))
#' report(x)
#' }
#'
#' @details
#' The rule of thumb is that the models are "very similar" if |elpd_diff| (the
#' absolute value of elpd_diff) is less than 4 (Sivula, Magnusson and Vehtari, 2020).
#' If superior to 4, then one can use the SE to obtain a standardized difference
#' (Z-diff) and interpret it as such, assuming that the difference is normally
#' distributed.
#'
#' @return Objects of class [report_text()].
#' @export
report.compare.loo <- function(x, ...) {
# https://stats.stackexchange.com/questions/608881/how-to-interpret-elpd-diff-of-bayesian-loo-estimate-in-bayesian-logistic-regress

Check warning on line 30 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=30,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 133 characters.
# https://users.aalto.fi/%7Eave/CV-FAQ.html#12_What_is_the_interpretation_of_ELPD__elpd_loo__elpd_diff
# https://users.aalto.fi/%7Eave/CV-FAQ.html#se_diff

# The difference in expected log predictive density (elpd) between each model
# and the best model as well as the standard error of this difference (assuming
# the difference is approximately normal).
x <- as.data.frame(x)
# The values in the first row are 0s because the models are ordered from best to worst according to their elpd.
modnames <- rownames(x)

#
z_elpd_diff <- x$elpd_diff / x$se_diff

text <- "The difference in predictive accuracy, as index by Expected Log Predictive Density (ELPD), suggests that '"

Check warning on line 44 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=44,col=3,[object_overwrite_linter] 'text' is an exported object from package 'graphics'. Avoid re-using such symbols.
text <- paste0(text, modnames[1], "' is the best model, followed by '")

Check warning on line 45 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=45,col=3,[object_overwrite_linter] 'text' is an exported object from package 'graphics'. Avoid re-using such symbols.

for (m in 2:(nrow(x))) {
text <- paste0(text, modnames[m ],

Check warning on line 48 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=48,col=5,[object_overwrite_linter] 'text' is an exported object from package 'graphics'. Avoid re-using such symbols.

Check warning on line 48 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=48,col=36,[spaces_inside_linter] Do not place spaces before square brackets.
"' (diff = ",
insight::format_value(x$elpd_diff[m]),
# " ± ",
# insight::format_value(x$se_diff[m]),
", Z-diff = ",
insight::format_value(z_elpd_diff[m]),
")")
}
class(text) <- c("report_text", class(text))
text
}

Check warning on line 59 in R/report.compare.loo.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/report.compare.loo.R,line=59,col=2,[trailing_blank_lines_linter] Add a terminal newline.
39 changes: 39 additions & 0 deletions man/report.compare.loo.Rd

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

0 comments on commit 040775e

Please sign in to comment.