From 41f40eecdf111c91709853fcad6f5bdf10b213e5 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 19 Nov 2026 18:22:58 +0200 Subject: [PATCH 1/7] Count Pareto k values that could not be estimated as Inf, and use the threshold for the draws of each k --- NEWS.md | 1 + R/cv_varsel.R | 45 +++++++++------- tests/testthat/test_pareto_k.R | 98 ++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 tests/testthat/test_pareto_k.R diff --git a/NEWS.md b/NEWS.md index 8073ffa6f..0c0dfd3ee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -96,6 +96,7 @@ If you read this from a place other than .ps_khat_threshold(dim(psisloo)[1])), n = n, - khat_threshold = .ps_khat_threshold(dim(psisloo)[1]), + pareto_k, S = dim(psisloo)[1], n = n, warn_txt = paste0("Some (%d / %d) Pareto k's for the reference model's ", "PSIS-LOO weights are > %s.") ) @@ -825,8 +824,7 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, # al., 2024, , beginning of # section 3, section 3.2.8, appendix D, and appendix E). warn_pareto( - n07 = sum(y_lat_E$pareto_k > .ps_khat_threshold(dim(psisloo)[1])), n = n, - khat_threshold = .ps_khat_threshold(dim(psisloo)[1]), + y_lat_E$pareto_k, S = dim(psisloo)[1], n = n, warn_txt = paste0( "In the recalculation of the latent response values, some (%d / %d) ", "expectation-specific Pareto k-values are > %s.\n", @@ -1004,8 +1002,7 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, if (importance_sampling_nm == "psis") { pareto_k_eval <- loo::pareto_k_values(sub_psisloo) warn_pareto( - n07 = sum(pareto_k_eval > .ps_khat_threshold(dim(psisloo)[1])), n = n, - khat_threshold = .ps_khat_threshold(dim(sub_psisloo)[1]), + pareto_k_eval, S = dim(sub_psisloo)[1], n = n, warn_txt = paste0( "Some (%d / %d) Pareto k's for the reference model's PSIS-LOO ", "weights given ", txt_clust_draws(clust_used_eval, nprjdraws_eval), @@ -1442,31 +1439,39 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, return(out_list) } -warn_pareto <- function(n07, n, khat_threshold = 0.7, warn_txt) { - if (!getOption("projpred.warn_psis", TRUE) || (n07 == 0)) return() - warning(sprintf(warn_txt, n07, n, as.character(round(khat_threshold, 2))), +# Warns if some of the Pareto k values `pareto_k` (estimated from `S` draws) +# are above the threshold for `S` draws. `warn_txt` is the sprintf() format for +# the number of these k values, `n` and the threshold. +warn_pareto <- function(pareto_k, S, n, warn_txt) { + if (!getOption("projpred.warn_psis", TRUE)) return() + khat_threshold <- .ps_khat_threshold(S) + n_high <- n_high_pareto_k(pareto_k, khat_threshold) + if (n_high == 0) return() + warning(sprintf(warn_txt, n_high, n, as.character(round(khat_threshold, 2))), call. = FALSE) return() } +# The number of Pareto k values above `threshold`. As in loo::psis(), a k value +# that could not be estimated (`NA`, `NaN` or `-Inf`, for example because the +# tail of the ratios underflows to 0) counts as `Inf`. +n_high_pareto_k <- function(pareto_k, threshold) { + pareto_k[is.na(pareto_k) | pareto_k == -Inf] <- Inf + return(sum(pareto_k > threshold)) +} + #' Pareto-smoothing k-hat threshold #' -#' Copied from loo package. Remove after loo package exposes this. -#' -#' Given sample size S computes khat threshold for reliable Pareto -#' smoothed estimate (to have small probability of large error). See -#' section 3.2.4, equation (13). Sample sizes 100, 320, 1000, 2200, -#' 10000 correspond to thresholds 0.5, 0.6, 0.67, 0.7, 0.75. Although -#' with bigger sample size S we can achieve estimates with small -#' probability of large error, it is difficult to get accurate MCSE -#' estimates as the bias starts to dominate when k > 0.7 (see Section 3.2.3). -#' Thus the sample size dependend k-ht threshold is capped at 0.7. +#' The threshold of posterior::ps_khat_threshold(), capped at 0.7 as in the loo +#' package (which does not export its version). With more draws, the estimates +#' can have a small probability of a large error also for k > 0.7, but it is +#' difficult to get accurate MCSE estimates, because the bias dominates. #' @param S sample size #' @param ... unused #' @return threshold #' @noRd .ps_khat_threshold <- function(S, ...) { - min(1 - 1 / log10(S), 0.7) + min(posterior::ps_khat_threshold(S), 0.7) } # K-fold CV --------------------------------------------------------------- diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R new file mode 100644 index 000000000..86cc99ff6 --- /dev/null +++ b/tests/testthat/test_pareto_k.R @@ -0,0 +1,98 @@ +test_that(paste( + "n_high_pareto_k() counts a k value that could not be estimated as `Inf`,", + "as loo::psis() does" +), { + k <- c(NA, NaN, -Inf, Inf, 0.2, 0.69, 0.71) + expect_identical(n_high_pareto_k(k, 0.7), 5L) + expect_identical(n_high_pareto_k(c(0.1, 0.5), 0.7), 0L) + expect_identical(n_high_pareto_k(numeric(0), 0.7), 0L) +}) + +test_that(paste( + ".ps_khat_threshold() gives the threshold of posterior, capped at 0.7" +), { + for (S in c(40, 100, 1000, 2200, 4000, 1e5)) { + expect_equal(.ps_khat_threshold(S), min(1 - 1 / log10(S), 0.7), + info = S) + } +}) + +test_that(paste( + "warn_pareto() counts and prints the k values with the threshold for `S`", + "draws, and counts k values that could not be estimated" +), { + txt <- "Some (%d / %d) k's are > %s." + # The threshold for 100 draws is 0.5, for 4000 draws 0.7: + k <- c(0.55, 0.65, 0.2, NA) + expect_warning(warn_pareto(k, S = 100, n = 4, warn_txt = txt), + "Some (3 / 4) k's are > 0.5.", fixed = TRUE) + expect_warning(warn_pareto(k, S = 4000, n = 4, warn_txt = txt), + "Some (1 / 4) k's are > 0.7.", fixed = TRUE) + expect_warning(warn_pareto(c(0.2, -Inf), S = 4000, n = 2, warn_txt = txt), + "Some (1 / 2) k's are > 0.7.", fixed = TRUE) + expect_silent(warn_pareto(c(0.2, 0.3), S = 4000, n = 2, warn_txt = txt)) + withr::local_options(projpred.warn_psis = FALSE) + expect_silent(warn_pareto(k, S = 100, n = 4, warn_txt = txt)) +}) + +test_that(paste( + "cv_varsel() with LOO and the latent projection counts the", + "expectation-specific Pareto k values that could not be estimated" +), { + skip_on_cran() + withr::local_seed(5629) + n <- 30 + S <- 40 + x <- matrix(rnorm(n * 2), n, 2, dimnames = list(NULL, c("x1", "x2"))) + dat <- data.frame(x) + dat$y <- rpois(n, exp(0.5 + 0.5 * x[, 1])) + beta <- rbind(rnorm(S, 0.5, 0.1), rnorm(S, 0.5, 0.1), rnorm(S, 0, 0.1)) + # Draws 1 to 4 predict a mean far from y_1. If observation 1 is left out, + # the PSIS weights of all other draws underflow to 0, so that the + # expectation-specific Pareto k of observation 1 cannot be estimated: + ref_predfun <- function(fit, newdata = NULL) { + nd <- if (is.null(newdata)) dat else newdata + eta <- cbind(1, as.matrix(nd[, colnames(x)])) %*% fit$beta + if (is.null(newdata)) { + eta[1, 1:4] <- 20 + } + eta + } + ref <- suppressWarnings(suppressMessages(init_refmodel( + object = list(beta = beta), data = dat, formula = y ~ x1 + x2, + family = poisson(), ref_predfun = ref_predfun, dis = rep(1, S), + latent = TRUE + ))) + # The k values of loo::E_loo(): + k_eloo <- new.env() + ns_loo <- asNamespace("loo") + withr::defer(suppressMessages(untrace("E_loo.matrix", where = ns_loo))) + suppressMessages(trace( + "E_loo.matrix", where = ns_loo, print = FALSE, + exit = bquote(assign("k", returnValue()$pareto_k, envir = .(k_eloo))) + )) + warns_lat <- character() + cvvs <- withCallingHandlers( + cv_varsel(ref, cv_method = "LOO", validate_search = FALSE, + nclusters_pred = 5, nterms_max = 1, seed = 1, verbose = FALSE), + warning = function(w) { + if (grepl("recalculation of the latent response", + conditionMessage(w), fixed = TRUE)) { + warns_lat <<- c(warns_lat, conditionMessage(w)) + } + # The other warnings (Pareto k of the reference model) are expected: + invokeRestart("muffleWarning") + } + ) + expect_s3_class(cvvs, "vsel") + k <- k_eloo$k + expect_length(k, n) + thres <- .ps_khat_threshold(S) + # The k value of observation 1 could not be estimated (or is above the + # threshold): + expect_true(is.na(k[1]) || k[1] > thres) + k_inf <- replace(k, is.na(k) | k == -Inf, Inf) + expect_length(warns_lat, 1) + expect_match(warns_lat, sprintf("some (%d / %d)", sum(k_inf > thres), n), + fixed = TRUE) +}) From ef0edf7cf7e14b9cdc6f18fe2f74b967e884be3f Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 19 Nov 2026 18:30:03 +0200 Subject: [PATCH 2/7] Turn on the Pareto k warnings in the tests that expect them --- tests/testthat/test_pareto_k.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R index 86cc99ff6..3740a3706 100644 --- a/tests/testthat/test_pareto_k.R +++ b/tests/testthat/test_pareto_k.R @@ -21,6 +21,8 @@ test_that(paste( "warn_pareto() counts and prints the k values with the threshold for `S`", "draws, and counts k values that could not be estimated" ), { + # setup.R turns off these warnings: + withr::local_options(projpred.warn_psis = TRUE) txt <- "Some (%d / %d) k's are > %s." # The threshold for 100 draws is 0.5, for 4000 draws 0.7: k <- c(0.55, 0.65, 0.2, NA) @@ -40,6 +42,8 @@ test_that(paste( "expectation-specific Pareto k values that could not be estimated" ), { skip_on_cran() + # setup.R turns off the Pareto k warnings: + withr::local_options(projpred.warn_psis = TRUE) withr::local_seed(5629) n <- 30 S <- 40 From c5679d37470abb5a54cf7083059422375d4f7530 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 19 Nov 2026 18:41:11 +0200 Subject: [PATCH 3/7] Test the threshold of the submodel Pareto k warning, and accept -Inf for an unestimated k in the tests --- NEWS.md | 1 - R/cv_varsel.R | 5 +- tests/testthat/test_pareto_k.R | 83 ++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0c0dfd3ee..24ef3aa28 100644 --- a/NEWS.md +++ b/NEWS.md @@ -95,7 +95,6 @@ If you read this from a place other than threshold)) diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R index 3740a3706..5da173e14 100644 --- a/tests/testthat/test_pareto_k.R +++ b/tests/testthat/test_pareto_k.R @@ -12,6 +12,9 @@ test_that(paste( ".ps_khat_threshold() gives the threshold of posterior, capped at 0.7" ), { for (S in c(40, 100, 1000, 2200, 4000, 1e5)) { + expect_equal(.ps_khat_threshold(S), + min(posterior::ps_khat_threshold(S), 0.7), info = S) + # The values of loo's internal function: expect_equal(.ps_khat_threshold(S), min(1 - 1 / log10(S), 0.7), info = S) } @@ -70,15 +73,19 @@ test_that(paste( # The k values of loo::E_loo(): k_eloo <- new.env() ns_loo <- asNamespace("loo") + skip_if_not(exists("E_loo.matrix", ns_loo, inherits = FALSE)) withr::defer(suppressMessages(untrace("E_loo.matrix", where = ns_loo))) suppressMessages(trace( "E_loo.matrix", where = ns_loo, print = FALSE, exit = bquote(assign("k", returnValue()$pareto_k, envir = .(k_eloo))) )) warns_lat <- character() + # With 5 projected draws, cv_varsel() gives a message about SIS: cvvs <- withCallingHandlers( - cv_varsel(ref, cv_method = "LOO", validate_search = FALSE, - nclusters_pred = 5, nterms_max = 1, seed = 1, verbose = FALSE), + suppressMessages(cv_varsel( + ref, cv_method = "LOO", validate_search = FALSE, nclusters_pred = 5, + nterms_max = 1, seed = 1, verbose = FALSE + )), warning = function(w) { if (grepl("recalculation of the latent response", conditionMessage(w), fixed = TRUE)) { @@ -92,11 +99,77 @@ test_that(paste( k <- k_eloo$k expect_length(k, n) thres <- .ps_khat_threshold(S) - # The k value of observation 1 could not be estimated (or is above the - # threshold): - expect_true(is.na(k[1]) || k[1] > thres) + # The k value of observation 1 could not be estimated (`NA` or `-Inf`, + # depending on the posterior version) or is above the threshold: + expect_true(is.na(k[1]) || k[1] == -Inf || k[1] > thres) k_inf <- replace(k, is.na(k) | k == -Inf, Inf) expect_length(warns_lat, 1) expect_match(warns_lat, sprintf("some (%d / %d)", sum(k_inf > thres), n), fixed = TRUE) }) + +test_that(paste( + "cv_varsel() with LOO counts the Pareto k values of the submodel evaluation", + "with the threshold for the number of projected draws" +), { + skip_on_cran() + # setup.R turns off the Pareto k warnings: + withr::local_options(projpred.warn_psis = TRUE) + withr::local_seed(8135) + n <- 30 + S <- 4000 + S_prj <- 30 + dat <- data.frame(x1 = rnorm(n), x2 = rnorm(n)) + dat$y <- 0.5 * dat$x1 + rnorm(n) + beta <- rbind(rnorm(S, 0, 0.2), rnorm(S, 0.5, 0.2), rnorm(S, 0, 0.2)) + ref <- init_refmodel( + object = list(beta = beta), data = dat, formula = y ~ x1 + x2, + family = gaussian(), dis = rep(1, S), + ref_predfun = function(fit, newdata = NULL) { + nd <- if (is.null(newdata)) dat else newdata + cbind(1, as.matrix(nd[, c("x1", "x2")])) %*% fit$beta + } + ) + # The number of draws and the k values of each loo::psis() call: + psis_calls <- new.env() + psis_calls$out <- list() + ns_loo <- asNamespace("loo") + skip_if_not(exists("psis.matrix", ns_loo, inherits = FALSE)) + withr::defer(suppressMessages(untrace("psis.matrix", where = ns_loo))) + suppressMessages(trace( + "psis.matrix", where = ns_loo, print = FALSE, + exit = bquote(assign("out", c( + get("out", envir = .(psis_calls)), + list(list(S = dim(returnValue())[1], + k = loo::pareto_k_values(returnValue()))) + ), envir = .(psis_calls))) + )) + warns_sub <- character() + cvvs <- withCallingHandlers( + cv_varsel(ref, cv_method = "LOO", validate_search = FALSE, + nclusters_pred = S_prj, nterms_max = 1, seed = 1, + verbose = FALSE), + warning = function(w) { + if (grepl("PSIS-LOO weights given", conditionMessage(w), + fixed = TRUE)) { + warns_sub <<- c(warns_sub, conditionMessage(w)) + } + invokeRestart("muffleWarning") + } + ) + expect_s3_class(cvvs, "vsel") + out_sub <- Filter(function(o) o$S == S_prj, psis_calls$out) + expect_length(out_sub, 1) + k_sub <- out_sub[[1]]$k + thres_prj <- .ps_khat_threshold(S_prj) + thres_ref <- .ps_khat_threshold(S) + # The fixture has k values between the two thresholds, so that the count + # depends on the threshold: + expect_true(any(k_sub > thres_prj & k_sub <= thres_ref)) + expect_length(warns_sub, 1) + expect_match(warns_sub, sprintf( + "Some (%d / %d) ", n_high_pareto_k(k_sub, thres_prj), n + ), fixed = TRUE) + expect_match(warns_sub, sprintf("are > %s.", round(thres_prj, 2)), + fixed = TRUE) +}) From 74c1273532b184df6f9836f807fa442dc9f8a60a Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 19 Nov 2026 18:48:04 +0200 Subject: [PATCH 4/7] Compute the expected submodel count in the test without the helper --- tests/testthat/test_pareto_k.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R index 5da173e14..742202b8d 100644 --- a/tests/testthat/test_pareto_k.R +++ b/tests/testthat/test_pareto_k.R @@ -167,9 +167,9 @@ test_that(paste( # depends on the threshold: expect_true(any(k_sub > thres_prj & k_sub <= thres_ref)) expect_length(warns_sub, 1) - expect_match(warns_sub, sprintf( - "Some (%d / %d) ", n_high_pareto_k(k_sub, thres_prj), n - ), fixed = TRUE) + k_inf <- replace(k_sub, is.na(k_sub) | k_sub == -Inf, Inf) + expect_match(warns_sub, sprintf("Some (%d / %d) ", sum(k_inf > thres_prj), n), + fixed = TRUE) expect_match(warns_sub, sprintf("are > %s.", round(thres_prj, 2)), fixed = TRUE) }) From 8d370a44bfe0375edd7e7a53a48704c16dfad99f Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 19 Nov 2026 18:56:39 +0200 Subject: [PATCH 5/7] Remove the posterior version claim from the comments --- R/cv_varsel.R | 3 +-- tests/testthat/test_pareto_k.R | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/R/cv_varsel.R b/R/cv_varsel.R index c614f679c..b821acae7 100644 --- a/R/cv_varsel.R +++ b/R/cv_varsel.R @@ -1454,8 +1454,7 @@ warn_pareto <- function(pareto_k, S, n, warn_txt) { # The number of Pareto k values above `threshold`. As in loo::psis(), a k value # that could not be estimated (`NA` or `NaN`, for example because the tail of -# the ratios underflows to 0) counts as `Inf`. `-Inf` (from some posterior -# versions in this case) also counts as `Inf`. +# the ratios underflows to 0) counts as `Inf`. `-Inf` also counts as `Inf`. n_high_pareto_k <- function(pareto_k, threshold) { pareto_k[is.na(pareto_k) | pareto_k == -Inf] <- Inf return(sum(pareto_k > threshold)) diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R index 742202b8d..d5258b565 100644 --- a/tests/testthat/test_pareto_k.R +++ b/tests/testthat/test_pareto_k.R @@ -99,8 +99,8 @@ test_that(paste( k <- k_eloo$k expect_length(k, n) thres <- .ps_khat_threshold(S) - # The k value of observation 1 could not be estimated (`NA` or `-Inf`, - # depending on the posterior version) or is above the threshold: + # The k value of observation 1 could not be estimated (`NA` or `-Inf`) or is + # above the threshold: expect_true(is.na(k[1]) || k[1] == -Inf || k[1] > thres) k_inf <- replace(k, is.na(k) | k == -Inf, Inf) expect_length(warns_lat, 1) From dac3990e3fad348c9dc5e1fe4c91041452cdc4cd Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Fri, 20 Nov 2026 11:17:13 +0200 Subject: [PATCH 6/7] Use at least the psis() Pareto k for the latent response values instead of counting NA as Inf --- NEWS.md | 2 +- R/cv_varsel.R | 18 +++++------- tests/testthat/test_pareto_k.R | 52 ++++++++++++++++------------------ 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/NEWS.md b/NEWS.md index 24ef3aa28..719a46173 100644 --- a/NEWS.md +++ b/NEWS.md @@ -95,7 +95,7 @@ If you read this from a place other than , beginning of - # section 3, section 3.2.8, appendix D, and appendix E). + # section 3, section 3.2.8, appendix D, and appendix E). Use at least the + # k-value of the ratios from loo::psis() (`pareto_k`): loo::E_loo() of loo + # 2.10.1 can give `NA` where loo::psis() gives `Inf`, for example if most + # of the ratios underflow to 0. warn_pareto( - y_lat_E$pareto_k, S = dim(psisloo)[1], n = n, + pmax(y_lat_E$pareto_k, pareto_k, na.rm = TRUE), S = dim(psisloo)[1], + n = n, warn_txt = paste0( "In the recalculation of the latent response values, some (%d / %d) ", "expectation-specific Pareto k-values are > %s.\n", @@ -1445,21 +1449,13 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, warn_pareto <- function(pareto_k, S, n, warn_txt) { if (!getOption("projpred.warn_psis", TRUE)) return() khat_threshold <- .ps_khat_threshold(S) - n_high <- n_high_pareto_k(pareto_k, khat_threshold) + n_high <- sum(pareto_k > khat_threshold) if (n_high == 0) return() warning(sprintf(warn_txt, n_high, n, as.character(round(khat_threshold, 2))), call. = FALSE) return() } -# The number of Pareto k values above `threshold`. As in loo::psis(), a k value -# that could not be estimated (`NA` or `NaN`, for example because the tail of -# the ratios underflows to 0) counts as `Inf`. `-Inf` also counts as `Inf`. -n_high_pareto_k <- function(pareto_k, threshold) { - pareto_k[is.na(pareto_k) | pareto_k == -Inf] <- Inf - return(sum(pareto_k > threshold)) -} - #' Pareto-smoothing k-hat threshold #' #' The threshold of posterior::ps_khat_threshold(), capped at 0.7 as in the loo diff --git a/tests/testthat/test_pareto_k.R b/tests/testthat/test_pareto_k.R index d5258b565..2c63464b7 100644 --- a/tests/testthat/test_pareto_k.R +++ b/tests/testthat/test_pareto_k.R @@ -1,13 +1,3 @@ -test_that(paste( - "n_high_pareto_k() counts a k value that could not be estimated as `Inf`,", - "as loo::psis() does" -), { - k <- c(NA, NaN, -Inf, Inf, 0.2, 0.69, 0.71) - expect_identical(n_high_pareto_k(k, 0.7), 5L) - expect_identical(n_high_pareto_k(c(0.1, 0.5), 0.7), 0L) - expect_identical(n_high_pareto_k(numeric(0), 0.7), 0L) -}) - test_that(paste( ".ps_khat_threshold() gives the threshold of posterior, capped at 0.7" ), { @@ -22,27 +12,26 @@ test_that(paste( test_that(paste( "warn_pareto() counts and prints the k values with the threshold for `S`", - "draws, and counts k values that could not be estimated" + "draws" ), { # setup.R turns off these warnings: withr::local_options(projpred.warn_psis = TRUE) txt <- "Some (%d / %d) k's are > %s." # The threshold for 100 draws is 0.5, for 4000 draws 0.7: - k <- c(0.55, 0.65, 0.2, NA) + k <- c(0.55, 0.65, 0.2, Inf) expect_warning(warn_pareto(k, S = 100, n = 4, warn_txt = txt), "Some (3 / 4) k's are > 0.5.", fixed = TRUE) expect_warning(warn_pareto(k, S = 4000, n = 4, warn_txt = txt), "Some (1 / 4) k's are > 0.7.", fixed = TRUE) - expect_warning(warn_pareto(c(0.2, -Inf), S = 4000, n = 2, warn_txt = txt), - "Some (1 / 2) k's are > 0.7.", fixed = TRUE) expect_silent(warn_pareto(c(0.2, 0.3), S = 4000, n = 2, warn_txt = txt)) withr::local_options(projpred.warn_psis = FALSE) expect_silent(warn_pareto(k, S = 100, n = 4, warn_txt = txt)) }) test_that(paste( - "cv_varsel() with LOO and the latent projection counts the", - "expectation-specific Pareto k values that could not be estimated" + "cv_varsel() with LOO and the latent projection counts an", + "expectation-specific Pareto k with the Pareto k of loo::psis() if", + "loo::E_loo() cannot estimate it" ), { skip_on_cran() # setup.R turns off the Pareto k warnings: @@ -70,14 +59,17 @@ test_that(paste( family = poisson(), ref_predfun = ref_predfun, dis = rep(1, S), latent = TRUE ))) - # The k values of loo::E_loo(): + # The k values of loo::E_loo() and of the loo::psis() object that it gets: k_eloo <- new.env() ns_loo <- asNamespace("loo") skip_if_not(exists("E_loo.matrix", ns_loo, inherits = FALSE)) withr::defer(suppressMessages(untrace("E_loo.matrix", where = ns_loo))) suppressMessages(trace( "E_loo.matrix", where = ns_loo, print = FALSE, - exit = bquote(assign("k", returnValue()$pareto_k, envir = .(k_eloo))) + exit = bquote({ + assign("k", returnValue()$pareto_k, envir = .(k_eloo)) + assign("k_psis", loo::pareto_k_values(psis_object), envir = .(k_eloo)) + }) )) warns_lat <- character() # With 5 projected draws, cv_varsel() gives a message about SIS: @@ -97,15 +89,19 @@ test_that(paste( ) expect_s3_class(cvvs, "vsel") k <- k_eloo$k + k_psis <- k_eloo$k_psis expect_length(k, n) + expect_length(k_psis, n) thres <- .ps_khat_threshold(S) - # The k value of observation 1 could not be estimated (`NA` or `-Inf`) or is - # above the threshold: - expect_true(is.na(k[1]) || k[1] == -Inf || k[1] > thres) - k_inf <- replace(k, is.na(k) | k == -Inf, Inf) + # For observation 1, loo::psis() gives `Inf`. loo::E_loo() gives `Inf`, or + # `NA` or `-Inf` in loo 2.10.1: + expect_identical(k_psis[1], Inf) + expect_true(is.na(k[1]) || is.infinite(k[1])) + expect_false(anyNA(k_psis)) + # An observation counts if one of its two k values is above the threshold: + n_high <- sum(k_psis > thres | (!is.na(k) & k > thres)) expect_length(warns_lat, 1) - expect_match(warns_lat, sprintf("some (%d / %d)", sum(k_inf > thres), n), - fixed = TRUE) + expect_match(warns_lat, sprintf("some (%d / %d)", n_high, n), fixed = TRUE) }) test_that(paste( @@ -122,14 +118,15 @@ test_that(paste( dat <- data.frame(x1 = rnorm(n), x2 = rnorm(n)) dat$y <- 0.5 * dat$x1 + rnorm(n) beta <- rbind(rnorm(S, 0, 0.2), rnorm(S, 0.5, 0.2), rnorm(S, 0, 0.2)) - ref <- init_refmodel( + # init_refmodel() warns about the default `cvrefbuilder`: + ref <- suppressWarnings(init_refmodel( object = list(beta = beta), data = dat, formula = y ~ x1 + x2, family = gaussian(), dis = rep(1, S), ref_predfun = function(fit, newdata = NULL) { nd <- if (is.null(newdata)) dat else newdata cbind(1, as.matrix(nd[, c("x1", "x2")])) %*% fit$beta } - ) + )) # The number of draws and the k values of each loo::psis() call: psis_calls <- new.env() psis_calls$out <- list() @@ -167,8 +164,7 @@ test_that(paste( # depends on the threshold: expect_true(any(k_sub > thres_prj & k_sub <= thres_ref)) expect_length(warns_sub, 1) - k_inf <- replace(k_sub, is.na(k_sub) | k_sub == -Inf, Inf) - expect_match(warns_sub, sprintf("Some (%d / %d) ", sum(k_inf > thres_prj), n), + expect_match(warns_sub, sprintf("Some (%d / %d) ", sum(k_sub > thres_prj), n), fixed = TRUE) expect_match(warns_sub, sprintf("are > %s.", round(thres_prj, 2)), fixed = TRUE) From d9b7f3244f552877aefcd036e7bab2fd8d7bb67f Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Fri, 20 Nov 2026 12:17:04 +0200 Subject: [PATCH 7/7] Add the PR number to the NEWS item --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7c07cb162..2b3d37c86 100644 --- a/NEWS.md +++ b/NEWS.md @@ -99,7 +99,7 @@ If you read this from a place other than