Skip to content

Commit aae7445

Browse files
parameters() supports loo_compare() (#1077)
* fix #1075 * hotfix * x -> model * lintr, version * pkgdown * wordlist --------- Co-authored-by: Daniel <[email protected]>
1 parent 45fef77 commit aae7445

File tree

6 files changed

+140
-1
lines changed

6 files changed

+140
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.24.2.10
4+
Version: 0.24.2.11
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ S3method(model_parameters,clm2)
215215
S3method(model_parameters,clmm)
216216
S3method(model_parameters,clmm2)
217217
S3method(model_parameters,coeftest)
218+
S3method(model_parameters,compare.loo)
218219
S3method(model_parameters,comparisons)
219220
S3method(model_parameters,cpglmm)
220221
S3method(model_parameters,data.frame)

R/methods_loo.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#' Bayesian Model Comparison
2+
#'
3+
#' Make a table of Bayesian model comparisons using the `loo` package.
4+
#'
5+
#' @param model An object of class [brms::loo_compare].
6+
#' @param include_IC Whether to include the information criteria (IC).
7+
#' @param include_ENP Whether to include the effective number of parameters (ENP).
8+
#' @param ... Additional arguments (not used for now).
9+
#'
10+
# nolint start
11+
#' @examplesIf all(insight::check_if_installed(c("brms", "RcppEigen", "BH"), quietly = TRUE))
12+
# nolint end
13+
#' \donttest{
14+
#' library(brms)
15+
#'
16+
#' m1 <- brms::brm(mpg ~ qsec, data = mtcars)
17+
#' m2 <- brms::brm(mpg ~ qsec + drat, data = mtcars)
18+
#' m3 <- brms::brm(mpg ~ qsec + drat + wt, data = mtcars)
19+
#'
20+
#' x <- suppressWarnings(brms::loo_compare(
21+
#' brms::add_criterion(m1, "loo"),
22+
#' brms::add_criterion(m2, "loo"),
23+
#' brms::add_criterion(m3, "loo"),
24+
#' model_names = c("m1", "m2", "m3")
25+
#' ))
26+
#' model_parameters(x)
27+
#' model_parameters(x, include_IC = FALSE, include_ENP = TRUE)
28+
#' }
29+
#'
30+
#' @details
31+
#' The rule of thumb is that the models are "very similar" if |elpd_diff| (the
32+
#' absolute value of elpd_diff) is less than 4 (Sivula, Magnusson and Vehtari, 2020).
33+
#' If superior to 4, then one can use the SE to obtain a standardized difference
34+
#' (Z-diff) and interpret it as such, assuming that the difference is normally
35+
#' distributed. The corresponding p-value is then calculated as `2 * pnorm(-abs(Z-diff))`.
36+
#' However, note that if the raw ELPD difference is small (less than 4), it doesn't
37+
#' make much sense to rely on its standardized value: it is not very useful to
38+
#' conclude that a model is much better than another if both models make very
39+
#' similar predictions.
40+
#'
41+
#' @return Objects of `parameters_model`.
42+
#' @export
43+
model_parameters.compare.loo <- function(model, include_IC = TRUE, include_ENP = FALSE, ...) {
44+
# nolint start
45+
# https://stats.stackexchange.com/questions/608881/how-to-interpret-elpd-diff-of-bayesian-loo-estimate-in-bayesian-logistic-regress
46+
# nolint end
47+
# https://users.aalto.fi/%7Eave/CV-FAQ.html#12_What_is_the_interpretation_of_ELPD__elpd_loo__elpd_diff
48+
# https://users.aalto.fi/%7Eave/CV-FAQ.html#se_diff
49+
50+
# The difference in expected log predictive density (elpd) between each model
51+
# and the best model as well as the standard error of this difference (assuming
52+
# the difference is approximately normal).
53+
54+
# The values in the first row are 0s because the models are ordered from best to worst according to their elpd.
55+
x <- as.data.frame(model)
56+
57+
out <- data.frame(Name = rownames(x), stringsAsFactors = FALSE)
58+
if ("looic" %in% colnames(x)) {
59+
if (include_IC) out$LOOIC <- x[["looic"]]
60+
if (include_ENP) out$ENP <- x[["p_loo"]]
61+
out$ELPD <- x[["elpd_loo"]]
62+
} else {
63+
if (include_IC) out$WAIC <- x[["waic"]]
64+
if (include_ENP) out$ENP <- x[["p_waic"]]
65+
out$ELPD <- x[["elpd_waic"]]
66+
}
67+
68+
out$Difference <- x[["elpd_diff"]]
69+
out$Difference_SE <- x[["se_diff"]]
70+
71+
z_elpd_diff <- x[["elpd_diff"]] / x[["se_diff"]]
72+
out$p <- 2 * stats::pnorm(-abs(z_elpd_diff))
73+
74+
class(out) <- c("parameters_model", "data.frame")
75+
out
76+
}

inst/WORDLIST

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Dupont
4848
DV
4949
EFA
5050
EGAnet
51+
ELPD
52+
ENP
5153
ESS
5254
Eingenvalues
5355
Eivenvalues
@@ -83,6 +85,7 @@ Hofman
8385
Hofmann
8486
Hornik
8587
Isager
88+
IC
8689
ICA
8790
IRR
8891
JB
@@ -107,6 +110,7 @@ MLM
107110
MPE
108111
MSA
109112
Maechler
113+
Magnusson
110114
Malo
111115
Mattan
112116
McNemar
@@ -164,6 +168,7 @@ Shachar
164168
Shi
165169
Shikano
166170
Shmekels
171+
Sivula
167172
Sphericity
168173
Stata
169174
Stigum
@@ -177,6 +182,7 @@ Turkheimer
177182
VGAM
178183
VSS
179184
Valls
185+
Vehtari
180186
Velicer
181187
Vos
182188
WRS
@@ -231,6 +237,7 @@ doi
231237
easystats
232238
effectsize
233239
effectsizes
240+
elpd
234241
emmGrid
235242
emmeans
236243
endogeneity

man/model_parameters.compare.loo.Rd

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

pkgdown/_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ reference:
3636
- model_parameters.glht
3737
- model_parameters.glimML
3838
- model_parameters.t1way
39+
- model_parameters.compare.loo
3940

4041
- title: "Standard Errors, Confidence Intervals, Degrees of Freedom and p-values"
4142
contents:

0 commit comments

Comments
 (0)