Learn R Programming

qwraps2 (version 0.1.2)

sensitivity: Sensitivity, Specificity, and Confusion Matrices (Contingency Tables)

Description

Functions for calculating the sensitivity and specificity, along with bootstrapped confidence intervals, of confusion matrices (contingency tables).

Usage

sensitivity(tab, ...)

specificity(tab, ...)

confusion_matrix(formula, data, boot = FALSE, boot_samples = 1000L, alpha = 0.05)

Arguments

tab
A 2-by-2 confusion_matrix matrix or ftable
...
not currently used
formula
column ~ row for building the confusion matrix
data
environment containing the variables listed in the formula
boot
boolean, should bootstrapped confidence intervals for the sensitivity and specificity be computed? Defaults to FALSE.
boot_samples
number of bootstrapping sample to generate, defaults to 1000L. Ignored if boot == FALSE.
alpha
100(1-alpha)sensitivity. Ignored if boot == FALSE.

Value

  • The sensitivity and specificity functions return numeric values. confusion_matrix returns a list with elements:
    • tab the confusion matrix,
    • specificity point estimate for specificity,
    • sensitivity point estimate for sensitivity,
    • specificity_ci bootstrapped confidence interval for specificity, and
    • sensitivity_ci bootstrapped confidence interval for sensitivity.

Details

Sensitivity and Specificity: For the sensitivity and specificity function we expect the 2-by-2 confusion matrix (contingency table) to be of the form:

lccc{ True Condition - + Predicted Condition - TN FN Predicted Condition + FP TP } where

  • TN: True Negative,
  • FP: False Positive,
  • FN: False Negative, and
  • TP: True Positive.

sensitivity = TP / (TP + FN) specificity = TN / (TN + FP)

This table set up is the result of using 0/1 or boolean variables. See examples.

Examples

Run this code
## Does knowing if a diamond is more than 1.5 carats in weight tell us if the
## price is more than $5,000?

data("diamonds", package = "ggplot2")

x <- ftable(I(price > 5000) ~ I(carat > 1.5), data = diamonds)

sensitivity(x)
specificity(x)

sensitivity(I(price > 5000) ~ I(carat > 1.5), data = diamonds)
specificity(I(price > 5000) ~ I(carat > 1.5), data = diamonds)

confusion_matrix(I(price > 5000) ~ I(carat > 1.5), data = diamonds)
print(confusion_matrix(I(price > 5000) ~ I(carat > 1.5), data = diamonds), digits = 4)

x <- confusion_matrix(I(price > 5000) ~ I(carat > 1.5),
                      data = diamonds,
                      boot = TRUE,
                      boot_samples = 100L)
print(x, digits = 4)

Run the code above in your browser using DataLab