繁体   English   中英

如何从给定的值 Plot rStudios 中的 ROC 曲线?

[英]How to Plot the ROC Curve in rStudios from the given values?

混淆矩阵值

  1. 截止 / TP / FP / TN / FN

  2. 0.1 100 50 500 450

  3. 0.2 150 100 450 400

  4. 0.3 250 150 400 300

  5. 0.4 300 200 350 250

  6. 0.5 350 250 300 200

  7. 0.6 350 300 250 200

  8. 0.7 400 350 200 150

  9. 0.8 400 400 150 150

  10. 0.9 450 450 100 100

  11. 1.0 500 500 50 50

仅使用 base-R,您可以编写以下代码:

## your data
df <- read.table(header = TRUE, text = "
Cut_off TP FP TN FN
0.1 100 50 500 450
0.2 150 100 450 400
0.3 250 150 400 300
0.4 300 200 350 250
0.5 350 250 300 200
0.6 350 300 250 200
0.7 400 350 200 150
0.8 400 400 150 150
0.9 450 450 100 100
1.0 500 500 50 50")

## calculate False Positive ratio
df$FPR <- df$FP/(df$FP + df$TN)
## calculte True Positive Ratio
df$TPR <- df$TP/(df$TP + df$FN)

## df is now: 
   Cut_off  TP  FP  TN  FN        FPR       TPR
      0.1 100  50 500 450 0.09090909 0.1818182
      0.2 150 100 450 400 0.18181818 0.2727273
      0.3 250 150 400 300 0.27272727 0.4545455
      0.4 300 200 350 250 0.36363636 0.5454545
      0.5 350 250 300 200 0.45454545 0.6363636
      0.6 350 300 250 200 0.54545455 0.6363636
      0.7 400 350 200 150 0.63636364 0.7272727
      0.8 400 400 150 150 0.72727273 0.7272727
      0.9 450 450 100 100 0.81818182 0.8181818
      1.0 500 500  50  50 0.90909091 0.9090909

## plot the ROC with base plot
plot(df$FPR, df$TPR, type = "b", 
     xlim = c(0,1), ylim = c(0,1), 
     main = 'ROC Curve',
     xlab = "False Positive Rate (1 - Specificity)",
     ylab = "True Positive Rate (Sensitivity)",
     col = "blue")
abline(a = 0, b = 1, lty=2, col = "grey") ### pure chance line

产生以下 plot:

在此处输入图像描述

如果你想用 label 标记截止点,你需要在abline(...

text(df$FPR, df$TPR+.05, df$Cut_off, col = "blue", cex = .7)

产生这个 plot:

在此处输入图像描述

这是您可以使用带有ggplotdplyr的 ROC plot 的一种方法。 首先是您的数据:

df = structure(list(Cutoff = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 
     0.8, 0.9, 1), TP = c(100, 150, 250, 300, 350, 350, 400, 400, 
     450, 500), FP = c(50, 100, 150, 200, 250, 300, 350, 400, 450, 
     500), TN = c(500, 450, 400, 350, 300, 250, 200, 150, 100, 50), 
     FN = c(450, 400, 300, 250, 200, 200, 150, 150, 100, 50)), class = 
     "data.frame", row.names = c(NA,-10L))

对于 ROC,您需要 False-Positive-rate (FPR) 和 True-Positive-rate (TPR),我在这里使用mutate计算:

df %>% mutate( FPR = FP / (FP + TN) , TPR = TP / ( TP + FN )) %>%
   ggplot( aes ( x = FPR , y = TPR)) + geom_point(size = 0) + 
   geom_line(size = 1, alpha = 1) + theme_bw() +
   xlab("1 - Specificity") + ylab("Sensitivity") +
   theme(
     plot.title = element_text(size = 20,hjust = 0.5),
     axis.text = element_text(size =10),
     axis.title = element_text(size = 20)
   ) + annotate('segment' , x = 0, xend = 1, y = 0, yend = 1, alpha = 0.7) 

结果如下: 在此处输入图像描述

如果你想在图上有点,你可以改变geom_point的大小,这将是结果: 在此处输入图像描述

暂无
暂无

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

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