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

多模型ROC曲线绘图中曲线重叠的分离方法求助

解决ROC曲线重叠的代码调整方法

以下是几种在代码层面分离重叠ROC曲线的可行方案:

1. 调整线条样式(虚实/粗细组合)

通过给重叠的曲线设置不同的线型(lty参数),即使颜色相近也能直观区分。修改对应的plot.roc调用和图例:

par(pty="s")
# 绘制基础曲线
roc(na.exclude(DATA$Outcome), glmVari1$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="maroon1", lwd=2)
# 依次添加其他曲线
plot.roc(na.exclude(DATA$Outcome), glmVari2$fitted.values, percent=TRUE, col="cyan", lwd=2, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari3$fitted.values, percent=TRUE, col="skyblue3", lwd=2, print.auc=FALSE, add=TRUE)
# Variable4用实线,Variable6用虚线区分
plot.roc(na.exclude(DATA$Outcome), glmVari4$fitted.values, percent=TRUE, col="seagreen4", lwd=2, lty=1, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari5$fitted.values, percent=TRUE, col="green", lwd=2, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari6$fitted.values, percent=TRUE, col="orangered3", lwd=2, lty=2, print.auc=FALSE, add=TRUE)
# 图例添加对应线型参数
legend("bottomright", c("Variable 1", "Variable 2", "Variable 3 ", "Variable 4", "Variable 5", "Variable 6"), 
       col = c("maroon1", "cyan", "skyblue3", "seagreen4", "green", "orangered3"), 
       lwd=2, lty=c(1,1,1,1,1,2), cex = 0.62)

2. 轻微偏移预测值(仅可视化调整)

给其中一条曲线的预测值添加微小缩放/偏移,让曲线轻微移位——注意这只是为了可视化分离,不改变模型本身的性能:

par(pty="s")
roc(na.exclude(DATA$Outcome), glmVari1$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="maroon1", lwd=2)
plot.roc(na.exclude(DATA$Outcome), glmVari2$fitted.values, percent=TRUE, col="cyan", lwd=2, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari3$fitted.values, percent=TRUE, col="skyblue3", lwd=2, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari4$fitted.values, percent=TRUE, col="seagreen4", lwd=2, print.auc=FALSE, add=TRUE)
plot.roc(na.exclude(DATA$Outcome), glmVari5$fitted.values, percent=TRUE, col="green", lwd=2, print.auc=FALSE, add=TRUE)
# 给Variable6的预测值乘以1.01(微小缩放,可根据实际情况调整系数)
plot.roc(na.exclude(DATA$Outcome), glmVari6$fitted.values * 1.01, percent=TRUE, col="orangered3", lwd=2, print.auc=FALSE, add=TRUE)
legend("bottomright", c("Variable 1", "Variable 2", "Variable 3 ", "Variable 4", "Variable 5", "Variable 6"), 
       col = c("maroon1", "cyan", "skyblue3", "seagreen4", "green", "orangered3"), lwd=2, cex = 0.62)

3. 在曲线上直接标注变量名/数值

在曲线的关键位置(比如特异度70%-80%区间)添加文本标注,直接对应曲线和变量:

par(pty="s")
# 先绘制所有曲线并保存ROC对象
roc_obj1 <- roc(na.exclude(DATA$Outcome), glmVari1$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="maroon1", lwd=2)
roc_obj2 <- plot.roc(na.exclude(DATA$Outcome), glmVari2$fitted.values, percent=TRUE, col="cyan", lwd=2, print.auc=FALSE, add=TRUE)
roc_obj3 <- plot.roc(na.exclude(DATA$Outcome), glmVari3$fitted.values, percent=TRUE, col="skyblue3", lwd=2, print.auc=FALSE, add=TRUE)
roc_obj4 <- plot.roc(na.exclude(DATA$Outcome), glmVari4$fitted.values, percent=TRUE, col="seagreen4", lwd=2, print.auc=FALSE, add=TRUE)
roc_obj5 <- plot.roc(na.exclude(DATA$Outcome), glmVari5$fitted.values, percent=TRUE, col="green", lwd=2, print.auc=FALSE, add=TRUE)
roc_obj6 <- plot.roc(na.exclude(DATA$Outcome), glmVari6$fitted.values, percent=TRUE, col="orangered3", lwd=2, print.auc=FALSE, add=TRUE)
# 在特异度80%的位置标注变量名,给Var6的y值加3避免重叠
text(x=80, y=roc_obj4$sensitivities[which.min(abs(roc_obj4$specificities - 80))], "Var4", col="seagreen4", cex=0.7)
text(x=80, y=roc_obj6$sensitivities[which.min(abs(roc_obj6$specificities - 80))] + 3, "Var6", col="orangered3", cex=0.7)
legend("bottomright", c("Variable 1", "Variable 2", "Variable 3 ", "Variable 4", "Variable 5", "Variable 6"), 
       col = c("maroon1", "cyan", "skyblue3", "seagreen4", "green", "orangered3"), lwd=2, cex = 0.62)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 13:55:28