14

In R caret library, if I got a confusion matrix like this below, if there a way to retrieve the overall accuracy 0.992? I can't get this single value out, since I need to store this value and use it for later processing. Is this possible at all?

 Prediction    A    B    C    D    E
          A 1114    2    0    0    0
          B    9  745    5    0    0
          C    0    6  674    4    0
          D    0    0    3  640    0
          E    0    0    2    1  718

Overall Statistics

            Accuracy : 0.992         
              95% CI : (0.989, 0.994)
 No Information Rate : 0.286         
 P-Value [Acc > NIR] : <2e-16        

               Kappa : 0.99          

Mcnemar's Test P-Value : NA

Statistics by Class:

                     Class: A Class: B Class: C Class: D Class: E
 Sensitivity             0.992    0.989    0.985    0.992    1.000
 Specificity             0.999    0.996    0.997    0.999    0.999
 Pos Pred Value          0.998    0.982    0.985    0.995    0.996
 Neg Pred Value          0.997    0.997    0.997    0.998    1.000
 Prevalence              0.286    0.192    0.174    0.164    0.183
 Detection Rate          0.284    0.190    0.172    0.163    0.183
 Detection Prevalence    0.284    0.193    0.174    0.164    0.184
 Balanced Accuracy       0.996    0.992    0.991    0.996    1.000
Cœur
  • 37,241
  • 25
  • 195
  • 267
user697911
  • 10,043
  • 25
  • 95
  • 169

1 Answers1

33

Given a confusion matrix cm, the overall accuracy is obtained by overall.accuracy <- cm$overall['Accuracy']

It's the first time I see the caret package, so how did I know this?

Since you didn't provide an example, I searched for an example code for caret confusion matrices. Here it is (I only added assignment in the last statement):

###################
## 3 class example

confusionMatrix(iris$Species, sample(iris$Species))

newPrior <- c(.05, .8, .15)
names(newPrior) <- levels(iris$Species)

cm <- confusionMatrix(iris$Species, sample(iris$Species))

Now, let's take a look what's in the confusion matrix:

> str(cm)
List of 5
 $ positive: NULL
 $ table   : 'table' int [1:3, 1:3] 13 18 19 20 13 17 17 19 14
  ..- attr(*, "dimnames")=List of 2
  .. ..$ Prediction: chr [1:3] "setosa" "versicolor" "virginica"
  .. ..$ Reference : chr [1:3] "setosa" "versicolor" "virginica"
 $ overall : Named num [1:7] 0.267 -0.1 0.198 0.345 0.333 ...
  ..- attr(*, "names")= chr [1:7] "Accuracy" "Kappa" "AccuracyLower" "AccuracyUpper" ...
 $ byClass : num [1:3, 1:8] 0.26 0.26 0.28 0.63 0.63 0.64 0.26 0.26 0.28 0.63 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "Class: setosa" "Class: versicolor" "Class: virginica"
  .. ..$ : chr [1:8] "Sensitivity" "Specificity" "Pos Pred Value" "Neg Pred Value" ...
 $ dots    : list()
 - attr(*, "class")= chr "confusionMatrix"

As you may see, the cm object is a list. We see various "byClass" and "overall" statistics. The overall part is obtained by:

overall <- cm$overall

Which gives us a vector of numbers with string indices:

> overall
      Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue  McnemarPValue 
     0.2666667     -0.1000000      0.1978421      0.3449492      0.3333333      0.9674672      0.9547790 

Now, extracting the relevant value is as simple as:

> overall.accuracy <- overall['Accuracy'] 

Summary: str is your friend. Another useful function is attributes -- it returns all the attributes of a given object.

Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
  • 1
    Nice answer but accuracy return a string and a value, how can I just access the value? I mean the double – Emixam23 Mar 20 '17 at 21:37
  • 2
    @Emixam23 you might have figured out by now the solution for your question, but for someone who is looking for answer, just add another square brackets in front and back of the brackets mentioned above, like this `overall.accuracy <- overall[['Accuracy']]` – Saahil Aug 09 '19 at 14:24