Computes 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
)

Arguments

truth

Binary outcome; coerced with ensure_binary_response().

scores

Numeric scores or probabilities.

selected

Ignored; kept for signature compatibility.

positive

Label treated as the positive ("event") class.

target_sensitivity

The sensitivity threshold at which to evaluate specificity (default 0.90). Must be between 0 and 1.

Value

Specificity at the target sensitivity, between 0 and 1. Returns 0 for degenerate inputs.

Details

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.

See also

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().

Examples

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