`bal_accuracy()`, `ppv()`, `npv()` return NA for macro averaging when a factor level is unused, unlike `j_index()`

Open Beginner friendly
#636 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
r
Domain
data

Research direction

Start in R/class-bal_accuracy.R, R/class-ppv.R, and R/class-npv.R at the listed multiclass macro-average calculations, then compare them with j_index()'s handling of missing class estimates. Done means an unused factor level no longer makes the macro average NA, while the existing behavior for valid class-level estimates remains unchanged.

Written by the indexing model from the issue text.

Description

tidy-dev-day :nerd_face:

bal_accuracy_table_impl(), ppv_table_impl(), and npv_table_impl() all compute the multiclass macro average via stats::weighted.mean(out_vec, w) without na.rm = TRUE. If any class-level estimate is NA (e.g. an unused/empty factor level), the entire weighted mean becomes NA, even though sibling metrics such as j_index() handle this by passing na.rm = TRUE.

# R/class-bal_accuracy.R:139, R/class-ppv.R:197, R/class-npv.R:172
out_vec <- bal_accuracy_multiclass(data, estimator)  # (or ppv_/npv_multiclass)
stats::weighted.mean(out_vec, w)

reprex:

library(yardstick)
truth <- factor(c("a", "a", "b", "b"), levels = c("a", "b", "c"))
estimate <- factor(c("a", "b", "a", "b"), levels = c("a", "b", "c"))
bal_accuracy_vec(truth, estimate)
#> Warning: While computing multiclass `recall()`, some levels had no true events (i.e.
#> `true_positive + false_negative = 0`).
#> Recall is undefined in this case, and those levels will be removed from the
#> averaged result.
#> Note that the following number of predicted events actually occurred for each
#> problematic event level:
#> 'c': 0
#> [1] NA

Created on 2026-08-31 with reprex v2.1.1

Suggested fix: add na.rm = TRUE to the three weighted.mean() calls, matching j_index()'s behavior.

Dominant language
R
Stars
402
Forks
63
Avg merge
33m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tidymodels/yardstick

All issues in tidymodels/yardstick

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.