在ROC曲线上添加pch并标记AUC值

时间:2020-02-03 11:12:49

标签: r legend roc auc

我想使用R的pROC应用程序绘制5个估计模型的ROC曲线。因为我使用的是灰度级,在视觉上看起来似乎无法区分,所以我想通过为不同的ROC曲线分配不同的pch来强调不同的线型,但是,我发现lines()函数对pch个选项。

此外,当我想使用print.auc选项标记它们各自的AUC值时,除了默认模型(模型1)之外,roc()lines()函数似乎确实起作用。仅显示第一个模型的AUC值。

如果有人可以指出一些可行的方法来达到预期的效果,将不胜感激。以下是我当前的绘图和输出代码。

### load the data ###
library(readxl)

dataURL <- "https://www.dropbox.com/s/vri9fx2xa1pfj7w/predict.xlsx?dl=1"
temp = tempfile(fileext = ".xlsx")
download.file(dataURL, destfile=temp, mode='wb')
predict <- readxl::read_excel(temp, sheet =1)

### plotting and labelling ###

library(pROC)

roc.pred2 <- roc(predict$IAC, predict$phat2, percent = TRUE, main = "Smoothing")
roc.pred3 <- roc(predict$IAC, predict$phat3, percent = TRUE, main = "Smoothing")
roc.pred4 <- roc(predict$IAC, predict$phat4, percent = TRUE, main = "Smoothing")
roc.pred5 <- roc(predict$IAC, predict$phat5, percent = TRUE, main = "Smoothing")

plot.roc(predict$IAC, predict$phat1, percent = TRUE, main = "ROC curves", add =  FALSE, asp = NA, print.auc = TRUE)

lines(roc.pred2, type = "l", lty = 2, col = "grey35")
lines(roc.pred3, type = "l", lty = 3, col = "grey48")
lines(roc.pred4, type = "l", lty = 4, col = "grey61")
lines(roc.pred5, type = "l", lty = 1, pch = 24, col = "grey76")

legend("bottomright", 
       legend = c("Model 1", "Model 2", "Model 3", "Model 4", "Model 5"), 
       col = c("black", "grey35", "grey48", "grey61", "grey76"),
       lty = c(1, 2, 3, 4, 1))

Output

1 个答案:

答案 0 :(得分:2)

除了颜色,您还可以在一行上更改两件事:类型(lty)和宽度(lwd)。

plot.roc(predict$IAC, predict$phat1, percent = TRUE, main = "ROC curves", add =  FALSE, asp = NA, print.auc = TRUE)

lines(roc.pred2, type = "l", lty = 2, col = "grey35")
lines(roc.pred3, type = "l", lty = 3, lwd = 4, col = "grey48")
lines(roc.pred4, type = "l", lty = 4, lwd = 8, col = "grey61")
lines(roc.pred5, type = "l", lty = 1, pch = 24, col = "grey76")

legend("bottomright", 
       legend = c("Model 1", "Model 2", "Model 3", "Model 4", "Model 5"), 
       col = c("black", "grey35", "grey48", "grey61", "grey76"),
       lty = c(1, 2, 3, 4, 1),
       lwd = c(1, 1, 4, 8, 1))

enter image description here

除此之外,您将达到在黑白情节中可以显示的内容的极限。

如果ROC曲线上的点较少,我建议使用type = "b"来绘制点和线:

data(aSAH)
plot(roc(aSAH$outcome, aSAH$wfns), type = "b")

enter image description here

然后,您也可以在这些点上玩pchbgcol。但是,您注意到这对您不起作用,因为您的曲线有太多的点。