簡體   English   中英

Plot R 中單個圖中的 3 條 ROC 曲線

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

我想在 R 中的單個圖中使用 plot 3 條 ROC 曲線。 我使用 pROC package。 生成的 plot 僅顯示兩條曲線。 我使用的代碼是:

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)

在這個 plot 中確實繪制了 3 條 ROC 曲線。

但曲線 1 和 3 正好在彼此之上。 如果您更改線的類型( lty=2使其成為虛線)並增加對比度(例如使其變為紅色),您將看到它出現。 您還可以移動 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)

因為您對權重進行了排序,並且因為 ROC 曲線只關心數據的排名而不是實際值,所以在weight1分析中weight和權重1 絕對相同。

3 條 ROC 曲線圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM