tabdiagi: Immediate diagnostic accuracy from a 2 x 2 table

View source: R/tabdiagi.R

tabdiagiR Documentation

Immediate diagnostic accuracy from a 2 x 2 table

Description

tabdiagi() calculates diagnostic test accuracy directly from the four cell counts of a 2 x 2 table. It is intended especially for teaching, checking hand calculations, and analyses where only aggregated counts are available.

Usage

tabdiagi(
  tp = NULL,
  fp = NULL,
  fn = NULL,
  tn = NULL,
  a = NULL,
  b = NULL,
  c = NULL,
  d = NULL,
  prevalence = NULL,
  ci = FALSE,
  ci_level = 0.95,
  ci_method = c("auto", "wilson", "exact"),
  zero_correction = 0.5,
  digit = 2,
  show = TRUE,
  console = FALSE
)

Arguments

tp

True positives. With positional input this is the first number.

fp

False positives. With positional input this is the second number.

fn

False negatives. With positional input this is the third number.

tn

True negatives. With positional input this is the fourth number.

a, b, c, d

Optional aliases for tp, fp, fn, and tn, respectively. Use either the TP/FP/FN/TN names or the a/b/c/d names, not both.

prevalence

Optional population disease prevalence, strictly between 0 and 1. When supplied, PPV and NPV are recalculated from sensitivity, specificity, and this prevalence. The observed predictive values remain available in the returned object.

ci

FALSE (default), TRUE, or a character vector specifying which confidence intervals to calculate. Examples include ci = c("sens", "spec"), ci = c("ppv", "npv", "lr+", "lr-", "dor"), or ci = TRUE.

ci_level

Confidence level, default 0.95.

ci_method

Method for binomial proportion confidence intervals: "auto" or "wilson" uses Wilson intervals; "exact" uses exact binomial intervals. LR+/LR- and DOR use log-scale intervals.

zero_correction

Continuity correction used only when a zero cell prevents a finite log-scale DOR confidence interval. Default 0.5.

digit

Number of digits displayed for estimates.

show

Logical; open the formatted result in the Viewer. Default TRUE.

console

Logical; also print the teaching-oriented result in the Console. Default FALSE.

Details

The cell layout is:

                        Reference standard
                      Positive     Negative
Test positive            TP           FP
Test negative            FN           TN

The function calculates the full set of commonly used 2 x 2 diagnostic measures: sensitivity, specificity, PPV, NPV, accuracy, balanced accuracy, LR+, LR-, diagnostic odds ratio (DOR), Youden index, F1 score, Matthews correlation coefficient (MCC), Cohen's kappa, false-positive rate (FPR), false-negative rate (FNR), false-discovery rate (FDR), false-omission rate (FOR), disease prevalence, detection rate, and detection prevalence.

Confidence intervals are optional because they can make console output much wider. ci = TRUE requests all currently supported intervals. Sensitivity, specificity, PPV, NPV, and accuracy use binomial intervals. LR+ and LR- use conventional log-scale intervals. DOR uses a log-scale interval; if any cell is zero, the requested zero_correction is applied for the DOR interval calculation and this is recorded in the returned object.

When prevalence is supplied, PPV and NPV in the main results are prevalence-adjusted. Because simple binomial confidence intervals no longer apply to these adjusted predictive values, their CI entries are returned as missing. The observed PPV and NPV remain in result$estimates as ppv_observed and npv_observed.

Value

Invisibly returns an object of class r4vn_diagi and r4vn_stat containing table, estimates, ci, settings, and notes.

Typical teaching use

Use four counts directly:

tabdiagi(80, 20, 10, 90)

or use explicit names:

tabdiagi(tp = 80, fp = 20, fn = 10, tn = 90)

Confidence intervals

Request only sensitivity and specificity confidence intervals:

tabdiagi(80, 20, 10, 90, ci = c("sens", "spec"))

Request all supported confidence intervals:

tabdiagi(80, 20, 10, 90, ci = TRUE)

Predictive values at a target prevalence

To show how PPV and NPV change when disease prevalence is 10 percent:

tabdiagi(80, 20, 10, 90, prevalence = 0.10)

Examples

tabdiagi(80, 20, 10, 90)
tabdiagi(tp = 80, fp = 20, fn = 10, tn = 90)
tabdiagi(80, 20, 10, 90, ci = c("sens", "spec"))
tabdiagi(80, 20, 10, 90, ci = TRUE, show = FALSE)
tabdiagi(80, 20, 10, 90, prevalence = 0.10)


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