diff --git a/NEWS.md b/NEWS.md index b922eb5b4..2b3d37c86 100644 --- a/NEWS.md +++ b/NEWS.md @@ -99,6 +99,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.") ) @@ -828,10 +827,13 @@ loo_varsel <- function(refmodel, method, nterms_max, ndraws, ) # The k-values are h-specific (expectation-specific) here (see Vehtari et # al., 2024, , 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( - n07 = sum(y_lat_E$pareto_k > .ps_khat_threshold(dim(psisloo)[1])), n = n, - khat_threshold = .ps_khat_threshold(dim(psisloo)[1]), + 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", @@ -1009,8 +1011,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), @@ -1456,31 +1457,31 @@ 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 <- 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() } #' 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..2c63464b7 --- /dev/null +++ b/tests/testthat/test_pareto_k.R @@ -0,0 +1,171 @@ +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) + } +}) + +test_that(paste( + "warn_pareto() counts and prints the k values with the threshold for `S`", + "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, 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_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 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: + withr::local_options(projpred.warn_psis = TRUE) + 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() 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)) + 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: + cvvs <- withCallingHandlers( + 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)) { + 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 + k_psis <- k_eloo$k_psis + expect_length(k, n) + expect_length(k_psis, n) + thres <- .ps_khat_threshold(S) + # 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)", n_high, 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)) + # 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() + 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) ", sum(k_sub > thres_prj), n), + fixed = TRUE) + expect_match(warns_sub, sprintf("are > %s.", round(thres_prj, 2)), + fixed = TRUE) +})