Skip to content

Collapse tied predictions in performance_roc() - #1017

Merged
strengejacke merged 3 commits into
easystats:mainfrom
finite-sample:fix/performance-roc-ties
Nov 24, 2026
Merged

strengejacke merged 3 commits into
easystats:mainfrom
finite-sample:fix/performance-roc-ties

Conversation

@soodoku

@soodoku soodoku commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

.performance_roc_numeric() previously emitted one ROC point per observation after sorting by predictions without collapsing ties. When predictions contain ties, the resulting ROC curve and AUC depend on the input row order and diverge from the standard trapezoidal ROC/AUC (e.g., pROC::auc).

Minimal reproducible example

library(performance)
library(pROC)

d1 <- mtcars
d2 <- mtcars[rev(seq_len(nrow(mtcars))), ]

m1 <- glm(vs ~ cyl, data = d1, family = "binomial")
m2 <- glm(vs ~ cyl, data = d2, family = "binomial")

as.numeric(performance_roc(m1))
as.numeric(performance_roc(m2))
as.numeric(pROC::auc(d1$vs, predict(m1, type = "response"), quiet = TRUE))
  • Observed (before fix): 0.9444444 for m1 vs 0.9365079 for m2 (row-order dependent), neither matching pROC::auc (0.9404762).
  • Expected: Both m1 and m2 return 0.9404762, matching pROC::auc.

Fix

In .performance_roc_numeric(), sort both x and predictions by order(predictions, decreasing = TRUE), compute cumulative true positives and false positives, and keep only the last point of each unique prediction value via !duplicated(predictions, fromLast = TRUE). Also drop the redundant trailing 1 since the final retained cumulative ratio is already 1.

Updated the hardcoded Sensitivity vectors in the two existing tests in tests/testthat/test-performance_roc.R whose fitted models contain tied predictions (glmer(vs ~ mpg + (1 | gear)) and glm(y ~ Sepal.Length + Sepal.Width) both have 4 tied predictions).

Verification

  • Added regression test "performance_roc collapses tied predictions (row-order invariant)" in tests/testthat/test-performance_roc.R.
  • testthat::test_file("tests/testthat/test-performance_roc.R"): PASS 10 | FAIL 0 | WARN 0 | SKIP 0.
  • Revert-check (reverting only R/performance_roc.R): FAIL 9 | PASS 1; restoring R/performance_roc.R: PASS 10 | FAIL 0 | WARN 0.

Comment thread R/performance_roc.R Outdated
x <- x[ord]
predictions <- predictions[ord]

tp <- cumsum(x)

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.

Can you write out the object names? I guess it is true_positives and false_positives. I know we often did not use clear names in the past, but now when we rewrite / add code, we can start improving readability :-)

@strengejacke

Copy link
Copy Markdown
Member

Can you add a NEWS item, and increase the dev-version number (to something like 0.20.0.10 or similar - increase the last digit by 1)

@soodoku
soodoku force-pushed the fix/performance-roc-ties branch from 95f9345 to bbcbf51 Compare November 24, 2026 13:49
@soodoku

soodoku commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Updated the PR:

  • Renamed tp/fp to true_positives/false_positives in R/performance_roc.R.
  • Added a NEWS.md bullet under performance (devel) and bumped the dev version in DESCRIPTION to 0.20.0.10.
  • Rebased onto main, formatted the changed files with air, fixed the lintr warnings in the touched files, and replaced the pROC::auc() test check with the exact rank-sum AUC calculation so R CMD check doesn't warn about an undeclared Suggests dependency.

@strengejacke

Copy link
Copy Markdown
Member

Thanks, LGTM!

@strengejacke
strengejacke merged commit 51616ec into easystats:main Nov 24, 2026
9 of 20 checks passed
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.

2 participants