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

混淆矩阵值

  1. 截止/ TP / FP / TN / FN
  2. 0.1 100 50 500 450
  3. 0.2150100450400
  4. 0.3250150400300
  5. 0.4300200350250
  6. 0.5350250300200
  7. 0.6 350300250200
  8. 0.7 400 350200150
  9. 0.8 400 400 150 150
  10. 0.9450450100100
  11. 1.0 500 500 50 50
评论
  • Oswal
    Oswal 回复

    仅使用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
    

    产生以下图:

    enter image description here

    if you want to mark the cut-off points with a label you need the following line after the line with abline(...

    text(df$FPR, df$TPR+.05, seq(from =.1, to = 1, by = .1), col = "blue", cex = .7)
    

    产生这个情节:

    enter image description here

  • 泪眸人
    泪眸人 回复

    Here is one way you can have a ROC plot with ggplot and dplyr. First here is your data:

    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))
    

    and for ROC, you need False-Positive-rate (FPR) and True-Positive-rate (TPR) which here I calculate with 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) 
    

    And here is the result: enter image description here

    If you want to have points on the plots you can change the size in geom_point and this would be the result: enter image description here