介绍其它用于ROC绘制的R包

library(tidyverse)  # for data manipulation
## -- Attaching packages -------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.0     √ purrr   0.3.2
## √ tibble  2.1.3     √ dplyr   0.8.3
## √ tidyr   0.8.3     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ----------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(pkgsearch)  # for searching packages
## Warning: package 'pkgsearch' was built under R version 3.6.1

该包会提供一系列关于感兴趣主题的R包,包括他们的评分,作者,连接等等 ps函数等价于pkg_search size:定义返回结果数量 format=“short”返回格式

Sys.setlocale('LC_ALL','C')
## [1] "C"
rocPkg <-  pkg_search(query="ROC",size=200)
head(rocPkg)
## - "ROC" ----------------------------------- 74 packages in 0.011 seconds - 
##   #     package     version 
##  1  100 pROC        1.15.0  
##  2   44 caTools     1.17.1.2
##  3   18 survivalROC 1.0.3   
##  4   18 PRROC       1.3.1   
##  5   15 plotROC     2.2.1   
##  6   14 precrec     0.10.1  
##  by                                                                    
##  Xavier Robin                                                          
##  ORPHANED                                                              
##  Paramita Saha-Chaudhuri<U+000a><paramita.sahachaudhuri.work@gmail.com>
##  Jan Grau                                                              
##  Michael C. Sachs                                                      
##  Takaya Saito                                                          
##    @
##   2M
##   4M
##   7y
##   1y
##   1y
##   3M
class(rocPkg)
## [1] "pkg_search_result" "tbl_df"            "tbl"              
## [4] "data.frame"
library(ROCR)
## Loading required package: gplots
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
data(ROCR.simple)
df <- data.frame(ROCR.simple)
head(df)
##   predictions labels
## 1   0.6125478      1
## 2   0.3642710      1
## 3   0.4321361      0
## 4   0.1402911      0
## 5   0.3848959      0
## 6   0.2444155      1
pred <- prediction(df$predictions, df$labels)
perf <- performance(pred,"tpr","fpr")
perf
## An object of class "performance"
## Slot "x.name":
## [1] "False positive rate"
## 
## Slot "y.name":
## [1] "True positive rate"
## 
## Slot "alpha.name":
## [1] "Cutoff"
## 
## Slot "x.values":
## [[1]]
##   [1] 0.000000000 0.000000000 0.000000000 0.000000000 0.009345794
##   [6] 0.009345794 0.018691589 0.028037383 0.028037383 0.028037383
##  [11] 0.028037383 0.028037383 0.028037383 0.028037383 0.037383178
##  [16] 0.037383178 0.037383178 0.037383178 0.037383178 0.037383178
##  [21] 0.037383178 0.037383178 0.046728972 0.046728972 0.046728972
##  [26] 0.046728972 0.046728972 0.046728972 0.046728972 0.046728972
##  [31] 0.046728972 0.046728972 0.046728972 0.046728972 0.046728972
##  [36] 0.046728972 0.056074766 0.056074766 0.056074766 0.056074766
##  [41] 0.065420561 0.065420561 0.065420561 0.065420561 0.065420561
##  [46] 0.065420561 0.065420561 0.065420561 0.065420561 0.065420561
##  [51] 0.065420561 0.065420561 0.065420561 0.074766355 0.084112150
##  [56] 0.084112150 0.084112150 0.084112150 0.084112150 0.084112150
##  [61] 0.093457944 0.093457944 0.102803738 0.102803738 0.102803738
##  [66] 0.102803738 0.102803738 0.102803738 0.112149533 0.112149533
##  [71] 0.112149533 0.112149533 0.112149533 0.112149533 0.112149533
##  [76] 0.121495327 0.121495327 0.121495327 0.121495327 0.121495327
##  [81] 0.130841121 0.130841121 0.130841121 0.130841121 0.130841121
##  [86] 0.140186916 0.140186916 0.140186916 0.140186916 0.140186916
##  [91] 0.140186916 0.140186916 0.140186916 0.149532710 0.149532710
##  [96] 0.149532710 0.158878505 0.168224299 0.177570093 0.186915888
## [101] 0.196261682 0.205607477 0.214953271 0.224299065 0.233644860
## [106] 0.242990654 0.252336449 0.261682243 0.271028037 0.280373832
## [111] 0.289719626 0.299065421 0.308411215 0.317757009 0.327102804
## [116] 0.336448598 0.345794393 0.355140187 0.364485981 0.373831776
## [121] 0.383177570 0.392523364 0.401869159 0.411214953 0.420560748
## [126] 0.429906542 0.439252336 0.439252336 0.448598131 0.457943925
## [131] 0.467289720 0.476635514 0.476635514 0.485981308 0.495327103
## [136] 0.504672897 0.514018692 0.514018692 0.514018692 0.523364486
## [141] 0.532710280 0.542056075 0.551401869 0.560747664 0.560747664
## [146] 0.560747664 0.570093458 0.579439252 0.588785047 0.588785047
## [151] 0.598130841 0.607476636 0.607476636 0.616822430 0.626168224
## [156] 0.635514019 0.635514019 0.644859813 0.654205607 0.663551402
## [161] 0.672897196 0.682242991 0.691588785 0.700934579 0.710280374
## [166] 0.719626168 0.728971963 0.738317757 0.747663551 0.747663551
## [171] 0.757009346 0.766355140 0.775700935 0.785046729 0.794392523
## [176] 0.803738318 0.803738318 0.813084112 0.822429907 0.831775701
## [181] 0.841121495 0.850467290 0.859813084 0.869158879 0.878504673
## [186] 0.887850467 0.897196262 0.906542056 0.915887850 0.925233645
## [191] 0.934579439 0.943925234 0.953271028 0.962616822 0.971962617
## [196] 0.981308411 0.990654206 0.990654206 0.990654206 1.000000000
## [201] 1.000000000
## 
## 
## Slot "y.values":
## [[1]]
##   [1] 0.00000000 0.01075269 0.02150538 0.03225806 0.03225806 0.04301075
##   [7] 0.04301075 0.04301075 0.05376344 0.06451613 0.07526882 0.08602151
##  [13] 0.09677419 0.10752688 0.10752688 0.11827957 0.12903226 0.13978495
##  [19] 0.15053763 0.16129032 0.17204301 0.18279570 0.18279570 0.19354839
##  [25] 0.20430108 0.21505376 0.22580645 0.23655914 0.24731183 0.25806452
##  [31] 0.26881720 0.27956989 0.29032258 0.30107527 0.31182796 0.32258065
##  [37] 0.32258065 0.33333333 0.34408602 0.35483871 0.35483871 0.36559140
##  [43] 0.37634409 0.38709677 0.39784946 0.40860215 0.41935484 0.43010753
##  [49] 0.44086022 0.45161290 0.46236559 0.47311828 0.48387097 0.48387097
##  [55] 0.48387097 0.49462366 0.50537634 0.51612903 0.52688172 0.53763441
##  [61] 0.53763441 0.54838710 0.54838710 0.55913978 0.56989247 0.58064516
##  [67] 0.59139785 0.60215054 0.60215054 0.61290323 0.62365591 0.63440860
##  [73] 0.64516129 0.65591398 0.66666667 0.66666667 0.67741935 0.68817204
##  [79] 0.69892473 0.70967742 0.70967742 0.72043011 0.73118280 0.74193548
##  [85] 0.75268817 0.75268817 0.76344086 0.77419355 0.78494624 0.79569892
##  [91] 0.80645161 0.81720430 0.82795699 0.82795699 0.83870968 0.84946237
##  [97] 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237
## [103] 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237
## [109] 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237
## [115] 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237
## [121] 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237 0.84946237
## [127] 0.84946237 0.86021505 0.86021505 0.86021505 0.86021505 0.86021505
## [133] 0.87096774 0.87096774 0.87096774 0.87096774 0.87096774 0.88172043
## [139] 0.89247312 0.89247312 0.89247312 0.89247312 0.89247312 0.89247312
## [145] 0.90322581 0.91397849 0.91397849 0.91397849 0.91397849 0.92473118
## [151] 0.92473118 0.92473118 0.93548387 0.93548387 0.93548387 0.93548387
## [157] 0.94623656 0.94623656 0.94623656 0.94623656 0.94623656 0.94623656
## [163] 0.94623656 0.94623656 0.94623656 0.94623656 0.94623656 0.94623656
## [169] 0.94623656 0.95698925 0.95698925 0.95698925 0.95698925 0.95698925
## [175] 0.95698925 0.95698925 0.96774194 0.96774194 0.96774194 0.96774194
## [181] 0.96774194 0.96774194 0.96774194 0.96774194 0.96774194 0.96774194
## [187] 0.96774194 0.96774194 0.96774194 0.96774194 0.96774194 0.96774194
## [193] 0.96774194 0.96774194 0.96774194 0.96774194 0.96774194 0.97849462
## [199] 0.98924731 0.98924731 1.00000000
## 
## 
## Slot "alpha.values":
## [[1]]
##   [1]         Inf 0.991096434 0.984667270 0.984599159 0.983494405
##   [6] 0.970641299 0.959417835 0.945548941 0.943432189 0.938583020
##  [11] 0.932159806 0.930846938 0.912325837 0.911723615 0.907651100
##  [16] 0.905121019 0.890172812 0.890078186 0.885179651 0.883431431
##  [21] 0.879459891 0.876086217 0.868751832 0.868331219 0.865639036
##  [26] 0.858970526 0.858876675 0.844415442 0.842974300 0.835981859
##  [31] 0.834803976 0.814586047 0.806543942 0.790240205 0.785213140
##  [36] 0.781781371 0.779771989 0.773336236 0.772728821 0.770178504
##  [41] 0.760726142 0.757364019 0.757325590 0.748438143 0.746588080
##  [46] 0.745110673 0.744769966 0.739554341 0.728536415 0.717845453
##  [51] 0.716680598 0.715459280 0.714985914 0.708412104 0.703885141
##  [56] 0.703293499 0.701561487 0.700501359 0.698826511 0.697235328
##  [61] 0.696702150 0.694457482 0.689075677 0.665444679 0.663367560
##  [66] 0.658692553 0.657732644 0.642933593 0.636728451 0.628095575
##  [71] 0.627012496 0.623494622 0.620501132 0.612547843 0.606960209
##  [76] 0.605235403 0.598162949 0.589909284 0.586857799 0.579511087
##  [81] 0.576836208 0.567043171 0.547983407 0.543533783 0.540221346
##  [86] 0.539086009 0.537336015 0.537091350 0.536339509 0.531958184
##  [91] 0.531464015 0.531006532 0.529402244 0.522384507 0.517308606
##  [96] 0.501489336 0.499788081 0.496513160 0.492596896 0.491735594
## [101] 0.490119891 0.488179708 0.480100183 0.470206008 0.468610247
## [106] 0.463227453 0.461150807 0.458674210 0.448687336 0.448484671
## [111] 0.439399995 0.432136142 0.429650397 0.423739359 0.419560072
## [116] 0.408939895 0.402801992 0.393378108 0.392173971 0.385240464
## [121] 0.384895941 0.383807972 0.380396459 0.378092079 0.371936985
## [126] 0.370549294 0.364288579 0.364270971 0.362693564 0.360180429
## [131] 0.360168796 0.353777439 0.353281048 0.349540913 0.348213542
## [136] 0.340078180 0.336592126 0.336315317 0.326672910 0.319672178
## [141] 0.310718619 0.300973098 0.292368449 0.288258273 0.277656869
## [146] 0.268138293 0.265686095 0.259278968 0.257402979 0.253271061
## [151] 0.248707470 0.248500489 0.244415489 0.240911145 0.239143340
## [156] 0.233160827 0.230157183 0.212404891 0.212014560 0.211378771
## [161] 0.195097957 0.194398425 0.180631658 0.172741714 0.161874325
## [166] 0.151350957 0.140291078 0.138387070 0.134794140 0.133257805
## [171] 0.126480399 0.123504905 0.122284044 0.120604738 0.120473353
## [176] 0.118555284 0.110241034 0.107031842 0.105722115 0.101699993
## [181] 0.090988280 0.088450984 0.086546283 0.085993218 0.084254795
## [186] 0.083685883 0.083048377 0.081439129 0.072369921 0.060413627
## [191] 0.059045866 0.050999138 0.040906997 0.035815400 0.030235062
## [196] 0.018527600 0.016694412 0.014823599 0.012653524 0.007746305
## [201] 0.005422562
plot(perf,colorize=TRUE)  


plotROC包-ggplot绘制ROC曲线

ROC曲线用于评估连续测量的精度,以预测二进制结果。在医学上,ROC曲线用于评价放射学和一般诊断的诊断试验有着悠久的历史。ROC曲线在信号检测理论中也有很长的应用历史。

require(plotROC)
## Loading required package: plotROC
## Warning: package 'plotROC' was built under R version 3.6.1
  • 提供网页版操作,为了代码的连贯性,这里不介绍网页版
  • 不可能我们分析到一般导出数据,拿到网页版去操作

  • 基本用法
set.seed(2529)
D.ex <- rbinom(200, size = 1, prob = .5)
M1 <- rnorm(200, mean = D.ex, sd = .65)
M2 <- rnorm(200, mean = D.ex, sd = 1.5)
test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1], 
                   M1 = M1, M2 = M2, stringsAsFactors = FALSE)
head(test)
##   D   D.str         M1          M2
## 1 1     Ill 1.48117155 -2.50636605
## 2 1     Ill 0.61994478  1.46861033
## 3 0 Healthy 0.57613345  0.07532573
## 4 1     Ill 0.85433197  2.41997703
## 5 0 Healthy 0.05258342  0.01863718
## 6 1     Ill 0.66703989  0.24732453
  • geom_roc绘图
    • d为编码1/0, m为用于预测的值marker
    • 注意需要一个disease code,不一定是1/0,但最后选择编码为1/0
    • 如不1/0,则stat_roc默认按顺序最低值为无病状态
basicplot <- ggplot(test, aes(d = D, m = M1)) + geom_roc()
basicplot

  • 如果diseaase编码非1/0
    • 提示warning:但仍能继续
ggplot(test, aes(d = D.str, m = M1)) + geom_roc()
## Warning in verify_d(data$d): D not labeled 0/1, assuming Healthy = 0 and
## Ill = 1!

  • n.cuts参数:展示几个截断点
  • labelsize: 展示标签的大小
  • labelround: label值保留几位小数
ggplot(test, aes(d = D, m = M1)) + geom_roc(n.cuts = 5, labelsize = 5,  labelround = 2)

  • 修改style-style_roc函数
styledplot <- basicplot + style_roc()
styledplot

  • 修改xlab, 主题
basicplot + style_roc(theme = theme_grey, xlab = "1 - Specificity")

multiROC-多因素诊断

  • meltroc转换数据为长数据,原数据为两列marker
  • meltroc类似于dplyr中的gather
head(test)
##   D   D.str         M1          M2
## 1 1     Ill 1.48117155 -2.50636605
## 2 1     Ill 0.61994478  1.46861033
## 3 0 Healthy 0.57613345  0.07532573
## 4 1     Ill 0.85433197  2.41997703
## 5 0 Healthy 0.05258342  0.01863718
## 6 1     Ill 0.66703989  0.24732453
longtest <- melt_roc(test, "D", c("M1", "M2"))
head(longtest)
##     D          M name
## M11 1 1.48117155   M1
## M12 1 0.61994478   M1
## M13 0 0.57613345   M1
## M14 1 0.85433197   M1
## M15 0 0.05258342   M1
## M16 1 0.66703989   M1
table(longtest$name)
## 
##  M1  M2 
## 200 200
## ROC曲线比较
ggplot(longtest, aes(d = D, m = M, color = name)) + 
  geom_roc() + 
  style_roc()+
  ggsci::scale_color_lancet()

  • ggplot2分面
ggplot(longtest, aes(d = D, m = M, color = name)) + 
  geom_roc() + 
  style_roc()+
  facet_wrap(~name)+
  ggsci::scale_color_lancet()

  • 主题与注释
    • AUC计算并绘制在图中-calc_auc函数
    • calc_auc(basicplot)$AUC提取
basicplot + 
  style_roc(theme = theme_grey) + ##主题修改
  theme(axis.text = element_text(colour = "blue")) +
  ggtitle("Themes and annotations") + ## 标题
  annotate("text", x = .75, y = .25, ## 注释text的位置
           label = paste("AUC =", round(calc_auc(basicplot)$AUC, 2))) +
  scale_x_continuous("1 - Specificity", breaks = seq(0, 1, by = .1)) ## x刻度
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.

## Scale for 'x' is already present. Adding another scale for 'x', whi
  • 对multi_ROC注释
  • 实现多个AUC值的呈现
  • 实际上仍然是ggplot2语法中的annotate注释
p<-ggplot(longtest, aes(d = D, m = M, color = name)) + 
  geom_roc(n.cuts = 0) + 
  style_roc()+
  ggsci::scale_color_lancet()
auc<-calc_auc(p)
head(auc)
##   PANEL group      AUC
## 1     1     1 0.833985
## 2     1     2 0.679599
p+annotate("text",x = .75, y = .25, ## 注释text的位置
           label = paste("AUC of M1 =", round(calc_auc(p)$AUC[1], 2))) +
  annotate("text",x = .75, y = .15, ## 注释text的位置)
            label=paste("AUC of M2 =", round(calc_auc(p)$AUC[2], 2)))