我创建了一个函数来根据混淆矩阵计算灵敏度和特异性,后来才发现该caret包有一个confusionMatrix(). 当我尝试时,事情变得非常混乱,因为它似乎caret使用了错误的公式?
示例数据:
dat <- data.frame(real = as.factor(c(1,1,1,0,0,1,1,1,1)),
pred = as.factor(c(1,1,0,1,0,1,1,1,0)))
cm <- table(dat$real, dat$pred)
cm
0 1
0 1 1
1 2 5
Run Code Online (Sandbox Code Playgroud)
我的功能:
model_metrics <- function(cm){
acc <- (cm[1] + cm[4]) / sum(cm[1:4])
# accuracy = ratio of the correctly labeled subjects to the whole pool of subjects = (TP+TN)/(TP+FP+FN+TN)
sens <- cm[4] / (cm[4] + cm[3])
# sensitivity/recall = ratio of the correctly +ve labeled to all who are +ve in …Run Code Online (Sandbox Code Playgroud)