Comparing two markers: DeLong, bootstrap and Venkatraman
On the same patients, two AUCs are correlated, and DeLong's test accounts for that. Venkatraman's test asks a different question, about the whole curves.
What runs in CodeSplash
- STAT309DeLong AUC comparison test Runs in CodeSplash
- STAT310Venkatraman ROC curve test Runs in CodeSplash
- Compare two ROC curves (paired) Verified in Python and R
You will be able to
- Explain why comparing two markers on the same subjects needs a paired test
- Read DeLong's test of equal areas and its interval
- Choose Venkatraman's test when the curves may cross
Read first
Same patients, two markers
ReadIn the Wisconsin biopsies, clump thickness and uniformity of cell size were scored on the same 241 malignant and 458 benign tumors. Their AUCs are 0.910 and 0.974.
Because the patients are shared, the estimates move together. DeLong's testDeLong testTests whether two AUCs are equal from placement values, including the covariance when both come from the same subjects. includes that covariance:
Here is each AUC's squared standard error, and is the correlation between the two estimates.
View data
| event | new score | old score |
|---|---|---|
| no | −0.8090 | 0.0380 |
| no | 0.9230 | 0.6760 |
| no | 1.187 | 1.591 |
| yes | 2.029 | 1.891 |
| no | 0.5620 | −0.1740 |
| no | 1.489 | 1.776 |
| yes | 0.9050 | 1.790 |
| no | −0.9150 | −0.1910 |
| yes | 1.018 | −0.0840 |
| no | 1.720 | 0.7810 |
| yes | 1.365 | 1.736 |
| no | −2.757 | −0.7970 |
| no | 0.5670 | 0.1020 |
| yes | 1.850 | 1.549 |
| yes | 1.867 | 3.360 |
| no | 0.8850 | −0.3320 |
| no | 1.045 | 1.212 |
| no | −0.4180 | 0.8660 |
| no | 1.857 | −0.6830 |
| no | −0.4750 | −1.303 |
| yes | 4.694 | 2.247 |
| yes | 3.104 | 2.772 |
| no | 1.388 | −0.9540 |
| no | 0.8140 | 1.098 |
| no | −0.9970 | 0.7090 |
| no | 1.188 | −0.3270 |
| yes | 1.774 | 1.216 |
| no | 1.745 | 0.5400 |
| yes | 1.855 | 0.8970 |
| no | −1.484 | −0.2460 |
| no | −0.1890 | 0.4880 |
| no | 1.987 | 0.7960 |
| no | 0.0630 | 1.117 |
| no | 0.8770 | 2.806 |
| no | −0.2320 | −2.070 |
| yes | 3.374 | 2.253 |
| no | 0.3370 | 0.2530 |
| yes | 0.3540 | 1.345 |
| no | −0.3370 | −1.845 |
| no | −1.189 | 0.1180 |
| no | −0.7490 | 0.0670 |
| no | 0.6920 | −0.9850 |
| no | −0.1470 | −0.4000 |
| yes | 0.9130 | 0.9950 |
| no | 1.377 | 1.176 |
| yes | 0.9640 | 0.1260 |
| yes | 0.8730 | 0.4830 |
| no | −1.098 | −1.637 |
| no | 0.8150 | 0.6330 |
| yes | 2.656 | 1.167 |
| no | −2.024 | −1.863 |
| no | −1.650 | −1.037 |
| no | −0.7470 | −0.2360 |
| no | −0.9810 | 0.8100 |
| yes | 0.7250 | −0.0480 |
| no | −1.654 | −0.8090 |
| yes | 0.6230 | −0.6370 |
| no | 0.0530 | 0.8150 |
| no | −0.5760 | 0.0650 |
| no | 0.4110 | 0.4100 |
First 60 of 300 rows.
Ignore the pairing
PredictPredict first
Weight and fuel economy both predict manual transmission in the same cars: AUCs 0.93 and 0.83, with = 0.79.
If you treat the two as independent, how strong is the comparison?
Reveal
Weaker. Without the covariance, the standard error is and . With it, the standard error is 0.049 and = 2.10, p = .036. Pairing removes shared noise, as it does in a paired t test.
Read the card
In the appBack to the biopsies. The card gives each AUC, the difference with its interval, and a Tests table that names the question each test answers.
ΔAUC = −0.064, 95% CI [−0.089, −0.039], z = −5.03, p < .001
- AUC difference (clump − cell size)
- −0.0642
- z (DeLong)
- −5.031
- p (DeLong)
- < .001
Effect size
- AUC difference −0.0642 95% CI [−0.0892, −0.0392]
Plots
View data
| marker | 1 − specificity | Sensitivity |
|---|---|---|
| clump | 0 | 0 |
| clump | 0 | 0.2863 |
| clump | 0 | 0.3444 |
| clump | 0.0087 | 0.5187 |
| clump | 0.0109 | 0.6100 |
| clump | 0.0459 | 0.6846 |
| clump | 0.2314 | 0.8714 |
| clump | 0.3799 | 0.9212 |
| clump | 0.5895 | 0.9710 |
| clump | 0.6900 | 0.9876 |
| clump | 1 | 1 |
| cell size | 0 | 0 |
| cell size | 0 | 0.2780 |
| cell size | 0.0022 | 0.2988 |
| cell size | 0.0044 | 0.4149 |
| cell size | 0.0066 | 0.4896 |
| cell size | 0.0109 | 0.5934 |
| cell size | 0.0109 | 0.7178 |
| cell size | 0.0306 | 0.8465 |
| cell size | 0.0895 | 0.9502 |
| cell size | 0.1703 | 0.9834 |
| cell size | 1 | 1 |
View data
| clump | cell size | class = malignant |
|---|---|---|
| 5 | 1 | 0 |
| 5 | 4 | 0 |
| 3 | 1 | 0 |
| 6 | 8 | 0 |
| 4 | 1 | 0 |
| 8 | 10 | 1 |
| 1 | 1 | 0 |
| 2 | 1 | 0 |
| 2 | 1 | 0 |
| 4 | 2 | 0 |
| 1 | 1 | 0 |
| 2 | 1 | 0 |
| 5 | 3 | 1 |
| 1 | 1 | 0 |
| 8 | 7 | 1 |
| 7 | 4 | 1 |
| 4 | 1 | 0 |
| 4 | 1 | 0 |
| 10 | 7 | 1 |
| 6 | 1 | 0 |
| 7 | 3 | 1 |
| 10 | 5 | 1 |
| 3 | 1 | 0 |
| 8 | 4 | 1 |
| 1 | 1 | 0 |
| 5 | 2 | 1 |
| 3 | 2 | 0 |
| 5 | 1 | 0 |
| 2 | 1 | 0 |
| 1 | 1 | 0 |
| 3 | 1 | 0 |
| 2 | 1 | 0 |
| 10 | 7 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 0 |
| 2 | 1 | 0 |
| 10 | 10 | 1 |
| 6 | 2 | 0 |
| 5 | 4 | 1 |
| 2 | 5 | 1 |
| 6 | 6 | 0 |
| 10 | 4 | 1 |
| 6 | 10 | 1 |
| 5 | 6 | 1 |
| 10 | 10 | 1 |
| 1 | 1 | 0 |
| 3 | 7 | 1 |
| 1 | 1 | 0 |
| 4 | 1 | 0 |
| 7 | 8 | 1 |
| 9 | 5 | 1 |
| 5 | 3 | 1 |
| 10 | 3 | 1 |
| 5 | 5 | 1 |
| 10 | 5 | 1 |
| 10 | 6 | 1 |
| 8 | 10 | 1 |
| 8 | 2 | 1 |
| 5 | 2 | 1 |
| 9 | 5 | 1 |
First 60 of 699 rows.
Assumptions
- Same subjects for both markers — Both curves use the same 699 subjects.
- Enough cases and non-cases — 241 cases and 458 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| clump | 0.9098 | 0.0118 |
| cell size | 0.9740 | 0.0059 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | −5.031 | 4.871e-7 | Equal areas under the curves? |
- Condition present: class = malignant. Correlation between the AUC estimates r = 0.080.
The exact R script · R R version 4.5.1 (2025-06-13)
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec 189c7c636a3e9900
# Dataset: biopsy (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_delong_paired, cs_roc_points (twins of codesplash.stats.roc)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/biopsy.parquet", col_select = c("clump", "cell size", "class")))
n_input <- nrow(df)
df[["class"]] <- factor(df[["class"]], levels = c("benign", "malignant")) # stored level order
df <- df[stats::complete.cases(df[c("clump", "cell size", "class")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("clump", "cell size", "class")) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1 <- "clump"
m2 <- "cell size"
r <- "class"
ref_levels <- c("benign", "malignant") # stored level order
pos_level <- ref_levels[length(ref_levels)] # default: the last level ("true")
s1 <- if ("higher" == "higher") 1 else -1 # explicit directions; never flipped automatically
s2 <- if ("higher" == "higher") 1 else -1
is_pos <- as.character(df[[r]]) == pos_level
x1 <- s1 * as.numeric(df[[m1]])
x2 <- s2 * as.numeric(df[[m2]])
npos <- sum(is_pos)
nneg <- sum(!is_pos)
if (npos == 0 || nneg == 0) cs_error("CS_E_TOO_FEW_GROUPS", column = r, min = 2, k = 1) # needs positives and negatives
conf <- 0.95
res <- cs_delong_paired(x1[is_pos], x1[!is_pos], x2[is_pos], x2[!is_pos], conf)
test <- "delong"
reps <- as.integer(2000)
seed <- as.integer(1)
vk <- if (test == "venkatraman") cs_venkatraman_paired(x1, x2, is_pos, reps, seed) else NULL
bt <- if (test == "bootstrap") cs_bootstrap_roc_paired(x1, x2, is_pos, reps, seed) else NULL
if (test == "venkatraman") {
test_label <- "Venkatraman's permutation test (equal ROC curves)"; test_stat <- vk$e; test_p <- vk$p
} else if (test == "bootstrap") {
test_label <- "Paired bootstrap test (equal AUCs)"; test_stat <- bt$z; test_p <- bt$p
} else {
test_label <- "DeLong's paired test (equal AUCs)"; test_stat <- res$z; test_p <- res$p
}
# %% Assumptions
assumptions <- list(
list(key = "pairing", label = "Same subjects for both markers", status = "info", detail = sprintf("Both curves use the same %d subjects.", npos + nneg)),
list(key = "sample_size", label = "Enough cases and non-cases", status = if (min(npos, nneg) < 10) "warn" else "pass",
detail = sprintf("%d cases and %d non-cases%s", npos, nneg, if (min(npos, nneg) < 10) "; the normal approximation is rough." else "."))
)
# %% Result
curves <- lapply(list(list(m1, x1), list(m2, x2)), function(mx) {
pts <- cs_roc_points(mx[[2]][is_pos], mx[[2]][!is_pos])
k <- length(pts$tpr)
keep <- unique(round(seq(1, k, length.out = min(k, 1500))))
data.frame(fpr = pts$fpr[keep], tpr = pts$tpr[keep], curve = mx[[1]])
})
curves <- do.call(rbind, curves)
pct <- sprintf("%.0f%%", conf * 100)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
.rc <- unique(round(seq(1, npos + nneg, length.out = min(npos + nneg, 5000))))
compare_chart <- cs_chart("compare", "roc-compare", sprintf("%s against %s", m1, m2), list(
label = cs_col(sprintf("%s = %s", r, pos_level), as.numeric(is_pos[.rc]), "numeric"),
a = cs_col(m1, x1[.rc], "numeric"), b = cs_col(m2, x2[.rc], "numeric")
), controls = list(level = conf), note = if (length(.rc) < npos + nneg) sprintf("Drawn from %s evenly spaced rows; the tests above use all %s.", format(length(.rc), big.mark = ","), format(npos + nneg, big.mark = ",")) else NULL)
cs_publish(list(
method = "diagnostic.roc_compare",
methodVersion = "1.1.2",
title = "Comparison of two ROC curves",
engine = cs_engine_info("stats", "nanoparquet"),
n = npos + nneg,
summary = paste0(sprintf("AUC %.3f (%s) vs %.3f (%s): difference %.3f, %s CI [%.3f, %.3f]; ", res$auc_1, m1, res$auc_2, m2, res$diff, pct, res$low, res$high),
if (!is.null(vk)) sprintf("Venkatraman E = %.0f, %s.", vk$e, cs_fmt_p(vk$p))
else if (!is.null(bt)) sprintf("bootstrap z = %.2f, %s.", bt$z, cs_fmt_p(bt$p))
else sprintf("DeLong z = %.2f, %s.", res$z, cs_fmt_p(res$p))),
apa = sprintf("ΔAUC = %s, %s CI [%s, %s], z = %s, %s", cs_fmt_num(res$diff, 3), pct, cs_fmt_num(res$low, 3), cs_fmt_num(res$high, 3),
cs_fmt_num(res$z), cs_fmt_p(res$p)),
statistics = c(list(
list(key = "auc_diff", label = sprintf("AUC difference (%s − %s)", m1, m2), value = res$diff),
list(key = "z", label = "z (DeLong)", value = res$z), list(key = "p", label = "p (DeLong)", value = res$p, format = "p")),
if (!is.null(vk)) list(list(key = "venkatraman_e", label = "Venkatraman E", value = vk$e), list(key = "venkatraman_p", label = "p (Venkatraman)", value = vk$p, format = "p")),
if (!is.null(bt)) list(list(key = "boot_z", label = "z (bootstrap)", value = bt$z), list(key = "boot_p", label = "p (bootstrap)", value = bt$p, format = "p"))
),
tables = list(list(
key = "aucs", title = "Areas under the curves",
columns = list(list(key = "marker", label = "Marker", type = "string"), list(key = "auc", label = "AUC", type = "number"),
list(key = "se", label = "SE (DeLong)", type = "number")),
rows = list(list(marker = m1, auc = res$auc_1, se = res$se_1), list(marker = m2, auc = res$auc_2, se = res$se_2))
), list(
key = "tests", title = "Tests",
columns = list(list(key = "test", label = "Test", type = "string"), list(key = "statistic", label = "Statistic", type = "number"),
list(key = "p", label = "p", type = "p"), list(key = "question", label = "Question", type = "string")),
rows = c(list(list(test = "DeLong (paired)", statistic = res$z, p = res$p, question = "Equal areas under the curves?")),
if (!is.null(vk)) list(list(test = sprintf("Venkatraman (%s permutations)", format(reps, big.mark = ",")), statistic = vk$e, p = vk$p,
question = "Identical curves at every operating point?")),
if (!is.null(bt)) list(list(test = sprintf("Paired bootstrap (%s resamples)", format(reps, big.mark = ",")), statistic = bt$z, p = bt$p,
question = "Equal areas under the curves?")))
)),
effectSizes = list(list(key = "auc_diff", label = "AUC difference", value = res$diff, ciLow = res$low, ciHigh = res$high, ciLevel = conf,
method = "DeLong paired variance")),
assumptions = assumptions,
warnings = warnings,
notes = c(list(sprintf("Condition present: %s = %s. Correlation between the AUC estimates r = %.3f.", r, pos_level, res$r)),
if (!is.null(vk)) list(paste("Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross.",
sprintf("Its p-value is a permutation p (%s within-subject swaps, seed %d) and matches in Python and R.", format(reps, big.mark = ","), seed))),
if (!is.null(bt)) list(sprintf("The bootstrap resamples cases and non-cases separately (%s resamples, seed %d); z = ΔAUC / SD of the bootstrap differences.",
format(reps, big.mark = ","), seed))),
charts = list(cs_chart("roc", "roc-curve", "ROC curves",
list(fpr = cs_col("1 − specificity", curves$fpr), tpr = cs_col("Sensitivity", curves$tpr),
curve = cs_col("marker", curves$curve, levels = c(m1, m2)))), compare_chart),
values = list(
n_pos = npos, n_neg = nneg, auc_1 = res$auc_1, auc_2 = res$auc_2, se_1 = res$se_1, se_2 = res$se_2,
auc_diff = res$diff, diff_se = res$se, diff_ci_low = res$low, diff_ci_high = res$high, z = res$z, p = res$p,
r = res$r, positive_level = pos_level, test = test, test_label = test_label, test_statistic = test_stat, test_p = test_p,
venkatraman_e = if (is.null(vk)) NULL else vk$e, venkatraman_p = if (is.null(vk)) NULL else vk$p,
boot_sd = if (is.null(bt)) NULL else bt$sd, boot_z = if (is.null(bt)) NULL else bt$z, boot_p = if (is.null(bt)) NULL else bt$p,
reps = if (is.null(vk) && is.null(bt)) NULL else reps
)
))
ΔAUC = −0.064, 95% CI [−0.089, −0.039], z = −5.03, p < .001
- AUC difference (clump − cell size)
- −0.0642
- z (DeLong)
- −5.031
- p (DeLong)
- < .001
Effect size
- AUC difference −0.0642 95% CI [−0.0892, −0.0392]
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Same subjects for both markers — Both curves use the same 699 subjects.
- Enough cases and non-cases — 241 cases and 458 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| clump | 0.9098 | 0.0118 |
| cell size | 0.9740 | 0.0059 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | −5.031 | 4.871e-7 | Equal areas under the curves? |
- Condition present: class = malignant. Correlation between the AUC estimates r = 0.080.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec 189c7c636a3e9900
# Dataset: biopsy (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import roc as csr # paired DeLong test (twin of codesplashr::cs_delong_paired)
# %% Data
df = pd.read_parquet("tests/parity/fixtures/biopsy.parquet", columns=["clump", "cell size", "class"])
n_input = len(df)
df["class"] = pd.Categorical(df["class"], categories=["benign", "malignant"]) # stored level order
df = df.dropna(subset=["clump", "cell size", "class"]) # listwise deletion over the analysis columns
df["class"] = df["class"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["clump", "cell size", "class"]) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1, m2, r = "clump", "cell size", "class"
ref_levels = ["benign", "malignant"] # stored level order
pos_level = ref_levels[-1] # default: the last level ("true")
s1 = 1.0 if "higher" == "higher" else -1.0 # explicit directions; never flipped automatically
s2 = 1.0 if "higher" == "higher" else -1.0
is_pos = (df[r].astype(str) == pos_level).to_numpy()
x1, x2 = s1 * df[m1].to_numpy(dtype=float), s2 * df[m2].to_numpy(dtype=float)
npos, nneg = int(is_pos.sum()), int((~is_pos).sum())
if npos == 0 or nneg == 0:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=r, min=2, k=1) # needs positives and negatives
conf = 0.95
res = csr.delong_paired(x1[is_pos], x1[~is_pos], x2[is_pos], x2[~is_pos], conf)
test = "delong"
reps = int(2000)
seed = int(1)
vk = csr.venkatraman_paired(x1, x2, is_pos, reps, seed) if test == "venkatraman" else None
bt = csr.bootstrap_paired(x1, x2, is_pos, reps, seed) if test == "bootstrap" else None
if test == "venkatraman":
test_label, test_stat, test_p = "Venkatraman's permutation test (equal ROC curves)", vk["e"], vk["p"]
elif test == "bootstrap":
test_label, test_stat, test_p = "Paired bootstrap test (equal AUCs)", bt["z"], bt["p"]
else:
test_label, test_stat, test_p = "DeLong's paired test (equal AUCs)", res["z"], res["p"]
# %% Assumptions
assumptions = [
{"key": "pairing", "label": "Same subjects for both markers", "status": "info", "detail": f"Both curves use the same {npos + nneg} subjects."},
{"key": "sample_size", "label": "Enough cases and non-cases", "status": "warn" if min(npos, nneg) < 10 else "pass",
"detail": f"{npos} cases and {nneg} non-cases" + ("; the normal approximation is rough." if min(npos, nneg) < 10 else ".")},
]
# %% Result
curves_fpr, curves_tpr, curves_lab = [], [], []
for name, x in ((m1, x1), (m2, x2)):
pts = csr.roc_points(x[is_pos], x[~is_pos])
k = len(pts["tpr"])
keep = np.unique(np.round(np.linspace(0, k - 1, min(k, 1500))).astype(int))
curves_fpr += pts["fpr"][keep].tolist()
curves_tpr += pts["tpr"][keep].tolist()
curves_lab += [name] * len(keep)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
_rc = np.unique(np.round(np.linspace(0, npos + nneg - 1, min(npos + nneg, 5000))).astype(int))
compare_chart = cs.chart("compare", "roc-compare", f"{m1} against {m2}", {
"label": cs.col(f"{r} = {pos_level}", [1 if is_pos[i] else 0 for i in _rc], "numeric"),
"a": cs.col(m1, x1[_rc], "numeric"), "b": cs.col(m2, x2[_rc], "numeric"),
}, controls={"level": conf}, note=None if len(_rc) == npos + nneg else f"Drawn from {len(_rc):,} evenly spaced rows; the tests above use all {npos + nneg:,}.")
cs.publish({
"method": "diagnostic.roc_compare",
"methodVersion": "1.1.2",
"title": "Comparison of two ROC curves",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": npos + nneg,
"summary": f"AUC {res['auc_1']:.3f} ({m1}) vs {res['auc_2']:.3f} ({m2}): difference {res['diff']:.3f}, {conf:.0%} CI [{res['low']:.3f}, {res['high']:.3f}]; "
+ (f"Venkatraman E = {vk['e']:.0f}, {cs.fmt_p(vk['p'])}." if vk else f"bootstrap z = {bt['z']:.2f}, {cs.fmt_p(bt['p'])}." if bt
else f"DeLong z = {res['z']:.2f}, {cs.fmt_p(res['p'])}."),
"apa": f"ΔAUC = {cs.fmt_num(res['diff'], 3)}, {conf:.0%} CI [{cs.fmt_num(res['low'], 3)}, {cs.fmt_num(res['high'], 3)}], z = {cs.fmt_num(res['z'])}, {cs.fmt_p(res['p'])}",
"statistics": [
{"key": "auc_diff", "label": f"AUC difference ({m1} − {m2})", "value": res["diff"]},
{"key": "z", "label": "z (DeLong)", "value": res["z"]}, {"key": "p", "label": "p (DeLong)", "value": res["p"], "format": "p"},
] + ([{"key": "venkatraman_e", "label": "Venkatraman E", "value": vk["e"]}, {"key": "venkatraman_p", "label": "p (Venkatraman)", "value": vk["p"], "format": "p"}] if vk else [])
+ ([{"key": "boot_z", "label": "z (bootstrap)", "value": bt["z"]}, {"key": "boot_p", "label": "p (bootstrap)", "value": bt["p"], "format": "p"}] if bt else []),
"tables": [{
"key": "aucs", "title": "Areas under the curves",
"columns": [{"key": "marker", "label": "Marker", "type": "string"}, {"key": "auc", "label": "AUC", "type": "number"},
{"key": "se", "label": "SE (DeLong)", "type": "number"}],
"rows": [{"marker": m1, "auc": res["auc_1"], "se": res["se_1"]}, {"marker": m2, "auc": res["auc_2"], "se": res["se_2"]}],
}, {
"key": "tests", "title": "Tests", "columns": [{"key": "test", "label": "Test", "type": "string"}, {"key": "statistic", "label": "Statistic", "type": "number"},
{"key": "p", "label": "p", "type": "p"}, {"key": "question", "label": "Question", "type": "string"}],
"rows": [{"test": "DeLong (paired)", "statistic": res["z"], "p": res["p"], "question": "Equal areas under the curves?"}]
+ ([{"test": f"Venkatraman ({reps:,} permutations)", "statistic": vk["e"], "p": vk["p"], "question": "Identical curves at every operating point?"}] if vk else [])
+ ([{"test": f"Paired bootstrap ({reps:,} resamples)", "statistic": bt["z"], "p": bt["p"], "question": "Equal areas under the curves?"}] if bt else []),
}],
"effectSizes": [{"key": "auc_diff", "label": "AUC difference", "value": res["diff"], "ciLow": res["low"], "ciHigh": res["high"], "ciLevel": conf,
"method": "DeLong paired variance"}],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Condition present: {r} = {pos_level}. Correlation between the AUC estimates r = {res['r']:.3f}."]
+ (["Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross. "
f"Its p-value is a permutation p ({reps:,} within-subject swaps, seed {seed}) and matches in Python and R."] if vk else [])
+ ([f"The bootstrap resamples cases and non-cases separately ({reps:,} resamples, seed {seed}); z = ΔAUC / SD of the bootstrap differences."] if bt else []),
"charts": [cs.chart("roc", "roc-curve", "ROC curves", {"fpr": cs.col("1 − specificity", curves_fpr), "tpr": cs.col("Sensitivity", curves_tpr),
"curve": cs.col("marker", curves_lab, levels=[m1, m2])}), compare_chart],
"values": {
"n_pos": npos, "n_neg": nneg, "auc_1": res["auc_1"], "auc_2": res["auc_2"], "se_1": res["se_1"], "se_2": res["se_2"],
"auc_diff": res["diff"], "diff_se": res["se"], "diff_ci_low": res["low"], "diff_ci_high": res["high"], "z": res["z"], "p": res["p"],
"r": res["r"], "positive_level": pos_level, "test": test, "test_label": test_label, "test_statistic": test_stat, "test_p": test_p,
"venkatraman_e": vk["e"] if vk else None, "venkatraman_p": vk["p"] if vk else None,
"boot_sd": bt["sd"] if bt else None, "boot_z": bt["z"] if bt else None, "boot_p": bt["p"] if bt else None, "reps": reps if (vk or bt) else None,
},
})
The difference is −0.064 (−0.089 to −0.039), = −5.03, p < .001.
Set First marker direction and Second marker direction yourself: they're never flipped automatically.
When equal areas hide a difference
PredictPredict first
One marker is better when you need high sensitivity, the other when you need high specificity. Their curves cross and the areas nearly match, so DeLong gives = 0.9.
Does that show the markers perform alike?
Reveal
No. DeLong tests areas, and gains in one region can cancel losses in another. Venkatraman's testVenkatraman's testA permutation test that two ROC curves measured on the same subjects are identical at every threshold, not just equal in area. compares the curves themselves, through the gap between them:
Run Venkatraman's test
In the appSet Test to Venkatraman's permutation test. Its p-value comes from swapping each subject's two ranks at random, not from shuffling outcomes. Permutations / resamples sets how many, and Random seed fixes them, so Python and R match.
On a simulated pair of scores, = 462, p = .488, and DeLong gives p = .344. Both results are compatible with no difference.
ΔAUC = 0.061, 95% CI [−0.066, 0.188], z = 0.95, p = .344
- AUC difference (score1 − score2)
- 0.0614
- z (DeLong)
- 0.9469
- p (DeLong)
- .344
- Venkatraman E
- 462
- p (Venkatraman)
- .488
Effect size
- AUC difference 0.0614 95% CI [−0.0657, 0.1885]
Plots
View data
| marker | 1 − specificity | Sensitivity |
|---|---|---|
| score1 | 0 | 0 |
| score1 | 0 | 0.0167 |
| score1 | 0 | 0.0333 |
| score1 | 0 | 0.0500 |
| score1 | 0 | 0.0667 |
| score1 | 0 | 0.0833 |
| score1 | 0 | 0.1000 |
| score1 | 0 | 0.1167 |
| score1 | 0 | 0.1333 |
| score1 | 0 | 0.1500 |
| score1 | 0 | 0.1667 |
| score1 | 0 | 0.1833 |
| score1 | 0 | 0.2000 |
| score1 | 0 | 0.2167 |
| score1 | 0.0167 | 0.2167 |
| score1 | 0.0167 | 0.2333 |
| score1 | 0.0167 | 0.2500 |
| score1 | 0.0333 | 0.2500 |
| score1 | 0.0500 | 0.2500 |
| score1 | 0.0500 | 0.2667 |
| score1 | 0.0500 | 0.2833 |
| score1 | 0.0500 | 0.3000 |
| score1 | 0.0500 | 0.3167 |
| score1 | 0.0667 | 0.3167 |
| score1 | 0.0667 | 0.3333 |
| score1 | 0.0833 | 0.3333 |
| score1 | 0.0833 | 0.3500 |
| score1 | 0.1000 | 0.3500 |
| score1 | 0.1000 | 0.3667 |
| score1 | 0.1167 | 0.3667 |
| score1 | 0.1167 | 0.3833 |
| score1 | 0.1167 | 0.4000 |
| score1 | 0.1167 | 0.4167 |
| score1 | 0.1167 | 0.4333 |
| score1 | 0.1167 | 0.4500 |
| score1 | 0.1167 | 0.4667 |
| score1 | 0.1167 | 0.4833 |
| score1 | 0.1333 | 0.4833 |
| score1 | 0.1500 | 0.4833 |
| score1 | 0.1500 | 0.5000 |
| score1 | 0.1500 | 0.5167 |
| score1 | 0.1500 | 0.5333 |
| score1 | 0.1500 | 0.5500 |
| score1 | 0.1667 | 0.5500 |
| score1 | 0.1667 | 0.5667 |
| score1 | 0.1833 | 0.5667 |
| score1 | 0.1833 | 0.5833 |
| score1 | 0.1833 | 0.6000 |
| score1 | 0.2000 | 0.6000 |
| score1 | 0.2167 | 0.6000 |
| score1 | 0.2333 | 0.6000 |
| score1 | 0.2500 | 0.6000 |
| score1 | 0.2500 | 0.6167 |
| score1 | 0.2667 | 0.6167 |
| score1 | 0.2667 | 0.6333 |
| score1 | 0.2667 | 0.6500 |
| score1 | 0.2833 | 0.6500 |
| score1 | 0.3000 | 0.6500 |
| score1 | 0.3167 | 0.6500 |
| score1 | 0.3333 | 0.6500 |
First 60 of 242 rows.
View data
| score1 | score2 | truth = true |
|---|---|---|
| 0.5206 | 1.906 | 0 |
| −1.080 | 2.132 | 0 |
| 0.1392 | 0.2315 | 0 |
| −0.0847 | 0.8969 | 0 |
| −0.6666 | −1.739 | 0 |
| −2.516 | 0.4688 | 0 |
| −0.7351 | −0.5441 | 0 |
| −1.020 | −0.1654 | 0 |
| 0.1136 | 0.5522 | 0 |
| −0.4738 | 1.033 | 0 |
| −0.4082 | −0.0462 | 0 |
| −0.7304 | 2.639 | 0 |
| −0.2214 | 0.5890 | 0 |
| −0.2258 | −0.2024 | 0 |
| −2.547 | 0.4416 | 0 |
| 1.347 | −0.1003 | 0 |
| 0.6164 | −1.093 | 0 |
| 0.2176 | 0.5032 | 0 |
| −0.8047 | 0.9492 | 0 |
| 0.6897 | 0.3828 | 0 |
| −0.3287 | 0.3716 | 0 |
| −0.1647 | 0.1572 | 0 |
| −1.392 | 0.8479 | 0 |
| 1.466 | −1.283 | 0 |
| 0.0482 | 1.158 | 0 |
| 1.908 | −0.9091 | 0 |
| 1.731 | 0.3350 | 0 |
| 0.0581 | 0.7759 | 0 |
| 0.6453 | 0.2371 | 0 |
| 1.726 | −1.560 | 0 |
| −0.5290 | 0.0243 | 0 |
| 0.1664 | 0.3346 | 0 |
| −0.2547 | 0.9894 | 0 |
| 0.3328 | 0.5004 | 0 |
| 0.1824 | 0.5769 | 0 |
| 1.165 | 1.143 | 0 |
| 0.5935 | −0.7074 | 0 |
| −0.8918 | −0.6186 | 0 |
| 0.5773 | −0.0083 | 0 |
| −0.8249 | 0.3389 | 0 |
| −1.157 | 1.405 | 0 |
| 0.7780 | −0.8653 | 0 |
| −1.204 | −0.8740 | 0 |
| 0.3067 | 0.7743 | 0 |
| −0.8337 | −0.4019 | 0 |
| 1.418 | −0.2151 | 0 |
| 0.7118 | 0.6053 | 0 |
| −0.4025 | −0.6146 | 0 |
| 0.7994 | −0.7244 | 0 |
| 0.4265 | −0.2569 | 0 |
| −1.170 | −0.3921 | 0 |
| −0.2062 | −0.6867 | 0 |
| −0.9306 | 0.8758 | 0 |
| 0.4494 | −0.4811 | 0 |
| −0.6448 | 0.0684 | 0 |
| −0.2314 | 0.1370 | 0 |
| −1.236 | −1.859 | 0 |
| −0.9610 | 0.3463 | 0 |
| 0.1340 | 1.502 | 0 |
| −0.9991 | −0.0137 | 0 |
First 60 of 120 rows.
Assumptions
- Same subjects for both markers — Both curves use the same 120 subjects.
- Enough cases and non-cases — 60 cases and 60 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| score1 | 0.7583 | 0.0431 |
| score2 | 0.6969 | 0.0482 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | 0.9469 | 0.3437 | Equal areas under the curves? |
| Venkatraman (999 permutations) | 462 | 0.4880 | Identical curves at every operating point? |
- Condition present: truth = true. Correlation between the AUC estimates r = -0.005.
- Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross. Its p-value is a permutation p (999 within-subject swaps, seed 1) and matches in Python and R.
The exact R script · R R version 4.5.1 (2025-06-13)
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec a11b89162715f774
# Dataset: c3_roc (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_delong_paired, cs_roc_points (twins of codesplash.stats.roc)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/c3_roc.parquet", col_select = c("score1", "score2", "truth")))
n_input <- nrow(df)
df[["truth"]] <- factor(df[["truth"]], levels = c("false", "true")) # stored level order
df <- df[stats::complete.cases(df[c("score1", "score2", "truth")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("score1", "score2", "truth")) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1 <- "score1"
m2 <- "score2"
r <- "truth"
ref_levels <- c("false", "true") # stored level order
pos_level <- ref_levels[length(ref_levels)] # default: the last level ("true")
s1 <- if ("higher" == "higher") 1 else -1 # explicit directions; never flipped automatically
s2 <- if ("higher" == "higher") 1 else -1
is_pos <- as.character(df[[r]]) == pos_level
x1 <- s1 * as.numeric(df[[m1]])
x2 <- s2 * as.numeric(df[[m2]])
npos <- sum(is_pos)
nneg <- sum(!is_pos)
if (npos == 0 || nneg == 0) cs_error("CS_E_TOO_FEW_GROUPS", column = r, min = 2, k = 1) # needs positives and negatives
conf <- 0.95
res <- cs_delong_paired(x1[is_pos], x1[!is_pos], x2[is_pos], x2[!is_pos], conf)
test <- "venkatraman"
reps <- as.integer(999)
seed <- as.integer(1)
vk <- if (test == "venkatraman") cs_venkatraman_paired(x1, x2, is_pos, reps, seed) else NULL
bt <- if (test == "bootstrap") cs_bootstrap_roc_paired(x1, x2, is_pos, reps, seed) else NULL
if (test == "venkatraman") {
test_label <- "Venkatraman's permutation test (equal ROC curves)"; test_stat <- vk$e; test_p <- vk$p
} else if (test == "bootstrap") {
test_label <- "Paired bootstrap test (equal AUCs)"; test_stat <- bt$z; test_p <- bt$p
} else {
test_label <- "DeLong's paired test (equal AUCs)"; test_stat <- res$z; test_p <- res$p
}
# %% Assumptions
assumptions <- list(
list(key = "pairing", label = "Same subjects for both markers", status = "info", detail = sprintf("Both curves use the same %d subjects.", npos + nneg)),
list(key = "sample_size", label = "Enough cases and non-cases", status = if (min(npos, nneg) < 10) "warn" else "pass",
detail = sprintf("%d cases and %d non-cases%s", npos, nneg, if (min(npos, nneg) < 10) "; the normal approximation is rough." else "."))
)
# %% Result
curves <- lapply(list(list(m1, x1), list(m2, x2)), function(mx) {
pts <- cs_roc_points(mx[[2]][is_pos], mx[[2]][!is_pos])
k <- length(pts$tpr)
keep <- unique(round(seq(1, k, length.out = min(k, 1500))))
data.frame(fpr = pts$fpr[keep], tpr = pts$tpr[keep], curve = mx[[1]])
})
curves <- do.call(rbind, curves)
pct <- sprintf("%.0f%%", conf * 100)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
.rc <- unique(round(seq(1, npos + nneg, length.out = min(npos + nneg, 5000))))
compare_chart <- cs_chart("compare", "roc-compare", sprintf("%s against %s", m1, m2), list(
label = cs_col(sprintf("%s = %s", r, pos_level), as.numeric(is_pos[.rc]), "numeric"),
a = cs_col(m1, x1[.rc], "numeric"), b = cs_col(m2, x2[.rc], "numeric")
), controls = list(level = conf), note = if (length(.rc) < npos + nneg) sprintf("Drawn from %s evenly spaced rows; the tests above use all %s.", format(length(.rc), big.mark = ","), format(npos + nneg, big.mark = ",")) else NULL)
cs_publish(list(
method = "diagnostic.roc_compare",
methodVersion = "1.1.2",
title = "Comparison of two ROC curves",
engine = cs_engine_info("stats", "nanoparquet"),
n = npos + nneg,
summary = paste0(sprintf("AUC %.3f (%s) vs %.3f (%s): difference %.3f, %s CI [%.3f, %.3f]; ", res$auc_1, m1, res$auc_2, m2, res$diff, pct, res$low, res$high),
if (!is.null(vk)) sprintf("Venkatraman E = %.0f, %s.", vk$e, cs_fmt_p(vk$p))
else if (!is.null(bt)) sprintf("bootstrap z = %.2f, %s.", bt$z, cs_fmt_p(bt$p))
else sprintf("DeLong z = %.2f, %s.", res$z, cs_fmt_p(res$p))),
apa = sprintf("ΔAUC = %s, %s CI [%s, %s], z = %s, %s", cs_fmt_num(res$diff, 3), pct, cs_fmt_num(res$low, 3), cs_fmt_num(res$high, 3),
cs_fmt_num(res$z), cs_fmt_p(res$p)),
statistics = c(list(
list(key = "auc_diff", label = sprintf("AUC difference (%s − %s)", m1, m2), value = res$diff),
list(key = "z", label = "z (DeLong)", value = res$z), list(key = "p", label = "p (DeLong)", value = res$p, format = "p")),
if (!is.null(vk)) list(list(key = "venkatraman_e", label = "Venkatraman E", value = vk$e), list(key = "venkatraman_p", label = "p (Venkatraman)", value = vk$p, format = "p")),
if (!is.null(bt)) list(list(key = "boot_z", label = "z (bootstrap)", value = bt$z), list(key = "boot_p", label = "p (bootstrap)", value = bt$p, format = "p"))
),
tables = list(list(
key = "aucs", title = "Areas under the curves",
columns = list(list(key = "marker", label = "Marker", type = "string"), list(key = "auc", label = "AUC", type = "number"),
list(key = "se", label = "SE (DeLong)", type = "number")),
rows = list(list(marker = m1, auc = res$auc_1, se = res$se_1), list(marker = m2, auc = res$auc_2, se = res$se_2))
), list(
key = "tests", title = "Tests",
columns = list(list(key = "test", label = "Test", type = "string"), list(key = "statistic", label = "Statistic", type = "number"),
list(key = "p", label = "p", type = "p"), list(key = "question", label = "Question", type = "string")),
rows = c(list(list(test = "DeLong (paired)", statistic = res$z, p = res$p, question = "Equal areas under the curves?")),
if (!is.null(vk)) list(list(test = sprintf("Venkatraman (%s permutations)", format(reps, big.mark = ",")), statistic = vk$e, p = vk$p,
question = "Identical curves at every operating point?")),
if (!is.null(bt)) list(list(test = sprintf("Paired bootstrap (%s resamples)", format(reps, big.mark = ",")), statistic = bt$z, p = bt$p,
question = "Equal areas under the curves?")))
)),
effectSizes = list(list(key = "auc_diff", label = "AUC difference", value = res$diff, ciLow = res$low, ciHigh = res$high, ciLevel = conf,
method = "DeLong paired variance")),
assumptions = assumptions,
warnings = warnings,
notes = c(list(sprintf("Condition present: %s = %s. Correlation between the AUC estimates r = %.3f.", r, pos_level, res$r)),
if (!is.null(vk)) list(paste("Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross.",
sprintf("Its p-value is a permutation p (%s within-subject swaps, seed %d) and matches in Python and R.", format(reps, big.mark = ","), seed))),
if (!is.null(bt)) list(sprintf("The bootstrap resamples cases and non-cases separately (%s resamples, seed %d); z = ΔAUC / SD of the bootstrap differences.",
format(reps, big.mark = ","), seed))),
charts = list(cs_chart("roc", "roc-curve", "ROC curves",
list(fpr = cs_col("1 − specificity", curves$fpr), tpr = cs_col("Sensitivity", curves$tpr),
curve = cs_col("marker", curves$curve, levels = c(m1, m2)))), compare_chart),
values = list(
n_pos = npos, n_neg = nneg, auc_1 = res$auc_1, auc_2 = res$auc_2, se_1 = res$se_1, se_2 = res$se_2,
auc_diff = res$diff, diff_se = res$se, diff_ci_low = res$low, diff_ci_high = res$high, z = res$z, p = res$p,
r = res$r, positive_level = pos_level, test = test, test_label = test_label, test_statistic = test_stat, test_p = test_p,
venkatraman_e = if (is.null(vk)) NULL else vk$e, venkatraman_p = if (is.null(vk)) NULL else vk$p,
boot_sd = if (is.null(bt)) NULL else bt$sd, boot_z = if (is.null(bt)) NULL else bt$z, boot_p = if (is.null(bt)) NULL else bt$p,
reps = if (is.null(vk) && is.null(bt)) NULL else reps
)
))
ΔAUC = 0.061, 95% CI [−0.066, 0.188], z = 0.95, p = .344
- AUC difference (score1 − score2)
- 0.0614
- z (DeLong)
- 0.9469
- p (DeLong)
- .344
- Venkatraman E
- 462
- p (Venkatraman)
- .488
Effect size
- AUC difference 0.0614 95% CI [−0.0657, 0.1885]
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Same subjects for both markers — Both curves use the same 120 subjects.
- Enough cases and non-cases — 60 cases and 60 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| score1 | 0.7583 | 0.0431 |
| score2 | 0.6969 | 0.0482 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | 0.9469 | 0.3437 | Equal areas under the curves? |
| Venkatraman (999 permutations) | 462 | 0.4880 | Identical curves at every operating point? |
- Condition present: truth = true. Correlation between the AUC estimates r = -0.005.
- Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross. Its p-value is a permutation p (999 within-subject swaps, seed 1) and matches in Python and R.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec a11b89162715f774
# Dataset: c3_roc (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import roc as csr # paired DeLong test (twin of codesplashr::cs_delong_paired)
# %% Data
df = pd.read_parquet("tests/parity/fixtures/c3_roc.parquet", columns=["score1", "score2", "truth"])
n_input = len(df)
df["truth"] = pd.Categorical(df["truth"], categories=["false", "true"]) # stored level order
df = df.dropna(subset=["score1", "score2", "truth"]) # listwise deletion over the analysis columns
df["truth"] = df["truth"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["score1", "score2", "truth"]) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1, m2, r = "score1", "score2", "truth"
ref_levels = ["false", "true"] # stored level order
pos_level = ref_levels[-1] # default: the last level ("true")
s1 = 1.0 if "higher" == "higher" else -1.0 # explicit directions; never flipped automatically
s2 = 1.0 if "higher" == "higher" else -1.0
is_pos = (df[r].astype(str) == pos_level).to_numpy()
x1, x2 = s1 * df[m1].to_numpy(dtype=float), s2 * df[m2].to_numpy(dtype=float)
npos, nneg = int(is_pos.sum()), int((~is_pos).sum())
if npos == 0 or nneg == 0:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=r, min=2, k=1) # needs positives and negatives
conf = 0.95
res = csr.delong_paired(x1[is_pos], x1[~is_pos], x2[is_pos], x2[~is_pos], conf)
test = "venkatraman"
reps = int(999)
seed = int(1)
vk = csr.venkatraman_paired(x1, x2, is_pos, reps, seed) if test == "venkatraman" else None
bt = csr.bootstrap_paired(x1, x2, is_pos, reps, seed) if test == "bootstrap" else None
if test == "venkatraman":
test_label, test_stat, test_p = "Venkatraman's permutation test (equal ROC curves)", vk["e"], vk["p"]
elif test == "bootstrap":
test_label, test_stat, test_p = "Paired bootstrap test (equal AUCs)", bt["z"], bt["p"]
else:
test_label, test_stat, test_p = "DeLong's paired test (equal AUCs)", res["z"], res["p"]
# %% Assumptions
assumptions = [
{"key": "pairing", "label": "Same subjects for both markers", "status": "info", "detail": f"Both curves use the same {npos + nneg} subjects."},
{"key": "sample_size", "label": "Enough cases and non-cases", "status": "warn" if min(npos, nneg) < 10 else "pass",
"detail": f"{npos} cases and {nneg} non-cases" + ("; the normal approximation is rough." if min(npos, nneg) < 10 else ".")},
]
# %% Result
curves_fpr, curves_tpr, curves_lab = [], [], []
for name, x in ((m1, x1), (m2, x2)):
pts = csr.roc_points(x[is_pos], x[~is_pos])
k = len(pts["tpr"])
keep = np.unique(np.round(np.linspace(0, k - 1, min(k, 1500))).astype(int))
curves_fpr += pts["fpr"][keep].tolist()
curves_tpr += pts["tpr"][keep].tolist()
curves_lab += [name] * len(keep)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
_rc = np.unique(np.round(np.linspace(0, npos + nneg - 1, min(npos + nneg, 5000))).astype(int))
compare_chart = cs.chart("compare", "roc-compare", f"{m1} against {m2}", {
"label": cs.col(f"{r} = {pos_level}", [1 if is_pos[i] else 0 for i in _rc], "numeric"),
"a": cs.col(m1, x1[_rc], "numeric"), "b": cs.col(m2, x2[_rc], "numeric"),
}, controls={"level": conf}, note=None if len(_rc) == npos + nneg else f"Drawn from {len(_rc):,} evenly spaced rows; the tests above use all {npos + nneg:,}.")
cs.publish({
"method": "diagnostic.roc_compare",
"methodVersion": "1.1.2",
"title": "Comparison of two ROC curves",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": npos + nneg,
"summary": f"AUC {res['auc_1']:.3f} ({m1}) vs {res['auc_2']:.3f} ({m2}): difference {res['diff']:.3f}, {conf:.0%} CI [{res['low']:.3f}, {res['high']:.3f}]; "
+ (f"Venkatraman E = {vk['e']:.0f}, {cs.fmt_p(vk['p'])}." if vk else f"bootstrap z = {bt['z']:.2f}, {cs.fmt_p(bt['p'])}." if bt
else f"DeLong z = {res['z']:.2f}, {cs.fmt_p(res['p'])}."),
"apa": f"ΔAUC = {cs.fmt_num(res['diff'], 3)}, {conf:.0%} CI [{cs.fmt_num(res['low'], 3)}, {cs.fmt_num(res['high'], 3)}], z = {cs.fmt_num(res['z'])}, {cs.fmt_p(res['p'])}",
"statistics": [
{"key": "auc_diff", "label": f"AUC difference ({m1} − {m2})", "value": res["diff"]},
{"key": "z", "label": "z (DeLong)", "value": res["z"]}, {"key": "p", "label": "p (DeLong)", "value": res["p"], "format": "p"},
] + ([{"key": "venkatraman_e", "label": "Venkatraman E", "value": vk["e"]}, {"key": "venkatraman_p", "label": "p (Venkatraman)", "value": vk["p"], "format": "p"}] if vk else [])
+ ([{"key": "boot_z", "label": "z (bootstrap)", "value": bt["z"]}, {"key": "boot_p", "label": "p (bootstrap)", "value": bt["p"], "format": "p"}] if bt else []),
"tables": [{
"key": "aucs", "title": "Areas under the curves",
"columns": [{"key": "marker", "label": "Marker", "type": "string"}, {"key": "auc", "label": "AUC", "type": "number"},
{"key": "se", "label": "SE (DeLong)", "type": "number"}],
"rows": [{"marker": m1, "auc": res["auc_1"], "se": res["se_1"]}, {"marker": m2, "auc": res["auc_2"], "se": res["se_2"]}],
}, {
"key": "tests", "title": "Tests", "columns": [{"key": "test", "label": "Test", "type": "string"}, {"key": "statistic", "label": "Statistic", "type": "number"},
{"key": "p", "label": "p", "type": "p"}, {"key": "question", "label": "Question", "type": "string"}],
"rows": [{"test": "DeLong (paired)", "statistic": res["z"], "p": res["p"], "question": "Equal areas under the curves?"}]
+ ([{"test": f"Venkatraman ({reps:,} permutations)", "statistic": vk["e"], "p": vk["p"], "question": "Identical curves at every operating point?"}] if vk else [])
+ ([{"test": f"Paired bootstrap ({reps:,} resamples)", "statistic": bt["z"], "p": bt["p"], "question": "Equal areas under the curves?"}] if bt else []),
}],
"effectSizes": [{"key": "auc_diff", "label": "AUC difference", "value": res["diff"], "ciLow": res["low"], "ciHigh": res["high"], "ciLevel": conf,
"method": "DeLong paired variance"}],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Condition present: {r} = {pos_level}. Correlation between the AUC estimates r = {res['r']:.3f}."]
+ (["Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross. "
f"Its p-value is a permutation p ({reps:,} within-subject swaps, seed {seed}) and matches in Python and R."] if vk else [])
+ ([f"The bootstrap resamples cases and non-cases separately ({reps:,} resamples, seed {seed}); z = ΔAUC / SD of the bootstrap differences."] if bt else []),
"charts": [cs.chart("roc", "roc-curve", "ROC curves", {"fpr": cs.col("1 − specificity", curves_fpr), "tpr": cs.col("Sensitivity", curves_tpr),
"curve": cs.col("marker", curves_lab, levels=[m1, m2])}), compare_chart],
"values": {
"n_pos": npos, "n_neg": nneg, "auc_1": res["auc_1"], "auc_2": res["auc_2"], "se_1": res["se_1"], "se_2": res["se_2"],
"auc_diff": res["diff"], "diff_se": res["se"], "diff_ci_low": res["low"], "diff_ci_high": res["high"], "z": res["z"], "p": res["p"],
"r": res["r"], "positive_level": pos_level, "test": test, "test_label": test_label, "test_statistic": test_stat, "test_p": test_p,
"venkatraman_e": vk["e"] if vk else None, "venkatraman_p": vk["p"] if vk else None,
"boot_sd": bt["sd"] if bt else None, "boot_z": bt["z"] if bt else None, "boot_p": bt["p"] if bt else None, "reps": reps if (vk or bt) else None,
},
})
In the biopsies it gives p = .002. Rejecting says the curves differ somewhere, which may or may not be at a threshold you'd use.
A resampling check
In the appPaired bootstrap tests equal areas, like DeLong. It resamples cases and non-cases separately, and swaps DeLong's variance formula for the spread of the resampled differences.
ΔAUC = 0.061, 95% CI [−0.066, 0.188], z = 0.95, p = .344
- AUC difference (score1 − score2)
- 0.0614
- z (DeLong)
- 0.9469
- p (DeLong)
- .344
- z (bootstrap)
- 0.9640
- p (bootstrap)
- .335
Effect size
- AUC difference 0.0614 95% CI [−0.0657, 0.1885]
Plots
View data
| marker | 1 − specificity | Sensitivity |
|---|---|---|
| score1 | 0 | 0 |
| score1 | 0 | 0.0167 |
| score1 | 0 | 0.0333 |
| score1 | 0 | 0.0500 |
| score1 | 0 | 0.0667 |
| score1 | 0 | 0.0833 |
| score1 | 0 | 0.1000 |
| score1 | 0 | 0.1167 |
| score1 | 0 | 0.1333 |
| score1 | 0 | 0.1500 |
| score1 | 0 | 0.1667 |
| score1 | 0 | 0.1833 |
| score1 | 0 | 0.2000 |
| score1 | 0 | 0.2167 |
| score1 | 0.0167 | 0.2167 |
| score1 | 0.0167 | 0.2333 |
| score1 | 0.0167 | 0.2500 |
| score1 | 0.0333 | 0.2500 |
| score1 | 0.0500 | 0.2500 |
| score1 | 0.0500 | 0.2667 |
| score1 | 0.0500 | 0.2833 |
| score1 | 0.0500 | 0.3000 |
| score1 | 0.0500 | 0.3167 |
| score1 | 0.0667 | 0.3167 |
| score1 | 0.0667 | 0.3333 |
| score1 | 0.0833 | 0.3333 |
| score1 | 0.0833 | 0.3500 |
| score1 | 0.1000 | 0.3500 |
| score1 | 0.1000 | 0.3667 |
| score1 | 0.1167 | 0.3667 |
| score1 | 0.1167 | 0.3833 |
| score1 | 0.1167 | 0.4000 |
| score1 | 0.1167 | 0.4167 |
| score1 | 0.1167 | 0.4333 |
| score1 | 0.1167 | 0.4500 |
| score1 | 0.1167 | 0.4667 |
| score1 | 0.1167 | 0.4833 |
| score1 | 0.1333 | 0.4833 |
| score1 | 0.1500 | 0.4833 |
| score1 | 0.1500 | 0.5000 |
| score1 | 0.1500 | 0.5167 |
| score1 | 0.1500 | 0.5333 |
| score1 | 0.1500 | 0.5500 |
| score1 | 0.1667 | 0.5500 |
| score1 | 0.1667 | 0.5667 |
| score1 | 0.1833 | 0.5667 |
| score1 | 0.1833 | 0.5833 |
| score1 | 0.1833 | 0.6000 |
| score1 | 0.2000 | 0.6000 |
| score1 | 0.2167 | 0.6000 |
| score1 | 0.2333 | 0.6000 |
| score1 | 0.2500 | 0.6000 |
| score1 | 0.2500 | 0.6167 |
| score1 | 0.2667 | 0.6167 |
| score1 | 0.2667 | 0.6333 |
| score1 | 0.2667 | 0.6500 |
| score1 | 0.2833 | 0.6500 |
| score1 | 0.3000 | 0.6500 |
| score1 | 0.3167 | 0.6500 |
| score1 | 0.3333 | 0.6500 |
First 60 of 242 rows.
View data
| score1 | score2 | truth = true |
|---|---|---|
| 0.5206 | 1.906 | 0 |
| −1.080 | 2.132 | 0 |
| 0.1392 | 0.2315 | 0 |
| −0.0847 | 0.8969 | 0 |
| −0.6666 | −1.739 | 0 |
| −2.516 | 0.4688 | 0 |
| −0.7351 | −0.5441 | 0 |
| −1.020 | −0.1654 | 0 |
| 0.1136 | 0.5522 | 0 |
| −0.4738 | 1.033 | 0 |
| −0.4082 | −0.0462 | 0 |
| −0.7304 | 2.639 | 0 |
| −0.2214 | 0.5890 | 0 |
| −0.2258 | −0.2024 | 0 |
| −2.547 | 0.4416 | 0 |
| 1.347 | −0.1003 | 0 |
| 0.6164 | −1.093 | 0 |
| 0.2176 | 0.5032 | 0 |
| −0.8047 | 0.9492 | 0 |
| 0.6897 | 0.3828 | 0 |
| −0.3287 | 0.3716 | 0 |
| −0.1647 | 0.1572 | 0 |
| −1.392 | 0.8479 | 0 |
| 1.466 | −1.283 | 0 |
| 0.0482 | 1.158 | 0 |
| 1.908 | −0.9091 | 0 |
| 1.731 | 0.3350 | 0 |
| 0.0581 | 0.7759 | 0 |
| 0.6453 | 0.2371 | 0 |
| 1.726 | −1.560 | 0 |
| −0.5290 | 0.0243 | 0 |
| 0.1664 | 0.3346 | 0 |
| −0.2547 | 0.9894 | 0 |
| 0.3328 | 0.5004 | 0 |
| 0.1824 | 0.5769 | 0 |
| 1.165 | 1.143 | 0 |
| 0.5935 | −0.7074 | 0 |
| −0.8918 | −0.6186 | 0 |
| 0.5773 | −0.0083 | 0 |
| −0.8249 | 0.3389 | 0 |
| −1.157 | 1.405 | 0 |
| 0.7780 | −0.8653 | 0 |
| −1.204 | −0.8740 | 0 |
| 0.3067 | 0.7743 | 0 |
| −0.8337 | −0.4019 | 0 |
| 1.418 | −0.2151 | 0 |
| 0.7118 | 0.6053 | 0 |
| −0.4025 | −0.6146 | 0 |
| 0.7994 | −0.7244 | 0 |
| 0.4265 | −0.2569 | 0 |
| −1.170 | −0.3921 | 0 |
| −0.2062 | −0.6867 | 0 |
| −0.9306 | 0.8758 | 0 |
| 0.4494 | −0.4811 | 0 |
| −0.6448 | 0.0684 | 0 |
| −0.2314 | 0.1370 | 0 |
| −1.236 | −1.859 | 0 |
| −0.9610 | 0.3463 | 0 |
| 0.1340 | 1.502 | 0 |
| −0.9991 | −0.0137 | 0 |
First 60 of 120 rows.
Assumptions
- Same subjects for both markers — Both curves use the same 120 subjects.
- Enough cases and non-cases — 60 cases and 60 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| score1 | 0.7583 | 0.0431 |
| score2 | 0.6969 | 0.0482 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | 0.9469 | 0.3437 | Equal areas under the curves? |
| Paired bootstrap (1,000 resamples) | 0.9640 | 0.3350 | Equal areas under the curves? |
- Condition present: truth = true. Correlation between the AUC estimates r = -0.005.
- The bootstrap resamples cases and non-cases separately (1,000 resamples, seed 1); z = ΔAUC / SD of the bootstrap differences.
The exact R script · R R version 4.5.1 (2025-06-13)
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec 9b5a85d4f2a78142
# Dataset: c3_roc (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_delong_paired, cs_roc_points (twins of codesplash.stats.roc)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/c3_roc.parquet", col_select = c("score1", "score2", "truth")))
n_input <- nrow(df)
df[["truth"]] <- factor(df[["truth"]], levels = c("false", "true")) # stored level order
df <- df[stats::complete.cases(df[c("score1", "score2", "truth")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("score1", "score2", "truth")) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1 <- "score1"
m2 <- "score2"
r <- "truth"
ref_levels <- c("false", "true") # stored level order
pos_level <- ref_levels[length(ref_levels)] # default: the last level ("true")
s1 <- if ("higher" == "higher") 1 else -1 # explicit directions; never flipped automatically
s2 <- if ("higher" == "higher") 1 else -1
is_pos <- as.character(df[[r]]) == pos_level
x1 <- s1 * as.numeric(df[[m1]])
x2 <- s2 * as.numeric(df[[m2]])
npos <- sum(is_pos)
nneg <- sum(!is_pos)
if (npos == 0 || nneg == 0) cs_error("CS_E_TOO_FEW_GROUPS", column = r, min = 2, k = 1) # needs positives and negatives
conf <- 0.95
res <- cs_delong_paired(x1[is_pos], x1[!is_pos], x2[is_pos], x2[!is_pos], conf)
test <- "bootstrap"
reps <- as.integer(1000)
seed <- as.integer(1)
vk <- if (test == "venkatraman") cs_venkatraman_paired(x1, x2, is_pos, reps, seed) else NULL
bt <- if (test == "bootstrap") cs_bootstrap_roc_paired(x1, x2, is_pos, reps, seed) else NULL
if (test == "venkatraman") {
test_label <- "Venkatraman's permutation test (equal ROC curves)"; test_stat <- vk$e; test_p <- vk$p
} else if (test == "bootstrap") {
test_label <- "Paired bootstrap test (equal AUCs)"; test_stat <- bt$z; test_p <- bt$p
} else {
test_label <- "DeLong's paired test (equal AUCs)"; test_stat <- res$z; test_p <- res$p
}
# %% Assumptions
assumptions <- list(
list(key = "pairing", label = "Same subjects for both markers", status = "info", detail = sprintf("Both curves use the same %d subjects.", npos + nneg)),
list(key = "sample_size", label = "Enough cases and non-cases", status = if (min(npos, nneg) < 10) "warn" else "pass",
detail = sprintf("%d cases and %d non-cases%s", npos, nneg, if (min(npos, nneg) < 10) "; the normal approximation is rough." else "."))
)
# %% Result
curves <- lapply(list(list(m1, x1), list(m2, x2)), function(mx) {
pts <- cs_roc_points(mx[[2]][is_pos], mx[[2]][!is_pos])
k <- length(pts$tpr)
keep <- unique(round(seq(1, k, length.out = min(k, 1500))))
data.frame(fpr = pts$fpr[keep], tpr = pts$tpr[keep], curve = mx[[1]])
})
curves <- do.call(rbind, curves)
pct <- sprintf("%.0f%%", conf * 100)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
.rc <- unique(round(seq(1, npos + nneg, length.out = min(npos + nneg, 5000))))
compare_chart <- cs_chart("compare", "roc-compare", sprintf("%s against %s", m1, m2), list(
label = cs_col(sprintf("%s = %s", r, pos_level), as.numeric(is_pos[.rc]), "numeric"),
a = cs_col(m1, x1[.rc], "numeric"), b = cs_col(m2, x2[.rc], "numeric")
), controls = list(level = conf), note = if (length(.rc) < npos + nneg) sprintf("Drawn from %s evenly spaced rows; the tests above use all %s.", format(length(.rc), big.mark = ","), format(npos + nneg, big.mark = ",")) else NULL)
cs_publish(list(
method = "diagnostic.roc_compare",
methodVersion = "1.1.2",
title = "Comparison of two ROC curves",
engine = cs_engine_info("stats", "nanoparquet"),
n = npos + nneg,
summary = paste0(sprintf("AUC %.3f (%s) vs %.3f (%s): difference %.3f, %s CI [%.3f, %.3f]; ", res$auc_1, m1, res$auc_2, m2, res$diff, pct, res$low, res$high),
if (!is.null(vk)) sprintf("Venkatraman E = %.0f, %s.", vk$e, cs_fmt_p(vk$p))
else if (!is.null(bt)) sprintf("bootstrap z = %.2f, %s.", bt$z, cs_fmt_p(bt$p))
else sprintf("DeLong z = %.2f, %s.", res$z, cs_fmt_p(res$p))),
apa = sprintf("ΔAUC = %s, %s CI [%s, %s], z = %s, %s", cs_fmt_num(res$diff, 3), pct, cs_fmt_num(res$low, 3), cs_fmt_num(res$high, 3),
cs_fmt_num(res$z), cs_fmt_p(res$p)),
statistics = c(list(
list(key = "auc_diff", label = sprintf("AUC difference (%s − %s)", m1, m2), value = res$diff),
list(key = "z", label = "z (DeLong)", value = res$z), list(key = "p", label = "p (DeLong)", value = res$p, format = "p")),
if (!is.null(vk)) list(list(key = "venkatraman_e", label = "Venkatraman E", value = vk$e), list(key = "venkatraman_p", label = "p (Venkatraman)", value = vk$p, format = "p")),
if (!is.null(bt)) list(list(key = "boot_z", label = "z (bootstrap)", value = bt$z), list(key = "boot_p", label = "p (bootstrap)", value = bt$p, format = "p"))
),
tables = list(list(
key = "aucs", title = "Areas under the curves",
columns = list(list(key = "marker", label = "Marker", type = "string"), list(key = "auc", label = "AUC", type = "number"),
list(key = "se", label = "SE (DeLong)", type = "number")),
rows = list(list(marker = m1, auc = res$auc_1, se = res$se_1), list(marker = m2, auc = res$auc_2, se = res$se_2))
), list(
key = "tests", title = "Tests",
columns = list(list(key = "test", label = "Test", type = "string"), list(key = "statistic", label = "Statistic", type = "number"),
list(key = "p", label = "p", type = "p"), list(key = "question", label = "Question", type = "string")),
rows = c(list(list(test = "DeLong (paired)", statistic = res$z, p = res$p, question = "Equal areas under the curves?")),
if (!is.null(vk)) list(list(test = sprintf("Venkatraman (%s permutations)", format(reps, big.mark = ",")), statistic = vk$e, p = vk$p,
question = "Identical curves at every operating point?")),
if (!is.null(bt)) list(list(test = sprintf("Paired bootstrap (%s resamples)", format(reps, big.mark = ",")), statistic = bt$z, p = bt$p,
question = "Equal areas under the curves?")))
)),
effectSizes = list(list(key = "auc_diff", label = "AUC difference", value = res$diff, ciLow = res$low, ciHigh = res$high, ciLevel = conf,
method = "DeLong paired variance")),
assumptions = assumptions,
warnings = warnings,
notes = c(list(sprintf("Condition present: %s = %s. Correlation between the AUC estimates r = %.3f.", r, pos_level, res$r)),
if (!is.null(vk)) list(paste("Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross.",
sprintf("Its p-value is a permutation p (%s within-subject swaps, seed %d) and matches in Python and R.", format(reps, big.mark = ","), seed))),
if (!is.null(bt)) list(sprintf("The bootstrap resamples cases and non-cases separately (%s resamples, seed %d); z = ΔAUC / SD of the bootstrap differences.",
format(reps, big.mark = ","), seed))),
charts = list(cs_chart("roc", "roc-curve", "ROC curves",
list(fpr = cs_col("1 − specificity", curves$fpr), tpr = cs_col("Sensitivity", curves$tpr),
curve = cs_col("marker", curves$curve, levels = c(m1, m2)))), compare_chart),
values = list(
n_pos = npos, n_neg = nneg, auc_1 = res$auc_1, auc_2 = res$auc_2, se_1 = res$se_1, se_2 = res$se_2,
auc_diff = res$diff, diff_se = res$se, diff_ci_low = res$low, diff_ci_high = res$high, z = res$z, p = res$p,
r = res$r, positive_level = pos_level, test = test, test_label = test_label, test_statistic = test_stat, test_p = test_p,
venkatraman_e = if (is.null(vk)) NULL else vk$e, venkatraman_p = if (is.null(vk)) NULL else vk$p,
boot_sd = if (is.null(bt)) NULL else bt$sd, boot_z = if (is.null(bt)) NULL else bt$z, boot_p = if (is.null(bt)) NULL else bt$p,
reps = if (is.null(vk) && is.null(bt)) NULL else reps
)
))
ΔAUC = 0.061, 95% CI [−0.066, 0.188], z = 0.95, p = .344
- AUC difference (score1 − score2)
- 0.0614
- z (DeLong)
- 0.9469
- p (DeLong)
- .344
- z (bootstrap)
- 0.9640
- p (bootstrap)
- .335
Effect size
- AUC difference 0.0614 95% CI [−0.0657, 0.1885]
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Same subjects for both markers — Both curves use the same 120 subjects.
- Enough cases and non-cases — 60 cases and 60 non-cases.
Areas under the curves
| Marker | AUC | SE (DeLong) |
|---|---|---|
| score1 | 0.7583 | 0.0431 |
| score2 | 0.6969 | 0.0482 |
Tests
| Test | Statistic | p | Question |
|---|---|---|---|
| DeLong (paired) | 0.9469 | 0.3437 | Equal areas under the curves? |
| Paired bootstrap (1,000 resamples) | 0.9640 | 0.3350 | Equal areas under the curves? |
- Condition present: truth = true. Correlation between the AUC estimates r = -0.005.
- The bootstrap resamples cases and non-cases separately (1,000 resamples, seed 1); z = ΔAUC / SD of the bootstrap differences.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Compare two ROC curves (paired)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method diagnostic.roc_compare 1.1.2 · spec 9b5a85d4f2a78142
# Dataset: c3_roc (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import roc as csr # paired DeLong test (twin of codesplashr::cs_delong_paired)
# %% Data
df = pd.read_parquet("tests/parity/fixtures/c3_roc.parquet", columns=["score1", "score2", "truth"])
n_input = len(df)
df["truth"] = pd.Categorical(df["truth"], categories=["false", "true"]) # stored level order
df = df.dropna(subset=["score1", "score2", "truth"]) # listwise deletion over the analysis columns
df["truth"] = df["truth"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["score1", "score2", "truth"]) # infinite values stop the run (registry/errors.yaml)
# %% Test
m1, m2, r = "score1", "score2", "truth"
ref_levels = ["false", "true"] # stored level order
pos_level = ref_levels[-1] # default: the last level ("true")
s1 = 1.0 if "higher" == "higher" else -1.0 # explicit directions; never flipped automatically
s2 = 1.0 if "higher" == "higher" else -1.0
is_pos = (df[r].astype(str) == pos_level).to_numpy()
x1, x2 = s1 * df[m1].to_numpy(dtype=float), s2 * df[m2].to_numpy(dtype=float)
npos, nneg = int(is_pos.sum()), int((~is_pos).sum())
if npos == 0 or nneg == 0:
cs.cs_error("CS_E_TOO_FEW_GROUPS", column=r, min=2, k=1) # needs positives and negatives
conf = 0.95
res = csr.delong_paired(x1[is_pos], x1[~is_pos], x2[is_pos], x2[~is_pos], conf)
test = "bootstrap"
reps = int(1000)
seed = int(1)
vk = csr.venkatraman_paired(x1, x2, is_pos, reps, seed) if test == "venkatraman" else None
bt = csr.bootstrap_paired(x1, x2, is_pos, reps, seed) if test == "bootstrap" else None
if test == "venkatraman":
test_label, test_stat, test_p = "Venkatraman's permutation test (equal ROC curves)", vk["e"], vk["p"]
elif test == "bootstrap":
test_label, test_stat, test_p = "Paired bootstrap test (equal AUCs)", bt["z"], bt["p"]
else:
test_label, test_stat, test_p = "DeLong's paired test (equal AUCs)", res["z"], res["p"]
# %% Assumptions
assumptions = [
{"key": "pairing", "label": "Same subjects for both markers", "status": "info", "detail": f"Both curves use the same {npos + nneg} subjects."},
{"key": "sample_size", "label": "Enough cases and non-cases", "status": "warn" if min(npos, nneg) < 10 else "pass",
"detail": f"{npos} cases and {nneg} non-cases" + ("; the normal approximation is rough." if min(npos, nneg) < 10 else ".")},
]
# %% Result
curves_fpr, curves_tpr, curves_lab = [], [], []
for name, x in ((m1, x1), (m2, x2)):
pts = csr.roc_points(x[is_pos], x[~is_pos])
k = len(pts["tpr"])
keep = np.unique(np.round(np.linspace(0, k - 1, min(k, 1500))).astype(int))
curves_fpr += pts["fpr"][keep].tolist()
curves_tpr += pts["tpr"][keep].tolist()
curves_lab += [name] * len(keep)
# Both scores on every kept row, oriented so higher means the condition (the chart recomputes DeLong's interval from them).
_rc = np.unique(np.round(np.linspace(0, npos + nneg - 1, min(npos + nneg, 5000))).astype(int))
compare_chart = cs.chart("compare", "roc-compare", f"{m1} against {m2}", {
"label": cs.col(f"{r} = {pos_level}", [1 if is_pos[i] else 0 for i in _rc], "numeric"),
"a": cs.col(m1, x1[_rc], "numeric"), "b": cs.col(m2, x2[_rc], "numeric"),
}, controls={"level": conf}, note=None if len(_rc) == npos + nneg else f"Drawn from {len(_rc):,} evenly spaced rows; the tests above use all {npos + nneg:,}.")
cs.publish({
"method": "diagnostic.roc_compare",
"methodVersion": "1.1.2",
"title": "Comparison of two ROC curves",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": npos + nneg,
"summary": f"AUC {res['auc_1']:.3f} ({m1}) vs {res['auc_2']:.3f} ({m2}): difference {res['diff']:.3f}, {conf:.0%} CI [{res['low']:.3f}, {res['high']:.3f}]; "
+ (f"Venkatraman E = {vk['e']:.0f}, {cs.fmt_p(vk['p'])}." if vk else f"bootstrap z = {bt['z']:.2f}, {cs.fmt_p(bt['p'])}." if bt
else f"DeLong z = {res['z']:.2f}, {cs.fmt_p(res['p'])}."),
"apa": f"ΔAUC = {cs.fmt_num(res['diff'], 3)}, {conf:.0%} CI [{cs.fmt_num(res['low'], 3)}, {cs.fmt_num(res['high'], 3)}], z = {cs.fmt_num(res['z'])}, {cs.fmt_p(res['p'])}",
"statistics": [
{"key": "auc_diff", "label": f"AUC difference ({m1} − {m2})", "value": res["diff"]},
{"key": "z", "label": "z (DeLong)", "value": res["z"]}, {"key": "p", "label": "p (DeLong)", "value": res["p"], "format": "p"},
] + ([{"key": "venkatraman_e", "label": "Venkatraman E", "value": vk["e"]}, {"key": "venkatraman_p", "label": "p (Venkatraman)", "value": vk["p"], "format": "p"}] if vk else [])
+ ([{"key": "boot_z", "label": "z (bootstrap)", "value": bt["z"]}, {"key": "boot_p", "label": "p (bootstrap)", "value": bt["p"], "format": "p"}] if bt else []),
"tables": [{
"key": "aucs", "title": "Areas under the curves",
"columns": [{"key": "marker", "label": "Marker", "type": "string"}, {"key": "auc", "label": "AUC", "type": "number"},
{"key": "se", "label": "SE (DeLong)", "type": "number"}],
"rows": [{"marker": m1, "auc": res["auc_1"], "se": res["se_1"]}, {"marker": m2, "auc": res["auc_2"], "se": res["se_2"]}],
}, {
"key": "tests", "title": "Tests", "columns": [{"key": "test", "label": "Test", "type": "string"}, {"key": "statistic", "label": "Statistic", "type": "number"},
{"key": "p", "label": "p", "type": "p"}, {"key": "question", "label": "Question", "type": "string"}],
"rows": [{"test": "DeLong (paired)", "statistic": res["z"], "p": res["p"], "question": "Equal areas under the curves?"}]
+ ([{"test": f"Venkatraman ({reps:,} permutations)", "statistic": vk["e"], "p": vk["p"], "question": "Identical curves at every operating point?"}] if vk else [])
+ ([{"test": f"Paired bootstrap ({reps:,} resamples)", "statistic": bt["z"], "p": bt["p"], "question": "Equal areas under the curves?"}] if bt else []),
}],
"effectSizes": [{"key": "auc_diff", "label": "AUC difference", "value": res["diff"], "ciLow": res["low"], "ciHigh": res["high"], "ciLevel": conf,
"method": "DeLong paired variance"}],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Condition present: {r} = {pos_level}. Correlation between the AUC estimates r = {res['r']:.3f}."]
+ (["Venkatraman's test asks whether the two curves are the same at every threshold, so it can reject when the AUCs are equal but the curves cross. "
f"Its p-value is a permutation p ({reps:,} within-subject swaps, seed {seed}) and matches in Python and R."] if vk else [])
+ ([f"The bootstrap resamples cases and non-cases separately ({reps:,} resamples, seed {seed}); z = ΔAUC / SD of the bootstrap differences."] if bt else []),
"charts": [cs.chart("roc", "roc-curve", "ROC curves", {"fpr": cs.col("1 − specificity", curves_fpr), "tpr": cs.col("Sensitivity", curves_tpr),
"curve": cs.col("marker", curves_lab, levels=[m1, m2])}), compare_chart],
"values": {
"n_pos": npos, "n_neg": nneg, "auc_1": res["auc_1"], "auc_2": res["auc_2"], "se_1": res["se_1"], "se_2": res["se_2"],
"auc_diff": res["diff"], "diff_se": res["se"], "diff_ci_low": res["low"], "diff_ci_high": res["high"], "z": res["z"], "p": res["p"],
"r": res["r"], "positive_level": pos_level, "test": test, "test_label": test_label, "test_statistic": test_stat, "test_p": test_p,
"venkatraman_e": vk["e"] if vk else None, "venkatraman_p": vk["p"] if vk else None,
"boot_sd": bt["sd"] if bt else None, "boot_z": bt["z"] if bt else None, "boot_p": bt["p"] if bt else None, "reps": reps if (vk or bt) else None,
},
})
The bootstrap's spread is 0.064, against DeLong's 0.065. Its result is p = .335, against p = .344. When they disagree, look at the sample size and the ties before trusting either.
Same tests, two dialects
ReadpROC offers all three. Python has no standard DeLong function, and the placement values below are short enough to write yourself. Permutation results differ from the card's, because these tools use their own random numbers.
import numpy as np
import pandas as pd
from scipy import stats
def placements(pos, neg):
h = (pos[:, None] > neg) + 0.5 * (pos[:, None] == neg)
return h.mean(axis=1), h.mean(axis=0)
df = pd.read_csv("biopsy.csv")
malignant = df["class"] == "malignant"
v10, v01 = [], []
for col in ["clump", "cell size"]:
a, b = placements(df.loc[malignant, col].to_numpy(float), df.loc[~malignant, col].to_numpy(float))
v10.append(a)
v01.append(b)
s = np.cov(v10) / malignant.sum() + np.cov(v01) / (~malignant).sum()
diff = v10[0].mean() - v10[1].mean()
z = diff / np.sqrt(s[0, 0] + s[1, 1] - 2 * s[0, 1])
print(diff, z, 2 * stats.norm.sf(abs(z)))library(pROC)
df <- read.csv("biopsy.csv", check.names = FALSE)
r1 <- roc(df$class, df$clump, levels = c("benign", "malignant"), direction = "<")
r2 <- roc(df$class, df[["cell size"]], levels = c("benign", "malignant"), direction = "<")
roc.test(r1, r2, paired = TRUE, method = "delong")
roc.test(r1, r2, paired = TRUE, method = "venkatraman", boot.n = 499)Check your understanding
CheckpointCheck yourself
What is the null hypothesis of DeLong's paired test?
Show the answer
Answer: a
DeLong compares one summary of each curve. Curves can differ a lot and still have equal areas.
Check yourself
Two AUCs have variances 0.0004 and 0.0009, and their covariance is 0.0003. What is the standard error of their difference? Give four decimals.
Show the answer
Answer: 0.0265
Variance of the difference: 0.0004 + 0.0009 − 2 × 0.0003 = 0.0007. Its square root is 0.0265.
Check yourself
Two markers were each measured on different patients. How does DeLong's approach change?
Show the answer
Answer: b
With independent samples there's no shared noise, so the covariance is zero. The app's method compares markers on the same subjects only.
Check yourself
You expect marker A to do better at high sensitivity and marker B at high specificity. Which test fits?
Show the answer
Answer: c
The two AUC tests compare areas, and crossing curves can have equal areas.
Check yourself
How does Venkatraman's paired test build its null distribution?
Show the answer
Answer: b
If the markers perform the same, swapping them within a subject shouldn't matter. Shuffling outcomes would test a different null: that a marker has no relation to the disease.
Check yourself
Venkatraman's test rejects, while DeLong's doesn't. What's a fair next step?
Show the answer
Answer: c
The rejection says the curves differ somewhere. It doesn't say the difference falls where your decision is made.
Recap
Recap- Markers on the same subjects need a paired test: DeLong includes the covariance.
- DeLong and the paired bootstrap test equal areas.
- Venkatraman tests identical curves, and it can see a crossing that areas hide.
- Choose the test before you look, and set each marker's direction yourself.
Concepts in this lesson