Skip to content

Review #363 (4/19): Soft-deprecate old functions with lifecycle - #428

Open
florence-bockting wants to merge 2 commits into
pred_measurefrom
fix/pred-measure-deprecation
Open

florence-bockting wants to merge 2 commits into
pred_measurefrom
fix/pred-measure-deprecation

Conversation

@florence-bockting

Copy link
Copy Markdown
Contributor

PR 4/19 for the review of #363.

1. Soft deprecation until brms and rstanarm are updated

Review comments: r4552563077, main review

Decision: lifecycle::deprecate_soft() now. We change to lifecycle::deprecate_warn() after brms and rstanarm have a release that calls model_compare(). The same plan applies to all 7 deprecated functions (section 2).

Implementation: lifecycle is added to Imports in DESCRIPTION. A user sees the warning only when they call the function directly. A call from brms or rstanarm gives no warning. NEWS.md now says "Soft-deprecate".

2. One deprecation mechanism

Review comments: r4552580093, r4552601476, r4552665625, r4552666608, r4552667491, r4552673867, r4552675331, r4552704181, main review

Decision: lifecycle::deprecate_soft("3.0.0", ...) for all of them, in the generic before UseMethod().

Implementation:

  • The call is in the 7 generics: loo_compare(), crps(), scrps(), loo_crps(), loo_scrps(), elpd() and loo_predictive_metric().
  • The 9 calls in the methods (7 .Deprecated(), 2 .deprecate_once()) are removed. .deprecate_once() is removed from R/helpers.R.
  • compare(), psislw() and psis_approximate_posterior() keep .Deprecated(). They are deprecated since earlier releases.

The call is in the generic and not in the methods because crps.numeric() calls crps.matrix(). A deprecate_soft() call in crps.matrix() sees loo as the caller and does not warn. The same applies to scrps().

Tests:

  • test_deprecated_measures.R uses lifecycle::expect_deprecated() for all 7 functions. A new test checks that crps() and scrps() warn for numeric input. This test fails if the call is in crps.matrix() instead of the generic.
  • test_compare.R: the "once per session" test is replaced by a lifecycle::expect_deprecated() test.
  • Two tests that call a deprecated function now suppress the warning: "loo_compare is still a generic" and the elpd() vector-input test in test_loo_and_waic.R.

3. Old and new functions give the same answer

Review comment: main review

Implementation: new tests in test_deprecated_measures.R use simulated data with 4000 draws, 20 observations and Pareto k below 0.7.

Old New Check
elpd() measure_elpd() estimate, SE and pointwise are equal
loo_predictive_metric() with mae, rmse, mse, acc measure_mae(), measure_rmse(), measure_mse(), measure_acc() with the PSIS log weights estimate and SE are equal
loo_predictive_metric() with balanced_acc measure_bacc() estimate is equal
crps(), scrps() measure_rps(), measure_srps() estimate, relative tolerance 0.01
loo_crps(), loo_scrps() loo_pred_measure(measures = c("rps", "srps")) estimate, relative tolerance 0.01
loo_compare() model_compare() already tested in test_compare.R ("loo_compare still returns what model_compare returns")

Where the results differ:

  • The crps() estimate has the opposite sign of rps. crps() is a utility and rps is a loss. scrps() and srps have the same sign.
  • The (s)crps functions use a tolerance because they estimate E|X − X′| differently. crps() uses one random pairing of x and x2. measure_rps() uses all pairs of draws in ypred.
  • The bacc SE differs. measure_bacc() uses the SE per class, sqrt(sum_c p_c (1 − p_c) / n_c) / K. The old .balanced_accuracy() divides by the total n, and its code comment says that this approximation is biased.

Test runs

test_deprecated_measures.R, test_compare.R, test_loo_and_waic.R, test_crps.R, test_loo_predictive_metric.R, test_model_weighting.R, test_loo_subsampling.R, test_pred_measure.R, test_pred_measure_helpers.R and test_pred_measure_builtin.R pass. On the base branch, the 6 deprecation tests fail, and the equivalence tests pass.

The 68 warnings in test_deprecated_measures.R were there before this PR. They are PSIS warnings from the small loo_crps() input in the "loo_crps() is deprecated" test.

I used LLM to assist me in this PR.

Call lifecycle::deprecate_soft() before UseMethod() in loo_compare(), crps(), scrps(), loo_crps(), loo_scrps(), elpd(), and loo_predictive_metric(). Remove .deprecate_once(). Add tests that compare the old and new functions.
@florence-bockting
florence-bockting changed the base branch from master to pred_measure November 24, 2026 11:46
@florence-bockting
florence-bockting marked this pull request as ready for review November 24, 2026 12:06

@jgabry jgabry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One important thing and then two small things:

  • I think by warning in the generic loo_compare function rstanarm and brms users will still see the warning (see my comment inline)
  • The new tests showing that crps() and measure_rps() have opposite signs reminds me that we should mention that in the CRPS section of vignettes/migration-guide.Rmd. Right now it only says the results aren't identical and a sign change is easy to miss.
  • Two tests still let a deprecation warning through: "elpd handles negative infinite log likelihoods" in tests/testthat/test_loo_and_waic.R (line 71) and "methods for single data point pass permutations to matrix methods" in tests/testthat/test_crps.R (lines 214-218).

Comment thread R/loo_compare.R
#' model_compare(loo1, loo2)
#'
loo_compare <- function(x, ...) {
lifecycle::deprecate_soft("3.0.0", "loo_compare()", "model_compare()")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to go in the loo methods instead of the generic function. brms and rstanarm re-export the loo_compare() generic function, so when a user calls loo_compare(fit1, fit2) they'll get a warning telling them to use model_compare, which doesn't exist.

If you do the deprecation in loo_compare.default() and loo_compare.psis_loo_ss_list(), then when rstanarm and brms internally call loo_compare the lifecycle package can tell it's through the internal rstanarm or brms namespace and it doesn't throw the warning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the other functions besides loo_compare (e.g., loo_scrps, etc) I think it's ok to leave the deprecations in the generic functions

Comment on lines 1730 to +1736
# methods registered elsewhere (e.g. brms, rstanarm) keep dispatching
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")
expect_identical(
suppressWarnings(loo_compare(structure(list(), class = "fake_fit"))),
"dispatched"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suppressWarnings() here was hiding the brms/rstanarm problem (see my comment on R/loo_compare.R). But if we move the deprecation into the loo methods instead of the generic, then this test can test that methods from other packages don't warn. (I think, but am not 100% sure, that expect_no_warning() ignores deprecation warnings unless you give it the class, so could use that.) Something like this maybe:

Suggested change
# methods registered elsewhere (e.g. brms, rstanarm) keep dispatching
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")
expect_identical(
suppressWarnings(loo_compare(structure(list(), class = "fake_fit"))),
"dispatched"
)
# 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)
out <- expect_no_warning(
loo_compare(structure(list(), class = "fake_fit")),
class = "lifecycle_warning_deprecated"
)
expect_identical(out, "dispatched")

lifecycle::expect_deprecated(crps(rnorm(10), rnorm(10), 0.5))
lifecycle::expect_deprecated(scrps(rnorm(10), rnorm(10), 0.5))
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test can actually catch the case from the PR description. I moved the deprecate_soft() call from crps() into crps.matrix() and this test still passes. That's because of how the lifecycle package works. When the loo package tests are running, I think the lifecycle package treats any call from loo's namespace as direct, so that means crps.numeric() calling crps.matrix() counts as direct and warns.

I think this is just an issue in the tests, not the code, and outside the tests it's true that with the deprecation in crps.matrix() then running crps(rnorm(10), rnorm(10), 0.5) doesn't warn.

So I think we can remove this test (and the sentence about it in the description). Optionally maybe put a small comment in crps() if we want to remember this, but not necessary

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.96%. Comparing base (99f83b2) to head (cdb47a9).

Additional details and impacted files
@@               Coverage Diff                @@
##           pred_measure     #428      +/-   ##
================================================
+ Coverage         91.89%   91.96%   +0.07%     
================================================
  Files                38       38              
  Lines              4751     4743       -8     
================================================
- Hits               4366     4362       -4     
+ Misses              385      381       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if cdb47a9 is merged into pred_measure:

Status Name Current Incoming Interval
✔️ loo_matrix 1.37s 1.37s [-0.23%, +0.13%]
✔️ loo_function 1.26s 1.26s [-0.13%, +0.09%]

Further explanation regarding interpretation and methodology can be found in the documentation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants