R/metric_discrimination.R
metric_specificity_at_sensitivity.RdComputes the specificity achievable when the classifier operates at a specified sensitivity level. This is useful for rule-out diagnostic applications where a minimum sensitivity is mandatory, and we want to know the best achievable specificity at that operating point.
metric_specificity_at_sensitivity(
truth,
scores = NULL,
selected = NULL,
positive = "Yes",
target_sensitivity = 0.9
)Binary outcome; coerced with ensure_binary_response().
Numeric scores or probabilities.
Ignored; kept for signature compatibility.
Label treated as the positive ("event") class.
The sensitivity threshold at which to evaluate specificity (default 0.90). Must be between 0 and 1.
Specificity at the target sensitivity, between 0 and 1. Returns 0
for degenerate inputs.
Uses pROC::coords() to find the operating point on the ROC curve
corresponding to the target sensitivity, then returns the specificity
at that point. If the exact target sensitivity cannot be achieved,
pROC interpolates to the nearest achievable point.
Degenerate inputs (a single-class subset, or a coords() call that yields
nothing usable) return 0 — the worst achievable value — rather than
erroring or returning NA_real_. This keeps the metric safe to use
directly as an NSGA objective: an error would abort the whole run, and an
NA is mapped to the 1e6 infeasibility penalty, which conflates
degeneracy with constraint violation, distorts NSGA-III normalization, and
still surfaces as NA in post-hoc solution re-evaluation. Argument errors
(missing scores, out-of-range target_sensitivity) still signal an
error.
metric_pauc() for partial AUC in the high-sensitivity region,
metric_sensitivity() and metric_specificity() for threshold-based
metrics. All three are internal; reach them by registry name via
define_objectives().
truth <- factor(c(rep("No", 50), rep("Yes", 50)), levels = c("No", "Yes"))
scores <- c(runif(50, 0, 0.6), runif(50, 0.4, 1))
# Specificity when sensitivity is fixed at 90%
metric_specificity_at_sensitivity(truth, scores, target_sensitivity = 0.90)
#> [1] 0.86
# Specificity when sensitivity is fixed at 95%
metric_specificity_at_sensitivity(truth, scores, target_sensitivity = 0.95)
#> [1] 0.7