Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate accuracy and precision of confusion matrix in R

Is there any tool / R package available to calculate accuracy and precision of confusion matrix in R ?

The formula and data structure are here

like image 590
Ajay Singh Avatar asked Nov 25 '12 03:11

Ajay Singh


People also ask

How do you find the accuracy and precision confusion matrix?

Here are some of the most common performance measures you can use from the confusion matrix. Accuracy: It gives you the overall accuracy of the model, meaning the fraction of the total samples that were correctly classified by the classifier. To calculate accuracy, use the following formula: (TP+TN)/(TP+TN+FP+FN).

How do you calculate confusion matrix in R?

The confusion matrix in R can be calculated by using the “confusionMatrix()” function of the caret library. This function not only calculates the matrix but also returns a detailed report of the matrix.

How do you calculate sensitivity and specificity from a confusion matrix in R?

sensitivity = TP / (TP + FN) specificity = TN / (TN + FP) positive predictive value (PPV) = TP / (TP + FP)


1 Answers

yes, you can calculate Accuracy and precision in R with confusion matrix. It uses Caret package.

Here is the example :

lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
                levels = rev(lvs))
pred <- factor(
               c(
                 rep(lvs, times = c(54, 32)),
                 rep(lvs, times = c(27, 231))),               
               levels = rev(lvs))

xtab <- table(pred, truth)
# load Caret package for computing Confusion matrix
library(caret) 
confusionMatrix(xtab)

And Confusion Matrix for xtab would be like this :

Confusion Matrix and Statistics

          truth
pred       abnormal normal
  abnormal      231     32
  normal         27     54

               Accuracy : 0.8285
                 95% CI : (0.7844, 0.8668)
    No Information Rate : 0.75
    P-Value [Acc > NIR] : 0.0003097

                  Kappa : 0.5336
 Mcnemar's Test P-Value : 0.6025370

            Sensitivity : 0.8953
            Specificity : 0.6279
         Pos Pred Value : 0.8783
         Neg Pred Value : 0.6667
             Prevalence : 0.7500
         Detection Rate : 0.6715
   Detection Prevalence : 0.7645

       'Positive' Class : abnormal

So here is everything, that you want.

like image 86
Nishu Tayal Avatar answered Oct 24 '22 17:10

Nishu Tayal