http://t.csdn.cn/3t5Po
library(pROC)
# label: 金标准,0 1 变量
# pred: 模型预测值,连续变量
# 方法一
cal_metrics <- function(label, pred){
roc.p=pROC::roc(label, pred)
if (roc.p$auc>0.5){
cutoff=roc.p$thresholds[which.max(roc.p$sensitivities+roc.p$specificities)]
sensitivity=roc.p$sensitivities[which.max(roc.p$sensitivities+roc.p$specificities)]
specificity=roc.p$specificities[which.max(roc.p$sensitivities+roc.p$specificities)]
df=data.frame(type='positive classification',
auc=round(roc.p$auc,3),cutoff=cutoff,
sensitivity=sensitivity,specificity=specificity)
return(df)
}
else{
cutoff=roc.p$thresholds[which.min(roc.p$sensitivities+roc.p$specificities)]
sensitivity=roc.p$sensitivities[which.min(roc.p$sensitivities+roc.p$specificities)]
specificity=roc.p$specificities[which.min(roc.p$sensitivities+roc.p$specificities)]
df=data.frame(type='negative classification',
auc=1-round(roc.p$auc,3),cutoff=cutoff,
sensitivity=1-sensitivity,specificity=1-specificity)
return(df)
}
}
# 方法二:
roc.p=pROC::roc(label, pred)
cutoff=roc.p$thresholds[which.max(roc.p$sensitivities+roc.p$specificities)]
pred_b = as.integer(pred > cutoff)
cft = table(pred_b, label)
cm = confusionMatrix(cft, positive = "1", mode = "everything")
paste0('cutoff: ', cutoff)
print(roc.p$auc)
print(cm)