Skip to content

Review #363 (3/19): Fix pred_measure input checks: psis_object, dims, posthoc, custom names - #426

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

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

Conversation

@florence-bockting

@florence-bockting florence-bockting commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

PR 3/19 for the review of #363.

1. No is.psis() check on psis_object

Review comments: r4544558130, main review

Implementation: a new helper .validate_psis_object() (R/pred_measure-helpers.R) runs at the start of loo_pred_measure() and pred_measure(). A NULL or a psis object passes. Anything else gives an error. If the object is a loo object, the error says to pass it to loo instead. The other wrappers already reject a psis_object.

Tests: a loo object as psis_object in loo_pred_measure() and a list in pred_measure() give the error.

2. A new psis_object replaces the saved weights

Review comment: r4544529644

Decision: an error, with the all.equal() check you suggested.

Implementation: a new helper .check_psis_match() (R/pred_measure-compute.R) is used in .get_psis_object() for both loo and predperf:

  • If log weights are stored, it compares them with all.equal(). A difference gives an error.
  • If only Pareto k values are stored (a loo object with save_psis = FALSE and a separate psis_object), it compares the k values. It skips observations with k = 0, because brms::reloo() sets k to 0 for refitted observations and sets no attribute. It skips the check if loo has a posthoc attribute (moment matching changes k).

Before, a loo object with save_psis = FALSE plus a psis_object always gave an error, because the code compared the weights with NULL. That case now works.

Tests: different weights in pred_measure() give the error, the same weights work. A save_psis = FALSE loo object with a matching psis_object works, with different k values it gives the error.

3. No post-hoc warning when measures are added

Review comment: r4544507747

Implementation: pred_measure() gets no loo object, so it cannot detect the post-hoc method again. The method is now detected once when the result is created and stored as attr(x, "posthoc"). .warn_posthoc() takes the method instead of the loo object and runs for new results and for updates. .detect_posthoc() reads the k values from the psis_object in use, so reloo is also detected when the weights come from a separate psis_object.

Tests: after moment matching, loo_pred_measure(measures = "elpd") gives no warning and stores the attribute. Adding "mae" with pred_measure() then gives the warning. A reloo case with a separate psis_object gives the warning too.

4. dims is unreliable, and N is not checked

Review comments: r4544658480, r4544661337, r4544939581, main review

Implementation:

  • A new helper .pred_measure_dims() (R/pred_measure-helpers.R) runs once at the start of do_pred_measure(). It reads the number of draws S and observations N from every input: y, ypred, mupred, ylp, ylp_test, the psis_object weights, the loo and kfold objects, and the stored dims of predperf. If two inputs disagree, it gives one error that lists each input and its size. It returns c(S, N). S is NA if no input has draws (a kfold object only). A later update with draws fills it in. For test_pred_measure(), the training ylp has a different N and is not read.
  • .add_attributes() sets dims from this value. Its two dims blocks and the five arguments that only fed them are removed.
  • print() shows "Computed from N observations." if S is NA. The fallback to the dimensions of log_weights is removed.
  • ps_khat_threshold() (R/diagnostics.R) returns the 0.7 cap if S is NA, so diag_elpd() in model_compare() does not fail. The other callers always know S.

Limit: a transposed matrix with S = N cannot be detected.

Tests: dims is c(S, N) for in-sample, LOO from a psis_object only, k-fold (c(NA, N), and the print header), and a k-fold update. A transposed ylp gives the error for observations. A ypred with fewer draws gives the error for draws, also in pred_measure(). Two results from a psis_object only compare with model_compare() without an error. ps_khat_threshold(NA) is 0.7.

5. custom_measure() does not check name

Review comment: r4544492688

Implementation: the checks are in .measure_entry_custom() (R/pred_measure-helpers.R), not in custom_measure(). Both custom_measure(fun, name) and a list name such as measures = list(elpd = fun) go through this function. The name must be a single non-empty string (checkmate::test_string()), and must not be a built-in measure or "p". custom_measure() uses cli::cli_abort() for the fun check.

Tests: 1, NA, "", NULL and c("a", "b") give the "needs a name" error. "elpd", "rmse", "srps", "p" and list(elpd = fun) give the "reserved" error.

Test runs

test_pred_measure.R, test_pred_measure_helpers.R, test_pred_measure_builtin.R, test_compare.R and test_print_plot.R pass. The new tests in test_pred_measure.R, test_pred_measure_helpers.R, test_compare.R and test_print_plot.R fail on the base branch. The exception is the .get_psis_object() mismatch test: the old code already gave an error there, with a different message.

This PR was assisted by use of LLM.

@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@               Coverage Diff                @@
##           pred_measure     #426      +/-   ##
================================================
+ Coverage         91.89%   92.03%   +0.13%     
================================================
  Files                38       38              
  Lines              4751     4796      +45     
================================================
+ Hits               4366     4414      +48     
+ Misses              385      382       -3     

☔ 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

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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

Status Name Current Incoming Interval
✔️ loo_matrix 1.36s 1.36s [-1.15%, +0.79%]
✔️ loo_function 1.19s 1.19s [-1.25%, +1.39%]

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

@florence-bockting
florence-bockting marked this pull request as ready for review November 24, 2026 09:57

@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.

Thanks, I think this is in good shape. I made a couple of suggestions.

Comment thread R/pred_measure-helpers.R
ylp_test = ylp_test,
psis_object = psis_object$log_weights
)
draws <- draws[!vapply(draws, is.null, logical(1L))]

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.

Do we want to support mupred or ypred being a vector? If mupred or ypred is a vector, this line keeps it that way, but then nrow() below at line 361 returns NULL, so the vapply() at line 361 errors (values must be length 1, but FUN(X[[1]]) result is length 0). The measure_*() docs say a length-n vector is accepted, and there's a message about "Coercing mupred from vector to 1 x n matrix". For a vector ypred, the old error (ypred must be a numeric matrix or array) I think was more helpful too.

I think skipping inputs that have no dimensions fixes both cases and lets the later checks throw the errors:

Suggested change
draws <- draws[!vapply(draws, is.null, logical(1L))]
draws <- Filter(function(x) length(dim(x)) >= 2L, draws)

Comment thread R/diagnostics.R
#' @noRd
ps_khat_threshold <- function(S, ...) {
min(1 - 1 / log10(S), 0.7)
min(1 - 1 / log10(S), 0.7, na.rm = TRUE)

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.

What's the reason for the NA handling here? I'm not actually sure that we can get an NA the way things currently are coded. Not a big deal either way, just curious.

Comment thread R/pred_measure-compute.R
if (source == "loo") {
if (is.null(predperf)) {
if (!is.null(loo) && is.null(loo$psis_object)) {
if (!is.null(loo) && is.null(loo$psis_object) && is.null(psis_object)) {

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.

Before this PR, a loo without a psis_object plus a separate psis_object errored with No psis_object found in loo, and I think that's probably fine.

Supporting this seems to require a bunch of things:

  • the Pareto k branch in .check_psis_match() (L422-426), including the k = 0 skip for reloo
  • the pareto_k line at L369 of this file, which skips the check for posthoc
  • the new psis_object argument to .detect_posthoc() (L290 and L293)
  • the test at L406-424 in test_pred_measure.R and the second half of the test at L124 (L134-143)

Is there a use case for this? If so that's fine. Who would have a matching psis_object but not have it saved in the loo object? Am I overlooking something? If there's not a real use case for this then I'd lean towards keeping the old error. And then .check_psis_match() only needs to compare log weights.

Comment thread R/pred_measure-compute.R
if (is.null(log_weights)) {
log_weights <- predperf$psis_object$log_weights
}
.check_psis_match(psis_object, log_weights, NULL, "predperf")

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 a plain pred_measure() update, above on L133-136 it takes psis_object from predperf. Then in this check it compares the stored weights with themselves, so every update runs all.equal() on the full S x N matrix, which adds a little bit of overhead. It's not a lot, so not a big deal, but maybe only run the check when the user actually passed a psis_object?

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