Skip to content

Commit 42f24bd

Browse files
authored
Merge be0266c into 45c261e
2 parents 45c261e + be0266c commit 42f24bd

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

R/E_loo.R

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
#' Pareto-k's, which may produce optimistic estimates.
4949
#'
5050
#' For `type="mean"`, `type="var"`, and `type="sd"`, the returned Pareto-k is
51-
#' the maximum of the Pareto-k's for the left and right tail of \eqn{hr} and
52-
#' the right tail of \eqn{r}, where \eqn{r} is the importance ratio and
53-
#' \eqn{h=x} for `type="mean"` and \eqn{h=x^2} for `type="var"` and
54-
#' `type="sd"`. For `type="quantile"`, the returned Pareto-k is the Pareto-k
55-
#' for the right tail of \eqn{r}.
51+
#' usually the maximum of the Pareto-k's for the left and right tail of \eqn{hr}
52+
#' and the right tail of \eqn{r}, where \eqn{r} is the importance ratio and
53+
#' \eqn{h=x} for `type="mean"` and \eqn{h=x^2} for `type="var"` and `type="sd"`.
54+
#' If \eqn{h} is binary, constant, or not finite, or if `type="quantile"`, the
55+
#' returned Pareto-k is the Pareto-k for the right tail of \eqn{r}.
5656
#' }
5757
#' }
5858
#'
@@ -291,10 +291,16 @@ E_loo_khat.matrix <- function(x, psis_object, log_ratios, ...) {
291291
h_theta <- x_i
292292
r_theta <- exp(log_ratios_i - max(log_ratios_i))
293293
khat_r <- posterior::pareto_khat(r_theta, tail = "right", ndraws_tail = tail_len_i)$khat
294-
if (is.null(x_i)) {
294+
if (is.null(x_i) || is_constant(x_i) || length(unique(x_i))==2 ||
295+
anyNA(x_i) || any(is.infinite(x_i))) {
295296
khat_r
296297
} else {
297298
khat_hr <- posterior::pareto_khat(h_theta * r_theta, tail = "both", ndraws_tail = tail_len_i)$khat
298-
max(khat_hr, khat_r)
299+
if (is.na(khat_hr) && is.na(khat_r)) {
300+
k <- NA
301+
} else {
302+
k <- max(khat_hr, khat_r, na.rm=TRUE)
303+
}
304+
k
299305
}
300306
}

R/helpers.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,7 @@ release_questions <- function() {
193193
)
194194
}
195195
# nocov end
196+
197+
is_constant <- function(x, tol = .Machine$double.eps) {
198+
abs(max(x) - min(x)) < tol
199+
}

man/E_loo.Rd

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

tests/testthat/test_E_loo.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ test_that("E_loo.matrix equal to reference", {
115115
test_that("E_loo throws correct errors and warnings", {
116116
# warnings
117117
expect_no_warning(E_loo.matrix(x, psis_mat))
118+
# no warnings if x is constant, binary, NA, NaN, Inf
119+
expect_no_warning(E_loo.matrix(x*0, psis_mat))
120+
expect_no_warning(E_loo.matrix(0+(x>0), psis_mat))
121+
expect_no_warning(E_loo.matrix(x+NA, psis_mat))
122+
expect_no_warning(E_loo.matrix(x*NaN, psis_mat))
123+
expect_no_warning(E_loo.matrix(x*Inf, psis_mat))
118124
expect_no_warning(E_test <- E_loo.default(x[, 1], psis_vec))
119125
expect_length(E_test$pareto_k, 1)
120126

@@ -191,4 +197,3 @@ test_that("weighted variance works", {
191197
w <- c(rep(0.1, 10), rep(0, 90))
192198
expect_equal(.wvar(x, w), var(x[w > 0]))
193199
})
194-

0 commit comments

Comments
 (0)