diff --git a/DESCRIPTION b/DESCRIPTION index 1f472cd9..3d78011d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,6 +37,7 @@ Depends: Imports: checkmate, cli (>= 3.4.0), + lifecycle (>= 1.0.0), matrixStats (>= 0.52), parallel, posterior (>= 1.7.0), diff --git a/NEWS.md b/NEWS.md index 0c157557..dec6c3ab 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,8 +4,8 @@ - new functions `insample_pred_measure()`, `loo_pred_measure()`, `kfold_pred_measure()`, `test_pred_measure()`, and `pred_measure()` - new built-in measures `measure_*()`, listed in `supported_measures_list` -* Deprecate `elpd()`, `crps()`, `scrps()`, `loo_crps()`, `loo_scrps()`, and - `loo_predictive_metric()`. Use `measure_*()` and `*_pred_measure()`. See +* Soft-deprecate `elpd()`, `crps()`, `scrps()`, `loo_crps()`, `loo_scrps()`, + and `loo_predictive_metric()`. Use `measure_*()` and `*_pred_measure()`. See `vignette("migration-guide")`. By @florence-bockting in #363 * Add `model_compare()` by @florence-bockting in #380: - compares models on each measure of `*_pred_measure()` results @@ -13,8 +13,8 @@ - shows loss measures on the utility scale - ranks models by `elpd`, else by the first shared measure (alphabetical) - new `print()` argument `measures` -* Deprecate `loo_compare()`. Use `model_compare()`. Methods in other packages - (e.g. **brms**) still work. By @florence-bockting in #380 +* Soft-deprecate `loo_compare()`. Use `model_compare()`. Methods in other + packages (e.g. **brms**) still work. By @florence-bockting in #380 * Add `custom_measure()` to set the name, loss flag, and SE of the difference for a custom measure by @florence-bockting in #380 * `loo_pred_measure()` warns when `loo_moment_match()` or `reloo()` changed the diff --git a/R/crps.R b/R/crps.R index 9844e079..ad4c13a1 100644 --- a/R/crps.R +++ b/R/crps.R @@ -68,6 +68,7 @@ #' Prediction, and Estimation. Journal of the American Statistical Association, #' 102(477), 359–378. crps <- function(x, ...) { + lifecycle::deprecate_soft("3.0.0", "crps()", "measure_rps()") UseMethod("crps") } @@ -75,18 +76,21 @@ crps <- function(x, ...) { #' @rdname crps #' @export scrps <- function(x, ...) { + lifecycle::deprecate_soft("3.0.0", "scrps()", "measure_srps()") UseMethod("scrps") } #' @rdname crps #' @export loo_crps <- function(x, ...) { + lifecycle::deprecate_soft("3.0.0", "loo_crps()", "loo_pred_measure()") UseMethod("loo_crps") } #' @rdname crps #' @export loo_scrps <- function(x, ...) { + lifecycle::deprecate_soft("3.0.0", "loo_scrps()", "loo_pred_measure()") UseMethod("loo_scrps") } @@ -94,7 +98,6 @@ loo_scrps <- function(x, ...) { #' @rdname crps #' @export crps.matrix <- function(x, x2, y, ..., permutations = 1) { - .Deprecated("measure_rps") validate_crps_input(x, x2, y) repeats <- replicate(permutations, EXX_compute(x, x2), simplify = F) EXX <- Reduce(`+`, repeats) / permutations @@ -129,7 +132,6 @@ loo_crps.matrix <- permutations = 1, r_eff = 1, cores = getOption("mc.cores", 1)) { - .Deprecated("loo_pred_measure") validate_crps_input(x, x2, y, log_lik) repeats <- replicate(permutations, EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, ...), @@ -144,7 +146,6 @@ loo_crps.matrix <- #' @rdname crps #' @export scrps.matrix <- function(x, x2, y, ..., permutations = 1) { - .Deprecated("measure_srps") validate_crps_input(x, x2, y) repeats <- replicate(permutations, EXX_compute(x, x2), simplify = F) EXX <- Reduce(`+`, repeats) / permutations @@ -173,7 +174,6 @@ loo_scrps.matrix <- permutations = 1, r_eff = 1, cores = getOption("mc.cores", 1)) { - .Deprecated("loo_pred_measure") validate_crps_input(x, x2, y, log_lik) repeats <- replicate(permutations, EXX_loo_compute(x, x2, log_lik, r_eff = r_eff, ...), diff --git a/R/elpd.R b/R/elpd.R index 2e1557bf..10c09d54 100644 --- a/R/elpd.R +++ b/R/elpd.R @@ -34,6 +34,7 @@ #' } #' elpd <- function(x, ...) { + lifecycle::deprecate_soft("3.0.0", "elpd()", "measure_elpd()") UseMethod("elpd") } @@ -42,7 +43,6 @@ elpd <- function(x, ...) { #' @template array #' elpd.array <- function(x, ...) { - .Deprecated("measure_elpd") ll <- llarray_to_matrix(x) .elpd_matrix_impl(ll) } @@ -52,7 +52,6 @@ elpd.array <- function(x, ...) { #' @template matrix #' elpd.matrix <- function(x, ...) { - .Deprecated("measure_elpd") .elpd_matrix_impl(x) } diff --git a/R/helpers.R b/R/helpers.R index 709ec2ff..dd5fc932 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -269,30 +269,3 @@ is_constant <- function(x, tol = .Machine$double.eps) { abs(max(x) - min(x)) < tol } -#' Issue a deprecation warning the first time it is triggered in a session -#' -#' Repeated calls with the same `id` are silent, so a script calling a -#' deprecated function in a loop is not flooded with warnings. `old` is passed -#' on explicitly so the message does not depend on which method called this. -#' Which `id`s have already warned is kept in `state`, an environment created -#' once when the package is built and private to this function. -#' -#' @noRd -#' @param id Identifier for the deprecation; one warning per `id` per session. -#' @param new,old Name of the replacement and deprecated function. -#' @return `TRUE` if a warning was issued, `FALSE` otherwise, invisibly. -#' -.deprecate_once <- local({ - state <- new.env(parent = emptyenv()) - function(id, new, old = id) { - if (isTRUE(state[[id]])) { - return(invisible(FALSE)) - } - state[[id]] <- TRUE - warning( - "'", old, "' is deprecated. Use '", new, "' instead.\n", - call. = FALSE, immediate. = TRUE - ) - invisible(TRUE) - } -}) diff --git a/R/loo_compare.R b/R/loo_compare.R index 54739ac7..9328663f 100644 --- a/R/loo_compare.R +++ b/R/loo_compare.R @@ -5,7 +5,8 @@ #' instead. See `vignette("migration-guide", package = "loo")` for details. #' #' `loo_compare()` remains an exported generic so that `loo_compare` methods -#' registered by other packages keep dispatching as before, but it is frozen at +#' registered by other packages keep dispatching as before without a deprecation +#' warning. It is frozen at #' its previous behavior: it compares `"loo"`, `"waic"`, and `"kfold"` objects on #' [ELPD][loo-glossary] only. Comparing #' [`pred_measure`][pred_measure] results requires [model_compare()]. @@ -41,8 +42,7 @@ loo_compare <- function(x, ...) { #' @rdname loo_compare #' @export loo_compare.default <- function(x, ...) { - .deprecate_once("loo_compare", new = "model_compare") - + lifecycle::deprecate_soft("3.0.0", "loo_compare()", "model_compare()") loos <- .model_compare_inputs(x, ...) if (any(vapply(loos, is.pred_measure, logical(1)))) { stop( @@ -58,6 +58,6 @@ loo_compare.default <- function(x, ...) { #' @rdname loo_compare #' @export loo_compare.psis_loo_ss_list <- function(x, ...) { - .deprecate_once("loo_compare", new = "model_compare") + lifecycle::deprecate_soft("3.0.0", "loo_compare()", "model_compare()") model_compare.psis_loo_ss_list(x, ...) } diff --git a/R/loo_predictive_metric.R b/R/loo_predictive_metric.R index 67ca3280..6c270b5f 100644 --- a/R/loo_predictive_metric.R +++ b/R/loo_predictive_metric.R @@ -92,6 +92,9 @@ #' } #' } loo_predictive_metric <- function(x, ...) { + lifecycle::deprecate_soft( + "3.0.0", "loo_predictive_metric()", "loo_pred_measure()" + ) UseMethod("loo_predictive_metric") } @@ -105,7 +108,6 @@ loo_predictive_metric.matrix <- metric = c("mae", "rmse", "mse", "acc", "balanced_acc"), r_eff = 1, cores = getOption("mc.cores", 1)) { - .Deprecated("loo_pred_measure") stopifnot( is.numeric(x), is.numeric(y), diff --git a/man/loo_compare.Rd b/man/loo_compare.Rd index c9211fe7..3e7f4c9e 100644 --- a/man/loo_compare.Rd +++ b/man/loo_compare.Rd @@ -28,7 +28,8 @@ the result is identical to what \code{\link[=model_compare]{model_compare()}} re instead. See \code{vignette("migration-guide", package = "loo")} for details. \code{loo_compare()} remains an exported generic so that \code{loo_compare} methods -registered by other packages keep dispatching as before, but it is frozen at +registered by other packages keep dispatching as before without a deprecation +warning. It is frozen at its previous behavior: it compares \code{"loo"}, \code{"waic"}, and \code{"kfold"} objects on \link[=loo-glossary]{ELPD} only. Comparing \code{\link{pred_measure}} results requires \code{\link[=model_compare]{model_compare()}}. diff --git a/tests/testthat/test_compare.R b/tests/testthat/test_compare.R index 51240e51..ee4937f8 100644 --- a/tests/testthat/test_compare.R +++ b/tests/testthat/test_compare.R @@ -1873,21 +1873,9 @@ test_that("printed comparison output stays within 80 columns", { # Tests for deprecated loo_compare() -------------------------------------- -test_that("loo_compare throws a deprecation warning once per session", { - # forget that the warning was already issued earlier in this session - forget_warning <- function() { - assign("loo_compare", FALSE, envir = environment(loo:::.deprecate_once)$state) - } - forget_warning() - on.exit(forget_warning(), add = TRUE) - - expect_warning(loo_compare(w1, w2), "deprecated") - # already warned in this session, so these are silent - expect_no_warning(loo_compare(w1, w2)) - expect_no_warning(loo_compare(x = list(w1, w2))) - - forget_warning() - expect_warning(loo_compare(x = list(w1, w2)), "deprecated") +test_that("loo_compare() is soft-deprecated", { + lifecycle::expect_deprecated(loo_compare(w1, w2)) + lifecycle::expect_deprecated(loo_compare(x = list(w1, w2))) }) test_that("loo_compare still returns what model_compare returns", { @@ -1914,10 +1902,15 @@ test_that("loo_compare is frozen to classic elpd comparison", { }) test_that("loo_compare is still a generic", { - # methods registered elsewhere (e.g. brms, rstanarm) keep dispatching + # methods registered elsewhere (e.g. brms, rstanarm) keep dispatching and + # don't warn, since their users can't switch to model_compare() yet assign("loo_compare.fake_fit", function(x, ...) "dispatched", envir = globalenv()) on.exit(rm("loo_compare.fake_fit", envir = globalenv()), add = TRUE) - expect_identical(loo_compare(structure(list(), class = "fake_fit")), "dispatched") + out <- expect_no_warning( + loo_compare(structure(list(), class = "fake_fit")), + class = "lifecycle_warning_deprecated" + ) + expect_identical(out, "dispatched") }) test_that("print.compare.loo names the source for non-loo comparisons", { diff --git a/tests/testthat/test_crps.R b/tests/testthat/test_crps.R index a19b655e..8b6a4ebb 100644 --- a/tests/testthat/test_crps.R +++ b/tests/testthat/test_crps.R @@ -211,10 +211,10 @@ test_that("loo_scrps() and loo_pred_measure(..., measures = 'srps') differ like }) test_that("methods for single data point pass permutations to matrix methods", { - set.seed(1); a <- crps(x1[,1], x2[,1], y[1], permutations = 5) - set.seed(1); b <- crps(x1[,1, drop = FALSE], x2[,1, drop = FALSE], y[1], permutations = 5) + set.seed(1); a <- suppressWarnings(crps(x1[,1], x2[,1], y[1], permutations = 5)) + set.seed(1); b <- suppressWarnings(crps(x1[,1, drop = FALSE], x2[,1, drop = FALSE], y[1], permutations = 5)) expect_equal(a, b) - set.seed(1); a <- scrps(x1[,1], x2[,1], y[1], permutations = 5) - set.seed(1); b <- scrps(x1[,1, drop = FALSE], x2[,1, drop = FALSE], y[1], permutations = 5) + set.seed(1); a <- suppressWarnings(scrps(x1[,1], x2[,1], y[1], permutations = 5)) + set.seed(1); b <- suppressWarnings(scrps(x1[,1, drop = FALSE], x2[,1, drop = FALSE], y[1], permutations = 5)) expect_equal(a, b) }) diff --git a/tests/testthat/test_deprecated_measures.R b/tests/testthat/test_deprecated_measures.R index 92c081a0..1a3015ce 100644 --- a/tests/testthat/test_deprecated_measures.R +++ b/tests/testthat/test_deprecated_measures.R @@ -1,10 +1,6 @@ -expect_deprecated <- function(object) { - testthat::expect_warning(object, "deprecated", ignore.case = TRUE) -} - test_that("elpd() is deprecated", { ll <- example_loglik_matrix() - expect_deprecated(elpd(ll)) + lifecycle::expect_deprecated(elpd(ll)) }) test_that("elpd() still returns elpd_generic", { @@ -32,8 +28,8 @@ test_that("crps() is deprecated", { y <- rnorm(5) x1 <- matrix(rnorm(50), nrow = 10) x2 <- matrix(rnorm(50), nrow = 10) - expect_deprecated(crps(x1, x2, y)) - expect_deprecated(scrps(x1, x2, y)) + lifecycle::expect_deprecated(crps(x1, x2, y)) + lifecycle::expect_deprecated(scrps(x1, x2, y)) }) test_that("loo_crps() is deprecated", { @@ -42,21 +38,17 @@ test_that("loo_crps() is deprecated", { x1 <- matrix(rnorm(50), nrow = 10) x2 <- matrix(rnorm(50), nrow = 10) ll <- matrix(rnorm(50) * 0.1 - 1, nrow = 10) - expect_warning( + lifecycle::expect_deprecated( tryCatch( loo_crps(x1, x2, y, ll), error = function(e) invisible(NULL) - ), - "deprecated", - ignore.case = TRUE + ) ) - expect_warning( + lifecycle::expect_deprecated( tryCatch( loo_scrps(x1, x2, y, ll), error = function(e) invisible(NULL) - ), - "deprecated", - ignore.case = TRUE + ) ) }) @@ -66,7 +58,87 @@ test_that("loo_predictive_metric() is deprecated", { r_eff <- relative_eff(exp(LL), chain_id) x <- matrix(rnorm(length(LL)), nrow = nrow(LL), ncol = ncol(LL)) y <- rnorm(ncol(LL)) - expect_deprecated( + lifecycle::expect_deprecated( loo_predictive_metric(x, y, LL, metric = "mae", r_eff = r_eff) ) }) + +# Equivalence of deprecated and new functions ------------------------------ + +.equiv_data <- function() { + set.seed(3) + S <- 4000 + n <- 20 + y <- rnorm(n) + mu <- matrix(rnorm(S, mean(y), 1 / sqrt(n)), S, n) + list( + y = y, + x1 = mu + matrix(rnorm(S * n), S), + x2 = mu + matrix(rnorm(S * n), S), + ll = dnorm(matrix(y, S, n, byrow = TRUE), mu, 1, log = TRUE) + ) +} + +test_that("elpd() matches measure_elpd()", { + ll <- example_loglik_matrix() + old <- suppressWarnings(elpd(ll)) + new <- measure_elpd(ylp = ll) + expect_equal(unname(new$estimates["elpd", ]), unname(old$estimates["elpd", ])) + expect_equal(unname(new$pointwise[, "elpd"]), unname(old$pointwise[, "elpd"])) +}) + +test_that("loo_predictive_metric() matches the measure functions", { + d <- .equiv_data() + lw <- weights(psis(-d$ll, r_eff = 1), log = TRUE) + new_fun <- list(mae = measure_mae, rmse = measure_rmse, mse = measure_mse) + for (m in names(new_fun)) { + old <- suppressWarnings(loo_predictive_metric(d$x1, d$y, d$ll, metric = m)) + new <- new_fun[[m]](d$y, d$x1, log_weights = lw) + expect_equal(unname(new$estimates[1, ]), c(old$estimate, old$se)) + } + yb <- as.integer(d$y > 0) + xb <- plogis(d$x1) + old <- suppressWarnings(loo_predictive_metric(xb, yb, d$ll, metric = "acc")) + new <- measure_acc(yb, xb, log_weights = lw) + expect_equal(unname(new$estimates[1, ]), c(old$estimate, old$se)) + old <- suppressWarnings( + loo_predictive_metric(xb, yb, d$ll, metric = "balanced_acc") + ) + new <- measure_bacc(yb, xb, log_weights = lw) + expect_equal(new$estimates[1, "Estimate"], old$estimate) +}) + +test_that("crps() and scrps() match measure_rps() and measure_srps()", { + d <- .equiv_data() + old_crps <- suppressWarnings(crps(d$x1, d$x2, d$y)) + old_scrps <- suppressWarnings(scrps(d$x1, d$x2, d$y)) + expect_equal( + measure_rps(d$y, d$x1)$estimates[1, "Estimate"], + -old_crps$estimates[["Estimate"]], + tolerance = 0.01 + ) + expect_equal( + measure_srps(d$y, d$x1)$estimates[1, "Estimate"], + old_scrps$estimates[["Estimate"]], + tolerance = 0.01 + ) +}) + +test_that("loo_crps() and loo_scrps() match loo_pred_measure()", { + d <- .equiv_data() + new <- suppressMessages(loo_pred_measure( + y = d$y, ypred = d$x1, ylp = d$ll, measures = c("rps", "srps") + )) + old_crps <- suppressWarnings(loo_crps(d$x1, d$x2, d$y, d$ll)) + old_scrps <- suppressWarnings(loo_scrps(d$x1, d$x2, d$y, d$ll)) + expect_equal( + new$estimates["rps_loo", "Estimate"], + -old_crps$estimates[["Estimate"]], + tolerance = 0.01 + ) + expect_equal( + new$estimates["srps_loo", "Estimate"], + old_scrps$estimates[["Estimate"]], + tolerance = 0.01 + ) +}) diff --git a/tests/testthat/test_loo_and_waic.R b/tests/testthat/test_loo_and_waic.R index da80671f..a5d276ac 100644 --- a/tests/testthat/test_loo_and_waic.R +++ b/tests/testthat/test_loo_and_waic.R @@ -68,7 +68,7 @@ test_that("mcse_elpd returns NA for an all-zero likelihood column", { test_that("elpd handles negative infinite log likelihoods", { log_lik <- cbind(c(-Inf, 0), c(-Inf, -Inf)) - out <- elpd(log_lik) + out <- suppressWarnings(elpd(log_lik)) expect_equal(out$pointwise[, "elpd"], c(-log(2), -Inf)) }) @@ -249,7 +249,7 @@ test_that("elpd.array and elpd.matrix give same result", { test_that("loo, waic, and elpd error with vector input", { expect_error(loo(LLvec), regexp = "no applicable method") expect_error(waic(LLvec), regexp = "no applicable method") - expect_error(elpd(LLvec), regexp = "no applicable method") + expect_error(suppressWarnings(elpd(LLvec)), regexp = "no applicable method") }) diff --git a/tests/testthat/test_loo_subsampling.R b/tests/testthat/test_loo_subsampling.R index c79b715f..c8d14d19 100644 --- a/tests/testthat/test_loo_subsampling.R +++ b/tests/testthat/test_loo_subsampling.R @@ -1269,6 +1269,6 @@ l1ss <- loo:::as.psis_loo_ss.psis_loo(l1) l2ss <- loo:::as.psis_loo_ss.psis_loo(l2) l3ss <- loo:::as.psis_loo_ss.psis_loo(l3) expect_equal( - unname(loo_compare(l1ss, l2ss, l3ss)[, "elpd_diff"]), - loo_compare(l1, l2, l3)$elpd_diff + unname(suppressWarnings(loo_compare(l1ss, l2ss, l3ss))[, "elpd_diff"]), + suppressWarnings(loo_compare(l1, l2, l3))$elpd_diff ) diff --git a/vignettes/migration-guide.Rmd b/vignettes/migration-guide.Rmd index ec1056ba..3900e1f3 100644 --- a/vignettes/migration-guide.Rmd +++ b/vignettes/migration-guide.Rmd @@ -87,6 +87,11 @@ measure_elpd(log_lik_matrix) and use a permutation-based estimator. The replacement uses **one** draw matrix (`ypred`) and a PWM estimator. Results are not identical. +**Sign change:** `crps()` and `loo_crps()` return the negated CRPS +(higher is better). `measure_rps()` and `loo_pred_measure(measures = "rps")` +return the CRPS (lower is better). `scrps()`, `loo_scrps()`, and +`measure_srps()` all use higher is better. + ```r # Deprecated crps(ypred1, ypred2, y = y)