Skip to content

BOT: Fix #763: Warn when binary factor levels are in counterintuitive order - #1091

Merged
seabbs merged 2 commits into
mainfrom
fix/763-binary-factor-level-order
Apr 20, 2026
Merged

seabbs merged 2 commits into
mainfrom
fix/763-binary-factor-level-order

Conversation

@nikosbosse

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes We should check for consistency when scoring binary forecasts #763
  • Adds a warning in assert_input_binary() when factor levels of observed appear to be in counterintuitive order (e.g., levels = c("1", "0") or levels = c("TRUE", "FALSE"))
  • This catches silent misinterpretation of predictions: with reversed 0/1 levels, predictions are interpreted as P(outcome = "0") instead of P(outcome = "1"), leading to completely wrong scores with no user indication

Root cause

assert_input_binary() only validated that observed is a factor with 2 levels and predicted is numeric in [0,1]. It performed no check on whether the factor level ordering makes semantic sense. Since as.numeric(factor) - 1 maps the first level to 0 and the second to 1, reversed levels silently produce incorrect scores.

What the fix does

Adds a check at the end of assert_input_binary() that detects two counterintuitive patterns:

  • Levels c("1", "0") — predictions would be interpreted as P(outcome = "0")
  • Levels c("TRUE", "FALSE") — predictions would be interpreted as P(outcome = "FALSE")

When detected, emits a cli_warn() with an informative message explaining the issue and suggesting how to fix it. This is a warning (not an error) so existing code continues to work.

Test coverage added

8 new tests across two test files:

  • assert_input_binary() warns for reversed 0/1 levels
  • assert_input_binary() does NOT warn for standard 0/1 order (regression guard)
  • assert_input_binary() does NOT warn for non-numeric levels like "a"/"b" (regression guard)
  • brier_score() demonstrates different results with reversed levels and emits warning
  • logs_binary() emits warning with reversed levels
  • as_forecast_binary() emits warning when data has reversed levels
  • assert_input_binary() warns for TRUE/FALSE in counterintuitive order
  • score() produces correct results with standard levels (regression guard)

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented Feb 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.99%. Comparing base (d4bbd6e) to head (3de8ee4).
⚠️ Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1091      +/-   ##
==========================================
+ Coverage   97.98%   97.99%   +0.01%     
==========================================
  Files          37       37              
  Lines        1984     1999      +15     
==========================================
+ Hits         1944     1959      +15     
  Misses         40       40              

☔ View full report in Codecov by Sentry.
📢 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.

@nikosbosse nikosbosse left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

CLAUDE: Review verdict — approve. Clean, well-scoped fix that adds a cli_warn() to assert_input_binary() for counterintuitive factor level orderings (c("1", "0") and c("TRUE", "FALSE")). The detection logic using setequal() + lvls[1] is correct, the warning message is informative and actionable, and all 8 test specifications are covered. The fix is non-breaking (warning, not error) and placed after existing assertion checks. Minor note: the warning fires multiple times in a score() pipeline (once per assert_input_binary() call), but this is acceptable and consistent with the codebase.

@nikosbosse
nikosbosse marked this pull request as draft February 13, 2026 08:28
@nikosbosse nikosbosse changed the title Fix #763: Warn when binary factor levels are in counterintuitive order BOT: Fix #763: Warn when binary factor levels are in counterintuitive order Feb 13, 2026
Resolve merge conflict in R/metrics-binary.R (assert_dims_ok_point renamed
to assert_dims_ok_scalar on main). Fix nonportable_path_linter warnings by
replacing "/" with "-" in test descriptions (e.g. "0/1" -> "0-1").

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nikosbosse

Copy link
Copy Markdown
Collaborator Author

I think this useful to have

@nikosbosse
nikosbosse requested a review from seabbs April 5, 2026 14:55
@nikosbosse
nikosbosse marked this pull request as ready for review April 5, 2026 14:55
@seabbs

seabbs commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

@nikosbosse it is unclear how much these PRs have been human reviewed before pinging for review. Anyway we can make that clearer as I don't really want to review other peoples bots!

@seabbs
seabbs merged commit 71bd4ae into main Apr 20, 2026
11 checks passed
@seabbs
seabbs deleted the fix/763-binary-factor-level-order branch April 20, 2026 09:54
@nikosbosse

Copy link
Copy Markdown
Collaborator Author

@seabbs Anything that tags you as a reviewer is human-reviewed by me!

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.

We should check for consistency when scoring binary forecasts

2 participants