Skip to content

1259: Match observed and predicted_label by label in logs_categorical() - #1277

Draft
nikosbosse wants to merge 2 commits into
mainfrom
issue-1259-nominal-level-order
Draft

nikosbosse wants to merge 2 commits into
mainfrom
issue-1259-nominal-level-order

Conversation

@nikosbosse

@nikosbosse nikosbosse commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

CLAUDE:

Description

This PR closes #1259.

logs_categorical() reordered the columns of predicted by the level codes of predicted_label, but then picked the observed outcome by the level codes of observed. Nominal forecasts only require both factors to have the same set of levels, so with a different level order the log score of the wrong category was returned without an error (2.30 instead of 0.36 in the issue's example).

The observed outcome is now matched to the probabilities by label. rps_ordinal() is not affected, because ordinal forecasts require identical levels in the same order.

Checklist

  • My PR is based on a package issue and I have explicitly linked it.
  • I have included the target issue or issues in the PR title as follows: issue-number: PR title
  • I have tested my changes locally. Full test suite: 0 failures, 1043 passes.
  • I have added or updated unit tests where necessary. Two new tests (direct call and via score()); I ran them against the unfixed code first, where they fail with 3 failed expectations, and they pass with the fix.
  • I have updated the documentation if required. N/A: documented behaviour is unchanged.
  • I have built the package locally and run rebuilt docs using roxygen2. N/A: no roxygen changes.
  • My code follows the established coding standards and I have run lintr::lint_package() to check for style issues introduced by my changes. Linted the changed files, no lints.
  • I have added a news item linked to this PR.
  • I have reviewed CI checks for this PR and addressed them as far as I am able.

Adversarial review

An independent agent reviewed the PR. It compared the branch with a reference that matches by name on 2,000 random cases (random level orders, permuted columns, n = 1, NA in observed): 0 mismatches on the branch, 1,521 on main. score(example_nominal) is identical on main and the branch, and two deliberately wrong fixes were both caught by the new tests. No defects found.

Not applied (optional suggestions): matching directly against as.character(predicted_label) and dropping the column reorder. I kept the reorder because rps_ordinal() uses the same idiom; and mentioning the level order in the argument docs.

🤖 Generated with Claude Code

nikosbosse and others added 2 commits November 10, 2026 23:19
)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.18%. Comparing base (779d7d6) to head (39cce25).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1277   +/-   ##
=======================================
  Coverage   99.18%   99.18%           
=======================================
  Files          41       41           
  Lines        2331     2331           
=======================================
  Hits         2312     2312           
  Misses         19       19           

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

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.

logs_categorical() scores the wrong category if the levels of observed and predicted_label are ordered differently

1 participant