Skip to content

Commit 9f28fc4

Browse files
authored
Merge pull request #264 from stan-dev/fix-NA-handling-in-E_loo_khat
Fix E_loo_khat error when posterior::pareto_khat returns NA
2 parents 1ccfc44 + bd4e7f8 commit 9f28fc4

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

R/E_loo.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,18 @@ E_loo_khat.matrix <- function(x, psis_object, log_ratios, ...) {
290290
.E_loo_khat_i <- function(x_i, log_ratios_i, tail_len_i) {
291291
h_theta <- x_i
292292
r_theta <- exp(log_ratios_i - max(log_ratios_i))
293-
khat_r <- posterior::pareto_khat(r_theta, tail = "right", ndraws_tail = tail_len_i)$khat
293+
khat_r <- posterior::pareto_khat(r_theta, tail = "right", ndraws_tail = tail_len_i)
294+
if (!is.na(khat_r)) { # https://github.com/stan-dev/loo/issues/263
295+
khat_r <- khat_r$khat
296+
}
294297
if (is.null(x_i) || is_constant(x_i) || length(unique(x_i))==2 ||
295298
anyNA(x_i) || any(is.infinite(x_i))) {
296299
khat_r
297300
} else {
298-
khat_hr <- posterior::pareto_khat(h_theta * r_theta, tail = "both", ndraws_tail = tail_len_i)$khat
301+
khat_hr <- posterior::pareto_khat(h_theta * r_theta, tail = "both", ndraws_tail = tail_len_i)
302+
if (!is.na(khat_hr)) { # https://github.com/stan-dev/loo/issues/263
303+
khat_hr <- khat_hr$khat
304+
}
299305
if (is.na(khat_hr) && is.na(khat_r)) {
300306
k <- NA
301307
} else {

0 commit comments

Comments
 (0)