如何在 R 中的一个图中组合多个 roc 曲线?

How to combine several roc curves in one graph in R?

我为4个模型绘制了4条roc曲线,想把它们放在一张图中以便比较。

library(pROC)

lr.probs <- predict(lr_model, newdata=test1, type='response')  
lr.plot <- plot(roc(test1$Y,lr.probs))  
gbm.probs <- predict(gbm,test1\[,predictorNames0\],type="prob")
gbm.plot <-plot(roc(test1$Y,gbm.probs\[,2\]))  
rf.probs <- predict(rf,test1\[,predictorNames0\],type="prob")
rf.plot <-plot(roc(test1$Y,rf.probs\[,2\]), col="blue")  
xgb.probs <- predict(model_xgb, newdata=d_test, type='response')
xgb.plot <-  plot(roc(test2$Y,xgb.probs))

它现在给了我 4 个单独的图表

您可以将 plot 函数与 add=TRUE 参数一起使用:

lr.plot <- plot(roc(test1$Y,lr.probs))  
gbm.plot <-plot(roc(test1$Y,gbm.probs\[,2\]), add=TRUE, col="red")  
rf.plot <-plot(roc(test1$Y,rf.probs\[,2\]), add=TRUE, col="blue")  
xgb.plot <-  plot(roc(test2$Y,xgb.probs), add=TRUE, col="green")

或者您可以使用 lines 函数:

lr.plot <- plot(roc(test1$Y,lr.probs))  
gbm.plot <-lines(roc(test1$Y,gbm.probs\[,2\]), col="red")  
rf.plot <-lines(roc(test1$Y,rf.probs\[,2\]), col="blue")  
xgb.plot <-  lines(roc(test2$Y,xgb.probs), col="green")