下午好,
假设我们有以下内容:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")和以下函数:
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1))
print(recall(as.factor(predicted1), as.factor(observed1))) # i can print recall 0.9285714
return(list(xtab,result))
}运行后,F1、召回率和精确度等指标没有显示出来:
metrics(obs,pred)
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1 pb是一些准确性指标没有显示出来,这看起来很奇怪: F1,召回,精度!
我注意到了另一个问题,例如,如果我们颠倒顺序:
metrics(pred,obs)
Error in confusionMatrix.default(as.factor(predicted1), as.factor(observed1)) :
The data must contain some levels that overlap the reference.
Calls: metrics -> confusionMatrix -> confusionMatrix.default
Execution halted我希望我的问题是清楚的。
感谢您的帮助!
发布于 2021-06-11 21:53:05
我找到了一个解决方案。confusionMatrix()有一个名为mode='everything'的选项,可以输出所有已实现的度量:
pred=c(2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1)
obs=structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("no-recurrence-events",
"recurrence-events"), class = "factor")
library(plyr)
library(caret)
metrics<-function(observed1,predicted1){
#predicted1=mapvalues(predicted1, from = unique(predicted1), to = unique(observed1))
observed1=mapvalues(observed1, from = unique(observed1), to = unique(predicted1) )
#xtab=table(observed1, predicted1)
xtab=table(predicted1,observed1)
#result<- confusionMatrix(as.factor(predicted1), as.factor(observed1))
result<- confusionMatrix(as.factor(predicted1),as.factor(observed1),mode='everything')
return(list(xtab,result))
}
metrics(obs,pred)这给出了预期的输出:
Loading required package: lattice
Loading required package: ggplot2
[[1]]
observed1
predicted1 1 2
1 13 4
2 1 2
[[2]]
Confusion Matrix and Statistics
Reference
Prediction 1 2
1 13 4
2 1 2
Accuracy : 0.75
95% CI : (0.509, 0.9134)
No Information Rate : 0.7
P-Value [Acc > NIR] : 0.4164
Kappa : 0.3056
Mcnemar's Test P-Value : 0.3711
Sensitivity : 0.9286
Specificity : 0.3333
Pos Pred Value : 0.7647
Neg Pred Value : 0.6667
Precision : 0.7647
Recall : 0.9286
F1 : 0.8387
Prevalence : 0.7000
Detection Rate : 0.6500
Detection Prevalence : 0.8500
Balanced Accuracy : 0.6310
'Positive' Class : 1 https://stackoverflow.com/questions/67936497
复制相似问题