1

I have a question concerning the construction of confusion matrix from the below link: Ranger Predicted Class Probability of each row in a data frame

If I have the following code for example (as explained by the answer in the link):

library(ranger)
library(caret)

idx = sample(nrow(iris),100)
data = iris
data$Species = factor(ifelse(data$Species=="versicolor",1,0))
Train_Set = data[idx,]
Test_Set = data[-idx,]

mdl <- ranger(Species ~ ., ,data=Train_Set,importance="impurity", save.memory = TRUE, probability=TRUE)
probabilities <- as.data.frame(predict(mdl, data = Test_Set,type='response', verbose = TRUE)$predictions)
max.col(probabilities) - 1

Invoking

confusionMatrix(table(Test_Set$Species, max.col(probabilities)-1))

yields: enter image description here

And, using this

caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species))

gives enter image description here

Which is the right way to create confusion matrix, since the values of sensitivity, specificity, ppv, npv differs becuase tp, tn, fp, fn switches?

If I demand the positive class to be 1 rather using

caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')

I getenter image description here

So, the values in the matrices are tp = 13, tn = 36, fp = 0, fn = 1, correct?

I am confused as to how to read the values of the confusion matrix.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Ray
  • 321
  • 2
  • 12
  • From `?confusionMatrix` it looks like that the first argument (the rows) should be the predictions and the columns the true values. – nicola Apr 29 '20 at 12:38
  • I understood that caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species)) is the right way as proposed by the answer in the link: https://stackoverflow.com/questions/60948668/ranger-predicted-class-probability-of-each-row-in-a-data-frame – Ray Apr 29 '20 at 12:45

1 Answers1

1

I have understood the construction of confusion matrices and the role of the entries if the class is changed.

The confusion matrices for the class 0 obtained using

caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '0')

and that of class 1 obtained using

caret::confusionMatrix(table(max.col(probabilities) - 1,Test_Set$Species), positive = '1')

are the same, and

enter image description here

In case of class 0: tp = 36, tn = 13, fp = 1, fn = 0, and in case of class 1: tp = 13, tn = 36, fp = 0, fn = 1 (the roles of tp and tn, and that of fp and fn are switched).

Ray
  • 321
  • 2
  • 12