简体   繁体   English

Plot R 中单个图中的 3 条 ROC 曲线

[英]Plot 3 ROC curves in a single graph in R

I would like to plot 3 ROC curves in a single graph in R.我想在 R 中的单个图中使用 plot 3 条 ROC 曲线。 I use the pROC package.我使用 pROC package。 The resulting plot only shows the two curves.生成的 plot 仅显示两条曲线。 The code I use is:我使用的代码是:

library(pROC) # install with install.packages("pROC")
library(randomForest) # install with install.packages("randomForest")
set.seed(420) # this will make my results match yours
num.samples <- 100
weight <- sort(rnorm(n=num.samples, mean=172, sd=29))
weight1 <- sort(rnorm(n=num.samples, mean=112, sd=11))
obese <- ifelse(test=(runif(n=num.samples) < (rank(weight)/num.samples)), 
                yes=1, no=0)
glm.fit=glm(obese ~ weight, family=binomial)
lines(weight, glm.fit$fitted.values)
glm.fit1=glm(obese ~ weight1, family=binomial)
lines(weight1, glm.fit1$fitted.values)
rf.model <- randomForest(factor(obese) ~ weight)
    roc(obese, glm.fit$fitted.values, plot=TRUE, legacy.axes=TRUE, percent=TRUE, xlab="False Positive Percentage", ylab="True Postive Percentage", col="#377eb8", lwd=4, print.auc=TRUE) 
    plot.roc(obese, rf.model$votes[,1], percent=TRUE, col="#4daf4a", lwd=4, print.auc=TRUE, add=TRUE, print.auc.y=40)
    plot.roc(obese, glm.fit1$fitted.values, percent=TRUE, col="#b237b8", lwd=4, print.auc=TRUE, add=TRUE, print.auc.y=40)
    legend("bottomright", legend=c("Logisitic Regression", "Random Forest","ff"), col=c("#377eb8", "#4daf4a","#b237b8"), lwd=4)

There are indeed 3 ROC curves plotted in this plot.在这个 plot 中确实绘制了 3 条 ROC 曲线。

But curves 1 and 3 are exactly on top of each other.但曲线 1 和 3 正好在彼此之上。 If you change the type of line ( lty=2 to make it dotted) and increase the contrast (for instance making it red) you will see it appear.如果您更改线的类型( lty=2使其成为虚线)并增加对比度(例如使其变为红色),您将看到它出现。 You can also move the AUC text ( print.auc.y=30 ).您还可以移动 AUC 文本 ( print.auc.y=30 )。

plot.roc(obese, glm.fit1$fitted.values, percent=TRUE, col="red", lwd=2, lty=2, print.auc=TRUE, add=TRUE, print.auc.y=30)

Because you sorted the weights, and because ROC curves only care about ranking of the data instead of the actual values, weight and weight1 are absolutely identical in ROC analysis.因为您对权重进行了排序,并且因为 ROC 曲线只关心数据的排名而不是实际值,所以在weight1分析中weight和权重1 绝对相同。

3 条 ROC 曲线图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM