Search code examples
rprecisionpredictionr-caretconfusion-matrix

Creating a Confusion Matrix using caret


I want to create a confusion matrix and need to extract Precision as a vector with the following sample:

true_values <- sample(c("Dog", "Cat"), 10000, replace = TRUE,
prob = c(0.3, 0.7))
predicted_values <- true_values
predicted_values[sample.int(10000, 4000)] <-
sample(c("Dog", "Cat"), 4000, replace = TRUE, prob = c(0.3, 0.7))


Solution

  • You can do it like this, assuming positive class is Dog :

    cm = confusionMatrix(table(predicted_values,true_values),
    positive="Dog",mode="prec_recall")
    
    cm$byClass["Precision"]
    Precision 
    0.7264901