Skip to content

Review #363 (2/19): fix model_compare() estimates by name, default names, measure check - #424

Open
florence-bockting wants to merge 5 commits into
pred_measurefrom
fix/model-compare-bugs
Open

florence-bockting wants to merge 5 commits into
pred_measurefrom
fix/model-compare-bugs

Conversation

@florence-bockting

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

Copy link
Copy Markdown
Contributor

PR 2/19 for the review of #363. This PR does not touch the files of PR 1/19: #423.

1. Estimate columns filled by position

Review comments: r4544927101, r4553271356

Implementation: .model_compare_estimates_table() (R/model_compare.R) subsets each model's estimates by name to the measures that all models share. The row order follows the first model. This applies only with bare_names = TRUE, which is the pred_measure path. Classic loo/waic/kfold objects keep the match by position, so their output does not change.

Tests: The test from the review checks that mse and mae are not in the output. The old code gave m1 a mae column with m1's mse value. A new test uses a different measure order and a different number of measures. It checks the m1 row, which the old code filled wrong. The second case gives the omitted-measures warning and no longer crashes.

2. Default model names assigned after sorting

Review comment: r4544947628

Implementation: compare_pred_measure() sets names(loos) <- find_model_names(loos) before the sort, as the classic path does.

Test: unnamed inputs where the second model is best give c("model2", "model1"), as loo_compare() does.

3. identical() on se_diff_fun closures

Review comment: r4544950797

Decision: ignore.environment = TRUE. It stays an error, not a warning, because a real difference (for example in loss) still means the models disagree. Limit: two closures with the same code but different captured values look equal.

Test: the same custom measure made in a helper function, and after saveRDS()/readRDS(), compares without an error. A different loss still errors.

4. grep("elpd") matches custom measures

Review comment: r4545391902

Implementation: .is_elpd_measure() matches elpd, elpd_loo, elpd_kfold and elpd_test exactly.

Test: a custom measure named elpd_custom gets its _diff and _se_diff columns. With the old function, both columns were missing.

5. Tolerance scaled twice

Review comment: r4545429584

Implementation: .check_declared_aggregation() uses a relative tolerance, sqrt(.Machine$double.eps), scaled by the larger of the two values. all.equal() used an absolute difference for values below 1.5e-8.

Test: a measure declared "mean" whose estimate is 1.5 times the mean, with values around 3e11, now gives the warning. The old tolerance was about 4300, so it missed this. Values scaled by 1e-8 with a correct declaration give no warning. The old tolerance warned there. A wrong declaration with values scaled by 1e-8 now gives the warning.

6. Fallback returns NULL again

Review comment: r4545434713

Implementation: the fallback in .measure_se_diff_fun() is removed. .measure_info() already copies se_diff_fun from .measure_spec.

7. compare_reference not set for subsampling

Review comments: r4544874307, r4545558077, r4544910468

Implementation: model_compare.psis_loo_ss_list() sets compare_reference as the classic path does. The fallback in .measure_ref_model() and its doc sentence are removed.

Test: in test_loo_subsampling.R, a subsampling comparison has compare_reference.

Test runs

test_compare.R, test_print.R and test_loo_subsampling.R pass. The 3 warnings in test_compare.R that are not caught were there before and will be addressed by a later PR.

This PR was assisted by use of LLM.

@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.88%. Comparing base (eaebc86) to head (e48d2d6).

Additional details and impacted files
@@               Coverage Diff                @@
##           pred_measure     #424      +/-   ##
================================================
- Coverage         92.03%   91.88%   -0.16%     
================================================
  Files                38       38              
  Lines              5201     4745     -456     
================================================
- Hits               4787     4360     -427     
+ Misses              414      385      -29     

☔ 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 23, 2026 •

Copy link
Copy Markdown
Contributor

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

Status Name Current Incoming Interval
✔️ loo_matrix 1.37s 1.38s [-0.8%, +0.95%]
✔️ loo_function 1.22s 1.21s [-2.43%, +1.98%]

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 23, 2026 13:16

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

Looks good. A few small suggestions in comments.

One small thing in the PR description: it says this also fixes model_compare_order(), but after the second commit the name matching only applies when bare_names = TRUE. model_compare_order() calls the helper function with bare_names = FALSE or it already looks up rank_col by name, so I don't think it's affected (and it also didn't have the bug).

Comment thread tests/testthat/test_compare.R Resolved
Comment thread tests/testthat/test_compare.R Resolved
Comment thread tests/testthat/test_compare.R Resolved
Comment thread R/model_compare-pred_measure.R Resolved
@florence-bockting

Copy link
Copy Markdown
Contributor Author

One small thing in the PR description: it says this also fixes model_compare_order(), but after the second commit the name matching only applies when bare_names = TRUE. model_compare_order() calls the helper function with bare_names = FALSE or it already looks up rank_col by name, so I don't think it's affected (and it also didn't have the bug).

Thanks for spotting this, I updated the PR description.

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