Computes a 2×2 confusion matrix and comprehensive diagnostic performance metrics for a binary classification test, with exact binomial confidence intervals.
Arguments
- data
A data.frame containing
testandrefvariables.- test
Unquoted name of the diagnostic test variable (must be binary).
- ref
Unquoted name of the reference standard variable (must be binary).
- positive
Character or numeric. Level representing "Positive" in the reference variable. If
NULL(default), auto-detected from common positive labels ("Yes","1","Positive", etc.) or the last level.- test_positive
Character or numeric. Level representing "Positive" in the test variable. If
NULL(default), mirrorspositivewhen the same label exists in the test variable, then falls back to auto-detection.- conf.level
Numeric. Confidence level for binomial CIs (0–1). Default:
0.95.
Value
An object of class diag_test — a named list with:
$table: 2×2tableobject (Test × Ref).$stats:data.framewith columnsMetric,Estimate,LowerCI,UpperCI.$labels: named list withref_pos,ref_neg,test_pos,test_neg.$sample_size: integer, total valid observations.$conf.level: numeric, confidence level used.
Details
Metrics Computed
Sensitivity (Recall) = TP / (TP + FN)
Specificity = TN / (TN + FP)
PPV (Precision) = TP / (TP + FP)
NPV = TN / (TN + FN)
Accuracy = (TP + TN) / Total
Prevalence = (TP + FN) / Total
Likelihood Ratio + = Sensitivity / (1 – Specificity)
Likelihood Ratio – = (1 – Sensitivity) / Specificity
Youden's Index = Sensitivity + Specificity – 1
F1 Score = 2 × (PPV × Sensitivity) / (PPV + Sensitivity)
Binomial CIs (exact Clopper–Pearson) are computed for the first six metrics. Likelihood Ratios, Youden's Index, and F1 Score do not have CIs.
Examples
set.seed(1)
n <- 200
ref <- factor(sample(c("No", "Yes"), n, replace = TRUE, prob = c(.55, .45)))
tst <- ifelse(ref == "Yes",
ifelse(runif(n) < .80, "Yes", "No"),
ifelse(runif(n) < .85, "No", "Yes"))
df <- data.frame(rapid_test = factor(tst), lab = ref)
result <- diag_test(df, test = rapid_test, ref = lab,
positive = "Yes", test_positive = "Yes")
print(result)
#>
#> ============================================================
#> DIAGNOSTIC TEST EVALUATION
#> ============================================================
#>
#> Sample size : 200
#> Confidence level : 95%
#>
#> Reference standard (gold standard):
#> Positive = 'Yes' | Negative = 'No'
#>
#> Diagnostic test:
#> Positive = 'Yes' | Negative = 'No'
#>
#> ------------------------------------------------------------
#> Confusion Matrix
#> ------------------------------------------------------------
#> Ref
#> Test Yes No
#> Yes 79 21
#> No 14 86
#>
#> ============================================================
#> Performance Metrics (95% CI)
#> ============================================================
#> Sensitivity : 0.849 (0.760 – 0.915)
#> Specificity : 0.804 (0.716 – 0.874)
#> Pos Pred Value (PPV) : 0.790 (0.697 – 0.865)
#> Neg Pred Value (NPV) : 0.860 (0.776 – 0.921)
#> Accuracy : 0.825 (0.765 – 0.875)
#> Prevalence : 0.465 (0.394 – 0.537)
#> ------------------------------------------------------------
#> Likelihood Ratio + : 4.328
#> Likelihood Ratio - : 0.187
#> Youden Index : 0.653
#> F1 Score : 0.819
#>
as.data.frame(result)
#> Metric Estimate LowerCI UpperCI
#> 1 Sensitivity 0.8494624 0.7603395 0.9151838
#> 2 Specificity 0.8037383 0.7157763 0.8742127
#> 3 Pos Pred Value (PPV) 0.7900000 0.6970846 0.8650563
#> 4 Neg Pred Value (NPV) 0.8600000 0.7762720 0.9212946
#> 5 Accuracy 0.8250000 0.7651160 0.8749756
#> 6 Prevalence 0.4650000 0.3943527 0.5367001
#> 7 Likelihood Ratio + 4.3282130 NA NA
#> 8 Likelihood Ratio - 0.1872968 NA NA
#> 9 Youden Index 0.6532007 NA NA
#> 10 F1 Score 0.8186528 NA NA