Repository navigation
Review #363 (4/19): Soft-deprecate old functions with lifecycle - #428
florence-bockting wants to merge 2 commits into
Conversation
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.
jgabry
left a comment
There was a problem hiding this comment.
One important thing and then two small things:
- I think by warning in the generic
loo_comparefunction rstanarm and brms users will still see the warning (see my comment inline) - The new tests showing that
crps()andmeasure_rps()have opposite signs reminds me that we should mention that in the CRPS section ofvignettes/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" intests/testthat/test_crps.R(lines 214-218).
| #' model_compare(loo1, loo2) | ||
| #' | ||
| loo_compare <- function(x, ...) { | ||
| lifecycle::deprecate_soft("3.0.0", "loo_compare()", "model_compare()") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| # 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" | ||
| ) |
There was a problem hiding this comment.
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:
| # 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)) | ||
| }) | ||
|
|
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if cdb47a9 is merged into pred_measure:
Further explanation regarding interpretation and methodology can be found in the documentation. |
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 tolifecycle::deprecate_warn()after brms and rstanarm have a release that callsmodel_compare(). The same plan applies to all 7 deprecated functions (section 2).Implementation:
lifecycleis added toImportsinDESCRIPTION. A user sees the warning only when they call the function directly. A call from brms or rstanarm gives no warning.NEWS.mdnow 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 beforeUseMethod().Implementation:
loo_compare(),crps(),scrps(),loo_crps(),loo_scrps(),elpd()andloo_predictive_metric()..Deprecated(), 2.deprecate_once()) are removed..deprecate_once()is removed fromR/helpers.R.compare(),psislw()andpsis_approximate_posterior()keep.Deprecated(). They are deprecated since earlier releases.The call is in the generic and not in the methods because
crps.numeric()callscrps.matrix(). Adeprecate_soft()call incrps.matrix()sees loo as the caller and does not warn. The same applies toscrps().Tests:
test_deprecated_measures.Ruseslifecycle::expect_deprecated()for all 7 functions. A new test checks thatcrps()andscrps()warn for numeric input. This test fails if the call is incrps.matrix()instead of the generic.test_compare.R: the "once per session" test is replaced by alifecycle::expect_deprecated()test.elpd()vector-input test intest_loo_and_waic.R.3. Old and new functions give the same answer
Review comment: main review
Implementation: new tests in
test_deprecated_measures.Ruse simulated data with 4000 draws, 20 observations and Pareto k below 0.7.elpd()measure_elpd()loo_predictive_metric()withmae,rmse,mse,accmeasure_mae(),measure_rmse(),measure_mse(),measure_acc()with the PSIS log weightsloo_predictive_metric()withbalanced_accmeasure_bacc()crps(),scrps()measure_rps(),measure_srps()loo_crps(),loo_scrps()loo_pred_measure(measures = c("rps", "srps"))loo_compare()model_compare()test_compare.R("loo_compare still returns what model_compare returns")Where the results differ:
crps()estimate has the opposite sign ofrps.crps()is a utility andrpsis a loss.scrps()andsrpshave the same sign.crps()uses one random pairing ofxandx2.measure_rps()uses all pairs of draws inypred.baccSE 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 totaln, 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.Randtest_pred_measure_builtin.Rpass. On the base branch, the 6 deprecation tests fail, and the equivalence tests pass.The 68 warnings in
test_deprecated_measures.Rwere there before this PR. They are PSIS warnings from the smallloo_crps()input in the "loo_crps() is deprecated" test.I used LLM to assist me in this PR.