groc: ROC curve

View source: R/zzz-r4vn-graphs.R View source: R/graphs.R

grocR Documentation

ROC curve

Description

Calculates and draws a receiver operating characteristic curve from a binary outcome and numeric predicted probabilities or scores. No external package is required.

Usage

groc(
  data = NULL, outcome, pred = NULL, vars = NULL, by = NULL, event = NULL,
  diagonal = TRUE, auc = TRUE, digits = 3, curve_lty = 1, curve_lwd = 2.5,
  diagonal_color = "gray60", diagonal_lty = 2, diagonal_lwd = 1, xlab = NULL,
  ylab = NULL, xtitle = "1 - Specificity", ytitle = "Sensitivity",
  title = NULL, subtitle = NULL, note = NULL, color = NULL,
  palette = "journal", xline = NULL, yline = NULL, ref_color = "gray40",
  ref_lty = 2, ref_lwd = 1, theme = "journal", size = 11, combine = FALSE,
  ncol = NULL, file = NULL, width = 6, height = 6, dpi = 300, show = TRUE,
  bg = "white", vline = NULL, hline = NULL
)

Arguments

data

A data frame.

outcome

Binary outcome variable.

pred

Numeric predicted probability or score; larger values must indicate a greater probability of the event.

vars

Optional vars(...) selector for several numeric predictors or scores.

by

Optional grouping variable or hierarchical vars(...) selector.

event

Event value. By default, the second factor level or the larger numeric value is used.

diagonal

Show the no-discrimination diagonal.

auc

Show the area under the curve.

digits

Number of AUC digits.

curve_lty, curve_lwd

ROC-curve line type and width.

diagonal_color, diagonal_lty, diagonal_lwd

Colour, line type, and width of the no-discrimination diagonal.

xlab, ylab

Optional tick labels.

xtitle, ytitle

Axis titles.

title, subtitle, note, color, palette, theme, size, combine, ncol, file, width, height, dpi, show, bg

See gbar().

xline, yline

Optional numeric reference lines on the x and y axes.

ref_color, ref_lty, ref_lwd

Color, line type, and width for reference lines.

vline, hline

Deprecated aliases for xline and yline.

Details

Draws ROC curves for one or several predictor/score variables. by can create ROC analyses within hierarchical strata.

Value

An object of class r4vn_graph; its data component contains thresholds, sensitivity, and specificity, and its auc attribute contains the AUC.

Examples

d <- data.frame(y = c(0, 0, 1, 1, 1), p = c(.10, .35, .40, .75, .90))
groc(d, outcome = y, pred = p, event = 1)

# Extended usage examples

d <- data.frame(
  outcome = factor(c("No", "No", "No", "Yes", "Yes", "Yes", "Yes")),
  probability = c(.05, .20, .35, .40, .65, .80, .95)
)

# ROC curve with explicit event
roc1 <- groc(d, outcome, probability, event = "Yes", show = FALSE)
attr(roc1$data, "auc")

# Hide the diagonal or AUC annotation and customize labels
groc(d, outcome, probability, event = "Yes",
     diagonal = FALSE, auc = FALSE,
     xtitle = "False-positive rate", ytitle = "True-positive rate",
     show = FALSE)

# Numeric binary outcome; the larger value is the default event
d2 <- data.frame(y = c(0, 0, 1, 1, 1), score = c(.10, .35, .40, .75, .90))
groc(d2, y, score, show = FALSE)

# Export the ROC curve
groc(d, outcome, probability, event = "Yes",
     file = tempfile(fileext = ".pdf"), show = FALSE)


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.