R语言 计算AUC及其95置信区间的代码
首先,需要导入pROC包,并使用roc函数计算ROC曲线和AUC值。
library(pROC)
# 构造真实标签和预测概率
labels <- c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0)
pred <- c(0.2, 0.5, 0.6, 0.3, 0.8, 0.1, 0.9, 0.4, 0.7, 0.2)
# 计算ROC曲线和AUC值
roc_obj <- roc(labels, pred)
auc <- auc(roc_obj)
接下来,可以使用ci函数计算AUC的置信区间。
# 计算AUC的95%置信区间
ci_obj <- ci.auc(roc_obj, conf.level = 0.95)
lower_ci <- ci_obj$ci[1]
upper_ci <- ci_obj$ci[2]
最后,可以将AUC值和置信区间打印出来。
# 打印AUC值和置信区间
cat(sprintf("AUC = %.3f (95%% CI %.3f-%.3f)", auc, lower_ci, upper_ci))
原文地址: http://www.cveoy.top/t/topic/bkhD 著作权归作者所有。请勿转载和采集!