Solved – Confusion matrix and ROC curves

confusion matrixr

I have built a model to predict Upsell probability. When I use the function confusionMatirx from caret package, I get the following results:

> confusionMatrix(data = predict_svm_test_5, test_td$UpSell_Ind)
Confusion Matrix and Statistics

             Reference
Prediction    0    1
          0 7976 2886
          1  217  644

           Accuracy : 0.7353          
             95% CI : (0.7272, 0.7433)
No Information Rate : 0.6989          
P-Value [Acc > NIR] : < 2.2e-16       

              Kappa : 0.1987          
Mcnemar's Test P-Value : < 2.2e-16       

        Sensitivity : 0.9735          
        Specificity : 0.1824          
     Pos Pred Value : 0.7343          
     Neg Pred Value : 0.7480          
         Prevalence : 0.6989          
     Detection Rate : 0.6804          
Detection Prevalence : 0.9266          
  Balanced Accuracy : 0.5780          

   'Positive' Class : 0  

However, I expected to see the confusion matrix as follows:

             Reference
Prediction   1      0
      1     644    217
      0    2886   7976
Specificity(TPR): 0.9735
Sensitivity(TNR): 0.1824

1 meaning there was an Upsell (Event) and 0 meaning no Upsell (No Event) based on the PDF of Caret Package. Link is here Page 24, 25

Now my question: How do I interpret the results of confusionMatrix? The values given by the function are different from values that I calculate.

Thanks in advance for the help.

Best Answer

Thanks @charles for pointing me to "positive". Though positive = 1 did not work as the argument positive takes only character value in the function. But I was able to get what I wanted using the following:

levels(test_td$UpSell_Ind)

[1] "0" "1"
confusionMatrix(data = predict_glm_vif_test, test_td$UpSell_Ind, positive = levels(test_td$UpSell_Ind)[2])

Confusion Matrix and Statistics

          Reference
Prediction    0    1
         0 8104 3241
         1   89  289

           Accuracy : 0.7159          
             95% CI : (0.7077, 0.7241)
No Information Rate : 0.6989          
P-Value [Acc > NIR] : 2.701e-05       

              Kappa : 0.0952          
 Mcnemar's Test P-Value : < 2.2e-16       

        Sensitivity : 0.08187         
        Specificity : 0.98914         
     Pos Pred Value : 0.76455         
     Neg Pred Value : 0.71432         
         Prevalence : 0.30112         
     Detection Rate : 0.02465         
 Detection Prevalence : 0.03224         
  Balanced Accuracy : 0.53550         

   'Positive' Class : 1  
Related Question