You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言使用pROC包做ROC分析时如何计算对应阈值的实验室检测值

解决方法

你当前代码未返回实验室检测阈值的原因是ci.coords的ret参数没有指定输出阈值项,调整参数即可得到所需结果,完整操作步骤如下:

步骤1:修正核心分析代码

在ret参数中加入threshold项,即可同步返回对应灵敏度下的检测阈值:

library(pROC)
spred1 = predict(smodel1)
sroc1 = roc(EditedDF1$any_abnormality, spred1)
# 新增threshold到返回项中
result <- ci.coords(sroc1, x=0.95, input="sensitivity", transpose = FALSE, ret=c("threshold","sensitivity","specificity","ppv","npv"))

注意:如果你的检测数值越高对应患病概率越低,可在roc()函数中加入direction = "<"参数调整阈值判定方向,避免结果颠倒。

步骤2:生成目标格式表格

  • 仅输出点估计值的表格:
# 提取点估计结果并重命名列
table_result <- as.data.frame(t(result$mean))
colnames(table_result) <- c("Lab value", "Sens", "Spec", "PPV", "NPV")
# 控制台打印结果
print(table_result)
# 如需导出为本地csv文件可执行以下代码
# write.csv(table_result, "roc_analysis_result.csv", row.names = FALSE)
  • 同时输出指标95%置信区间的表格:
table_ci <- data.frame(
  `Lab value` = paste0(round(result$mean['threshold'],3), " (", round(result$ci.low['threshold'],3), "-", round(result$ci.high['threshold'],3), ")"),
  `Sens` = paste0(round(result$mean['sensitivity'],3), " (", round(result$ci.low['sensitivity'],3), "-", round(result$ci.high['sensitivity'],3), ")"),
  `Spec` = paste0(round(result$mean['specificity'],3), " (", round(result$ci.low['specificity'],3), "-", round(result$ci.high['specificity'],3), ")"),
  `PPV` = paste0(round(result$mean['ppv'],3), " (", round(result$ci.low['ppv'],3), "-", round(result$ci.high['ppv'],3), ")"),
  `NPV` = paste0(round(result$mean['npv'],3), " (", round(result$ci.low['npv'],3), "-", round(result$ci.high['npv'],3), ")"),
  check.names = FALSE
)
print(table_ci)

内容的提问来源于stack exchange,提问作者DW1310

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 04:48:04