diff --git a/R/model_compare-pred_measure.R b/R/model_compare-pred_measure.R index ca753b80..c13f4aeb 100644 --- a/R/model_compare-pred_measure.R +++ b/R/model_compare-pred_measure.R @@ -86,6 +86,7 @@ compare_pred_measure <- function(loos) { rank_measure <- .resolve_rank_measure(loos) compare_cols <- .compare_pointwise_cols(loos) custom_se_diffs <- .resolve_custom_se_diffs(loos, compare_cols) + names(loos) <- find_model_names(loos) ord <- model_compare_order(loos, rank_measure$internal) loos_ord <- loos[ord] @@ -215,7 +216,7 @@ compare_pred_measure <- function(loos) { ref <- non_null[[1L]] inconsistent <- vapply( non_null[-1L], - function(info) !identical(info, ref), + function(info) !identical(info, ref, ignore.environment = TRUE), logical(1) ) if (any(inconsistent)) { @@ -302,13 +303,9 @@ throw_omitted_compare_measures_warning <- function(loos) { } #' Is a measure an ELPD-family measure (for `p_worse` / `diag_diff`)? -#' -#' Matches on the raw column name: every source suffix (`elpd_loo`, -#' `elpd_kfold`, `elpd_test`, bare `elpd`) shares the `elpd` prefix, so no -#' suffix stripping is needed here. #' @noRd .is_elpd_measure <- function(name) { - grepl("^elpd", name) + name %in% c("elpd", "elpd_loo", "elpd_kfold", "elpd_test") } #' Look up the per-measure information recorded on a result object @@ -405,8 +402,7 @@ throw_omitted_compare_measures_warning <- function(loos) { ok <- length(pw) > 0L && is.finite(est) && is.finite(agg) if (ok) { - tol <- sqrt(.Machine$double.eps) * max(abs(c(est, pw)), na.rm = TRUE) - ok <- isTRUE(all.equal(est, agg, tolerance = tol, check.attributes = FALSE)) + ok <- abs(est - agg) <= sqrt(.Machine$double.eps) * max(abs(est), abs(agg)) } if (!ok) { warning( @@ -431,9 +427,6 @@ throw_omitted_compare_measures_warning <- function(loos) { info <- .get_measure_info(loos, bare) fun <- info$se_diff_fun - if (is.null(fun)) { - fun <- .measure_spec[[bare]]$se_diff_fun - } if (is.character(fun)) { fun <- .se_diff_funs[[fun]] } diff --git a/R/model_compare.R b/R/model_compare.R index 99ffc806..de0fd2d1 100644 --- a/R/model_compare.R +++ b/R/model_compare.R @@ -328,16 +328,9 @@ model_compare.default <- function(x, ...) { } #' Reference model a measure's differences were computed against -#' -#' Each measure has its own best model as reference, recorded in attribute `compare_reference`. Falls back to the first row for objects -#' created before that attribute existed. #' @noRd .measure_ref_model <- function(x, measure) { - refs <- attr(x, "compare_reference") - if (!is.null(refs) && measure %in% names(refs)) { - return(refs[[measure]]) - } - x$model[[1L]] + attr(x, "compare_reference")[[measure]] } #' Normalize `model_compare()` inputs to a list of model results @@ -523,8 +516,12 @@ find_model_names <- function(x) { #' @noRd .model_compare_estimates_table <- function(loos, bare_names = FALSE, subsampling = FALSE) { + shared <- if (bare_names) { + Reduce(intersect, lapply(loos, function(x) rownames(x$estimates))) + } sapply(loos, function(x) { est <- x$estimates + if (bare_names) est <- est[shared, , drop = FALSE] rows <- if (bare_names) .display_name(rownames(est), loos) else rownames(est) nms <- c(rows, paste0("se_", rows)) # A `psis_loo_ss` object carries a third estimate column, the subsampling diff --git a/R/model_compare.psis_loo_ss_list.R b/R/model_compare.psis_loo_ss_list.R index 1a2bffa5..ed59acb8 100644 --- a/R/model_compare.psis_loo_ss_list.R +++ b/R/model_compare.psis_loo_ss_list.R @@ -32,6 +32,7 @@ model_compare.psis_loo_ss_list <- function(x, ...) { ) rownames(comp) <- NULL + attr(comp, "compare_reference") <- c(elpd = rnms[[1L]]) class(comp) <- c("compare.loo", "data.frame") return(comp) } diff --git a/tests/testthat/test_compare.R b/tests/testthat/test_compare.R index 266a436c..51240e51 100644 --- a/tests/testthat/test_compare.R +++ b/tests/testthat/test_compare.R @@ -107,8 +107,76 @@ test_that("model_compare warns when predictive measures differ across models", { "Omitted measures: mae \\(m2\\), mse \\(m1\\)" ) expect_equal(attr(comp, "compare_measures"), c("elpd", "r2")) - expect_false("mse_diff" %in% colnames(comp)) - expect_false("mae_diff" %in% colnames(comp)) + expect_false(any(c("mse", "mae", "mse_diff", "mae_diff") %in% colnames(comp))) + expect_equal(comp$r2[comp$model == "m1"], unname(pm1$estimates["r2_loo", "Estimate"])) + expect_equal(comp$r2[comp$model == "m2"], unname(pm2$estimates["r2_loo", "Estimate"])) +}) + +test_that("model_compare matches estimate columns by measure name", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + pm1 <- loo_pred_measure( + loo = res$loo_p_m1, + y = res$y, + mupred = res$mupred_m1, + ylp = res$ylp_m1, + measures = c("elpd", "r2", "mse") + ) + pm2 <- loo_pred_measure( + loo = res$loo_p_m2, + y = res$y, + mupred = res$mupred_m2, + ylp = res$ylp_m2, + measures = c("mse", "elpd", "r2") + ) + pm3 <- loo_pred_measure( + loo = res$loo_p_m2, + y = res$y, + mupred = res$mupred_m2, + ylp = res$ylp_m2, + measures = c("elpd", "r2") + ) + + comp <- suppressMessages(model_compare(list(m1 = pm1, m2 = pm2))) + m1 <- comp$model == "m1" + expect_equal(comp$r2[m1], unname(pm1$estimates["r2_loo", "Estimate"])) + expect_equal(comp$mse[m1], unname(pm1$estimates["mse_loo", "Estimate"])) + expect_equal(comp$elpd[m1], unname(pm1$estimates["elpd_loo", "Estimate"])) + + expect_warning( + comp3 <- suppressMessages(model_compare(list(m1 = pm1, m3 = pm3))), + "Omitted measures: mse \\(m1\\)" + ) + expect_equal(comp3$r2[comp3$model == "m1"], unname(pm1$estimates["r2_loo", "Estimate"])) +}) + +test_that("model_compare compares loo and waic objects", { + LL <- example_loglik_matrix() + comp <- suppressWarnings(model_compare(loo(LL), waic(LL - 1))) + expect_s3_class(comp, "compare.loo") + expect_equal(nrow(comp), 2L) +}) + +test_that("model_compare names unnamed models by input position", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + unnamed <- function(x) { + attr(x, "model_name") <- NULL + x + } + pm1 <- unnamed(loo_pred_measure( + loo = unnamed(res$loo_p_m1), + y = res$y, + ylp = res$ylp_m1, + measures = "elpd" + )) + pm2 <- unnamed(loo_pred_measure( + loo = unnamed(res$loo_p_m2), + y = res$y, + ylp = res$ylp_m2, + measures = "elpd" + )) + + comp <- suppressMessages(model_compare(pm1, pm2)) + expect_equal(comp$model, c("model2", "model1")) }) test_that("model_compare works with three loo_pred_measure models", { @@ -728,6 +796,113 @@ test_that("bacc reports the difference without an se when the strata are gone", expect_false(is.na(mixed_stale_first$bacc_se_diff[2L])) }) +test_that("model_compare accepts the same se_diff_fun from other environments", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + my_mae <- function(y, mupred) { + ae_i <- abs(y - colMeans(mupred)) + list(estimate = mean(ae_i), se = sd(ae_i) / sqrt(length(ae_i)), + pointwise = ae_i) + } + make_measure <- function(loss = FALSE) { + custom_measure(my_mae, name = "my_mae", + se_diff_fun = function(ref, cmp) 0, loss = loss) + } + make <- function(loo, mupred, ylp, fun) { + loo_pred_measure( + loo = loo, y = res$y, mupred = mupred, ylp = ylp, measures = fun + ) + } + pm1 <- make(res$loo_p_m1, res$mupred_m1, res$ylp_m1, make_measure()) + pm2 <- make(res$loo_p_m2, res$mupred_m2, res$ylp_m2, make_measure()) + expect_no_error(suppressMessages(model_compare(pm1, pm2))) + + path <- tempfile(fileext = ".rds") + saveRDS(pm2, path) + expect_no_error(suppressMessages(model_compare(pm1, readRDS(path)))) + + pm_loss <- make(res$loo_p_m2, res$mupred_m2, res$ylp_m2, + make_measure(loss = TRUE)) + expect_error(suppressMessages(model_compare(pm1, pm_loss)), "Models disagree") +}) + +test_that("a custom measure whose name starts with elpd is not an elpd measure", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + my_mae <- function(y, mupred) { + ae_i <- abs(y - colMeans(mupred)) + list(estimate = mean(ae_i), se = sd(ae_i) / sqrt(length(ae_i)), + pointwise = ae_i) + } + measure <- custom_measure(my_mae, name = "elpd_custom", se_diff_fun = "mean") + make <- function(loo, mupred, ylp) { + loo_pred_measure( + loo = loo, y = res$y, mupred = mupred, ylp = ylp, + measures = list("elpd", measure) + ) + } + comp <- suppressMessages(model_compare( + make(res$loo_p_m1, res$mupred_m1, res$ylp_m1), + make(res$loo_p_m2, res$mupred_m2, res$ylp_m2) + )) + expect_true(all(c("elpd_custom_diff", "elpd_custom_se_diff") %in% colnames(comp))) +}) + +test_that("a wrong aggregation declaration warns for large values", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + big <- function(y, mupred) { + sqe_i <- (1e4 * (y - colMeans(mupred)))^2 + list(estimate = 1.5 * mean(sqe_i), se = 1, pointwise = sqe_i) + } + measure <- custom_measure(big, name = "big", se_diff_fun = "mean") + make <- function(loo, mupred, ylp) { + loo_pred_measure( + loo = loo, y = res$y, mupred = mupred, ylp = ylp, measures = measure + ) + } + pm1 <- make(res$loo_p_m1, res$mupred_m1, res$ylp_m1) + pm2 <- make(res$loo_p_m2, res$mupred_m2, res$ylp_m2) + expect_warning( + suppressMessages(model_compare(pm1, pm2)), + "does not reproduce its estimate" + ) +}) + +test_that("a wrong aggregation declaration warns for small values", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + tiny <- function(y, mupred) { + sqe_i <- (1e-8 * (y - colMeans(mupred)))^2 + list(estimate = 1.5 * mean(sqe_i), se = 1, pointwise = sqe_i) + } + measure <- custom_measure(tiny, name = "tiny", se_diff_fun = "mean") + make <- function(loo, mupred, ylp) { + loo_pred_measure( + loo = loo, y = res$y, mupred = mupred, ylp = ylp, measures = measure + ) + } + pm1 <- make(res$loo_p_m1, res$mupred_m1, res$ylp_m1) + pm2 <- make(res$loo_p_m2, res$mupred_m2, res$ylp_m2) + expect_warning( + suppressMessages(model_compare(pm1, pm2)), + "does not reproduce its estimate" + ) +}) + +test_that("a correct aggregation declaration does not warn for small values", { + res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") + tiny <- function(y, mupred) { + sqe_i <- (1e-8 * (y - colMeans(mupred)))^2 + list(estimate = mean(sqe_i) * (1 + 1e-14), se = 1, pointwise = sqe_i) + } + measure <- custom_measure(tiny, name = "tiny", se_diff_fun = "mean") + make <- function(loo, mupred, ylp) { + loo_pred_measure( + loo = loo, y = res$y, mupred = mupred, ylp = ylp, measures = measure + ) + } + pm1 <- make(res$loo_p_m1, res$mupred_m1, res$ylp_m1) + pm2 <- make(res$loo_p_m2, res$mupred_m2, res$ylp_m2) + expect_no_warning(suppressMessages(model_compare(pm1, pm2))) +}) + test_that("custom measures take their se_diff from their declaration", { res <- readRDS("data-for-tests/test_data_roaches_compare.Rds") diff --git a/tests/testthat/test_loo_subsampling.R b/tests/testthat/test_loo_subsampling.R index 795cbf73..c79b715f 100644 --- a/tests/testthat/test_loo_subsampling.R +++ b/tests/testthat/test_loo_subsampling.R @@ -1021,6 +1021,7 @@ test_that("model_compare_subsample", { expect_snapshot( lcss <- loo:::model_compare.psis_loo_ss_list(x = list(lss1, lss2, lss3)) ) + expect_equal(attr(lcss, "compare_reference"), c(elpd = lcss$model[[1L]])) expect_warning( lcss2 <- loo:::model_compare.psis_loo_ss_list(x = list(lss1, lss2, lss3o1)) )