Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Depends:
Imports:
checkmate,
cli (>= 3.4.0),
lifecycle (>= 1.0.0),
matrixStats (>= 0.52),
parallel,
posterior (>= 1.7.0),
Expand Down
8 changes: 4 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
- 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
- compares each measure against its own best model
- 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
Expand Down
8 changes: 4 additions & 4 deletions R/crps.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,36 @@
#' 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")
}


#' @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")
}


#' @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
Expand Down Expand Up @@ -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, ...),
Expand All @@ -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
Expand Down Expand Up @@ -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, ...),
Expand Down
3 changes: 1 addition & 2 deletions R/elpd.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#' }
#'
elpd <- function(x, ...) {
lifecycle::deprecate_soft("3.0.0", "elpd()", "measure_elpd()")
UseMethod("elpd")
}

Expand All @@ -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)
}
Expand All @@ -52,7 +52,6 @@ elpd.array <- function(x, ...) {
#' @template matrix
#'
elpd.matrix <- function(x, ...) {
.Deprecated("measure_elpd")
.elpd_matrix_impl(x)
}

Expand Down
27 changes: 0 additions & 27 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
8 changes: 4 additions & 4 deletions R/loo_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()].
Expand Down Expand Up @@ -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(
Expand All @@ -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, ...)
}
4 changes: 3 additions & 1 deletion R/loo_predictive_metric.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion man/loo_compare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 10 additions & 17 deletions tests/testthat/test_compare.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_crps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
104 changes: 88 additions & 16 deletions tests/testthat/test_deprecated_measures.R
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down Expand Up @@ -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", {
Expand All @@ -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
)
)
})

Expand All @@ -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
)
})
Loading
Loading