Learn R Programming

SimtablR (version 1.2.0)

diag_test: Diagnostic Test Accuracy Assessment

Description

Computes a 2x2 confusion matrix and comprehensive diagnostic performance metrics for a binary classification test, with exact binomial confidence intervals.

Usage

diag_test(
  data,
  test,
  ref,
  positive = NULL,
  test_positive = NULL,
  conf.level = 0.95
)

Value

An object of class diag_test - a named list with:

  • $table: 2x2 table object (Test x Ref).

  • $stats: data.frame with columns Metric, Estimate, LowerCI, UpperCI.

  • $labels: named list with ref_pos, ref_neg, test_pos, test_neg.

  • $sample_size: integer, total valid observations.

  • $conf.level: numeric, confidence level used.

Arguments

data

A data.frame containing test and ref variables.

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), mirrors positive when 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.

Details

Confusion Matrix Layout

           | Ref +   | Ref -
-----------+---------+--------
Test +     |   TP    |   FP
Test -     |   FN    |   TN

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 x (PPV x 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.

See Also

print.diag_test(), as.data.frame.diag_test(), plot.diag_test()

Examples

Run this code
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)
as.data.frame(result)

Run the code above in your browser using DataLab