#confusionMatrix with caret
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
###################
## 2 class example using the example given in 
## https://en.wikipedia.org/wiki/Sensitivity_and_specificity

lvs <- c("positive", "negative")
truth <- factor(rep(lvs, times = c(30, 2000)),
                levels = rev(lvs))
pred <- factor(
  c(
    rep(lvs, times = c(20, 10)),
    rep(lvs, times = c(180, 1820))),               
  levels = rev(lvs))

xtab <- table(pred, truth)
print(confusionMatrix(xtab[2:1,2:1]))
## Confusion Matrix and Statistics
## 
##           truth
## pred       positive negative
##   positive       20      180
##   negative       10     1820
##                                           
##                Accuracy : 0.9064          
##                  95% CI : (0.8929, 0.9187)
##     No Information Rate : 0.9852          
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0.1521          
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.666667        
##             Specificity : 0.910000        
##          Pos Pred Value : 0.100000        
##          Neg Pred Value : 0.994536        
##              Prevalence : 0.014778        
##          Detection Rate : 0.009852        
##    Detection Prevalence : 0.098522        
##       Balanced Accuracy : 0.788333        
##                                           
##        'Positive' Class : positive        
##