Compute the points of one or more ROC curves from true labels and predicted
probabilities, returning tidy (class, fpr, tpr, auc) rows rather than a
plot. This is the shared engine behind draw_roc and is reused anywhere the
curve data is needed without plotly (e.g. shipping the curve to a client).
Arguments
- true_labels
Factor: True outcome labels.
- predicted_prob
Numeric vector or matrix [0, 1]: Predicted probabilities for the positive class (second level of
true_labels), as a vector or a one-column matrix. For multiclass, a matrix with exactly one column per class, in factor-level order (rtemis's convention); the columns are labeled with the factor levels regardless of any names on the input.- max_points
Optional Integer [2, Inf): Cap on the number of vertices per curve.
NULLkeeps full resolution.
Value
data.frame with columns class, fpr, tpr, auc (the AUC is
constant within a class group, repeated per vertex). Zero rows when no
curve is defined.
Details
Binary problems yield a single curve for the positive class (the second
level of true_labels, rtemis convention); multiclass problems yield one
one-vs-rest curve per class. Points are ordered along the curve; pass
max_points to down-sample very long curves (one vertex per distinct score)
to a compact, smooth line.
Examples
true_labels <- factor(c("A", "B", "A", "A", "B", "A", "B", "B", "A", "B"))
predicted_prob <- c(0.1, 0.4, 0.35, 0.8, 0.65, 0.2, 0.9, 0.55, 0.3, 0.7)
roc_curve(true_labels, predicted_prob)
#> class fpr tpr auc
#> 1 B 0.0 0.0 0.84
#> 2 B 0.0 0.2 0.84
#> 3 B 0.2 0.2 0.84
#> 4 B 0.2 0.4 0.84
#> 5 B 0.2 0.6 0.84
#> 6 B 0.2 0.8 0.84
#> 7 B 0.2 1.0 0.84
#> 8 B 0.4 1.0 0.84
#> 9 B 0.6 1.0 0.84
#> 10 B 0.8 1.0 0.84
#> 11 B 1.0 1.0 0.84