When I calculate the measures with the Rose library I get measures for recall, precision and F1. The recall and precision measures differ however when I calculate them manually. How come?
install.packages("ROSE")
library(ROSE)
library(rpart)
s = sample(957,200)
training = data[-s,]
test = data[s,]
### Rose
treeimb <- rpart(Riskdrinker ~ ., data = training)
pred.treeimb <- predict(treeimb, newdata = test)
accuracy.meas(test$Riskdrinker, pred.treeimb[,2])
Output
Call: accuracy.meas(response = test$Riskdrinker, predicted = pred.treeimb[, 2])
Examples are labelled as positive when predicted is greater than 0.5
precision: 0.919 recall: 0.943 F: 0.465
However when I calculate this measures like this I get other results for precision and recall.
treeimb <- rpart(Riskdrinker ~ ., data = training)
pred.treeimb <- predict(treeimb, newdata = test)
pred <- predict(treeimb, newdata = test, type="class")
confMat <- table(test$Riskdrinker, pred)
#Precision
message("Precision: ", specify_decimal(confMat[1,1] / (confMat[1,1] + confMat[2,1])*100, 1), " %")
#Recall
message("Recall: ", specify_decimal(confMat[1] / (confMat[1] + confMat[1,2])*100, 1), " %")
#Accuracy
message("Accuracy: ", specify_decimal((confMat[1]+confMat[2,2]) / (confMat[1] + confMat[1,2] + confMat[2,1] + confMat[2,2])*100, 1), " %")
Or like this. Same same.
accuracy <- sum(diag(confMat))/sum(confMat)
This results in:
- Precision: 76.9 %
- Recall: 69.8 %
- Accuracy: 89.0 %
The main difference from the codes are that I use type="class" in one of the cases, but what makes that for difference? Can you get a matrix from Rose aswell? I would say that this is a reproducible example unless I give away my dataset ofc.