4.3 Sensitivity and Specificity

To demonstrate sensitivity, specificity, positive predictive value (PPV), and negative predictive value (NPV) calculations, we look at a classic, if sobering, example of HIV misdiagnoses.

Here T- and T+ mean that the HIV test came back negative and positive, respectively, and H- and H+ mean that HIV is not present and present, respectively.

hiv <- matrix(c(9801, 99, 1, 99), ncol = 2, byrow = TRUE)
colnames(hiv) <- c("T-", "T+")
rownames(hiv) <- c("H-", "H+")
hiv
##      T- T+
## H- 9801 99
## H+    1 99

We can calculate the marginals:

margin1 <- margin.table(hiv, margin = 1)
margin2 <- margin.table(hiv, margin = 2)

We can calculate the total number of cases of HIV, non-cases of HIV, positive results, negative results, true positive results, false positive results, true negative results, and false negative results,

hivpos <- margin1[2]
hivneg <- margin1[1]
testpos <- margin2[2]
testneg <- margin2[1]
truepos <- hiv[2, 2]
falsepos <- hiv[1, 2]
trueneg <- hiv[1, 1]
falseneg <- hiv[2, 1]

Then we can easily calculate the senstivitiy, specificity, PPV and NPV.

Then the sensitivity is,

truepos/hivpos
##   H+ 
## 0.99

which tells us that, given that the subject has HIV, the test correctly detects it 99% of the time.

The specificity is,

trueneg/hivneg
##   H- 
## 0.99

which tells us that, given that the subject does not have HIV, the test correctly returns negative 99% of the time.

The PPV is,

truepos/testpos
##  T+ 
## 0.5

which tells us that, given a positive test result, the test will be correct 50% of the time. In this case, it is worryingly low. One potential solution is upon a positive result with low PPV is to retest, taking advantage of the high sensitivity and specificity.

Finally, the NPV is,

trueneg/testneg
##       T- 
## 0.999898

which tells us that, given a negative test result, the test will be correct 99.989798% of the time. This is a very high probability, so if a test subject receives a negative result, then they are very, very likely not to have HIV.