Skip to content

Add held-out R2 to performance_accuracy - #1042

Open
M-Colley wants to merge 3 commits into
easystats:mainfrom
M-Colley:fix/364-held-out-r2
Open

M-Colley wants to merge 3 commits into
easystats:mainfrom
M-Colley:fix/364-held-out-r2

Conversation

@M-Colley

@M-Colley M-Colley commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #364.

  • Add metric = "r2" for linear models with method = "cv". Calculate the score in each test fold and summarize the finite scores with the existing return fields.
  • Keep the existing Pearson correlation and AUC defaults. Document negative scores and undefined scores for constant test responses.

Evidence

  • A seeded cross-validation regression compares the mean, spread, and interval with a direct calculation. It also verifies a negative fold score and agreement with performance_cv(stack = FALSE).
  • 42 targeted assertions passed across accuracy, AUC, and cross-validation. Six model/method combinations produced identical default results on the base and revision. A mixed-model score matched the direct fold calculation.
  • The source package check returned Status: OK with examples enabled. The command used --no-manual --ignore-vignettes --no-tests.

Merge Danger

Door Two-way

The new score is opt-in through a trailing argument.

@M-Colley

M-Colley commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

This PR fixes #364 by adding an opt-in held-out R2 for linear-model cross-validation. It computes the score within each test fold and keeps the existing defaults. The regression tests compare the scores and intervals with a direct calculation.

@M-Colley
M-Colley force-pushed the fix/364-held-out-r2 branch from 9c62a1e to e12d3db Compare December 4, 2026 08:43
@M-Colley

M-Colley commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased the fix for #364 onto current main, which includes the recent test repairs. All 13 focused assertions, installed-package coverage, and lint passed locally.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A moderate active issue remains because the guard can allow overflow to produce a non-finite score that is not filtered before aggregation.

1 open issue
1 file not reviewed
  • man/performance_accuracy.Rd: Generated file
What changed in this PR

Adds opt-in held-out R² scoring to performance_accuracy() for linear models using cross-validation while preserving existing correlation and AUC defaults.

Changes:

  • Adds fold-level R² calculation with finite-score summarization and validation.
  • Adds tests for R² behavior, edge cases, and backward compatibility.
  • Updates documentation, NEWS, and the development version.
File Description
tests/​testthat/​test-performance_accuracy.R Adds tests for R² calculations, validation, edge cases, and default compatibility.
R/​performance_accuracy.R Implements metric selection and held-out R² scoring for cross-validation.
NEWS.md Documents the addition of held-out R² scoring.
man/​performance_accuracy.Rd Documents the new metric argument and R² behavior.
DESCRIPTION Increments the package development version.

Review effort: Lite

Comment thread R/performance_accuracy.R Outdated
Comment on lines +296 to +299
if (total == 0 || !is.finite(total) || !is.finite(error)) {
return(NA_real_)
}
1 - error / total
@strengejacke

Copy link
Copy Markdown
Member

Minimal change requested here - not sure if this really can become an issue? You may ignore this request.

@strengejacke strengejacke added the Revisions required 💥 Review has been submittted and changed requested label Dec 4, 2026

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

Revisions required 💥 Review has been submittted and changed requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

performance_accuracy: cross-validated R2 vs cor(pred, resp)

3 participants