找ROC相关的包

Sys.setlocale('LC_ALL','C')
## [1] "C"
require(pkgsearch)
## Loading required package: pkgsearch
## Warning: package 'pkgsearch' was built under R version 3.6.1
rocPkg <-  pkg_search(query="ROC",size=200)
rocPkg
## - "ROC" ----------------------------------- 74 packages in 0.009 seconds - 
##   #     package          version  
##   1 100 pROC             1.15.3   
##   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   
##   7  14 WeightedROC      2018.10.1
##   8  13 surrosurvROC     0.1.0    
##   9  13 timeROC          0.3      
##  10  13 rocc             1.2      
##  11  12 cvAUC            1.1.0    
##  12  12 risksetROC       1.0.4    
##  13  12 icsurvROC        0.1.0    
##  14  12 roccv            1.2      
##  15  11 plotwidgets      0.4      
##  16  11 rocsvm.path      0.1.0    
##  17  11 nsROC            1.1      
##  18  11 ggROC            1.0      
##  19  11 Comp2ROC         1.1.4    
##  20  11 reportROC        3.3      
##  21  10 ROCS             1.3      
##  22  10 HandTill2001     0.2.12   
##  23  10 ROCt             0.9.5    
##  24  10 smoothROCtime    0.1.0    
##  25  10 npROCRegression  1.0.5    
##  26  10 sROC             0.1.2    
##  27  10 TOC              0.0.4    
##  28  10 HUM              1.0      
##  29  10 rocNIT           1.0      
##  30  10 trinROC          0.3      
##  31  10 ROCwoGS          1.0      
##  32   9 prognosticROC    0.7      
##  33   9 rocTree          1.0.0    
##  34   9 bcROCsurface     1.0.3    
##  35   9 ROCR             1.0.7    
##  36   8 multiROC         1.1.1    
##  37   8 iMRMC            1.2.0    
##  38   7 tdROC            1.0      
##  39   7 nproc            2.1.4    
##  40   6 ROC632           0.6      
##  41   4 PresenceAbsence  1.1.9    
##  42   3 hmeasure         1.0.2    
##  43   3 correctedAUC     0.0.3    
##  44   3 ModelGood        1.0.9    
##  45   2 AUCRF            1.1      
##  46   2 ezplot           0.3.1    
##  47   2 ROCit            1.1.1    
##  48   2 MATTOOLS         1.1      
##  49   2 riskRegression   2019.1.29
##  50   2 AnaCoDa          0.1.3.0  
##  51   2 AROC             1.0      
##  52   2 RJafroc          1.1.0    
##  53   2 logcondens       2.1.5    
##  54   2 DET              2.0.1    
##  55   2 Biocomb          0.4      
##  56   2 WVPlots          1.1.1    
##  57   2 MAMSE            0.2.1    
##  58   2 classifierplots  1.3.3    
##  59   2 InformationValue 1.2.3    
##  60   2 EL               1.0      
##  61   2 synRNASeqNet     1.0      
##  62   2 mccf1            1.0      
##  63   2 cubfits          0.1.3    
##  64   2 liquidSVM        1.2.2.1  
##  65   2 fbroc            0.4.1    
##  66   1 PredictABEL      1.2.2    
##  67   1 bmrm             4.1      
##  68   1 intcensROC       0.1.1    
##  69   1 DPpackage        1.1.7.4  
##  70   1 bimixt           1.0      
##  71   1 nlcv             0.3.5    
##  72   1 RcmdrPlugin.EZR  1.40     
##  73   1 r4lineups        0.1.1    
##  74   1 psfmi            0.1.0    
##  by                                                                    
##  Xavier Robin                                                          
##  ORPHANED                                                              
##  Paramita Saha-Chaudhuri<U+000a><paramita.sahachaudhuri.work@gmail.com>
##  Jan Grau                                                              
##  Michael C. Sachs                                                      
##  Takaya Saito                                                          
##  Toby Dylan Hocking                                                    
##  Yunro Chung                                                           
##  Paul Blanche                                                          
##  Martin Lauss                                                          
##  Erin LeDell                                                           
##  Paramita Saha-Chaudhuri<U+000a><paramita.sahachaudhuri.work@gmail.com>
##  Yunro Chung                                                           
##  Ben Sherwood                                                          
##  January Weiner                                                        
##  Seung Jun Shin                                                        
##  Sonia Perez Fernandez                                                 
##  Honglong Wu                                                           
##  Ana C. Braga                                                          
##  Zhicheng Du<dgdzc@hotmail.com>                                        
##  Tianwei Yu<tyu8@emory.edu>                                            
##  Andreas Dominik Cullmann                                              
##  Y. Foucher                                                            
##  Susana Diaz-Coto                                                      
##  Maria Xose Rodriguez-Alvarez                                          
##  Xiao-Feng Wang                                                        
##  Al<U+00ED> Santacruz                                                  
##  Natalia Novoselova                                                    
##  Zhicheng Du<dgdzc@hotmail.com>                                        
##  Reinhard Furrer                                                       
##  Chong Wang                                                            
##  Y. Foucher                                                            
##  Sy Han Chiou                                                          
##  Khanh To Duc                                                          
##  Tobias Sing                                                           
##  Runmin Wei                                                            
##  Brandon Gallas                                                        
##  Cai Wu                                                                
##  Yang Feng                                                             
##  Y. Foucher                                                            
##  Elizabeth Freeman                                                     
##  Christoforos Anagnostopoulos                                          
##  Weiliang Qiu                                                          
##  Thomas A. Gerds                                                       
##  Victor Urrea                                                          
##  Wojtek Kostelecki                                                     
##  Md Riaz Ahmed Khan                                                    
##  steven mosher                                                         
##  Thomas Alexander Gerds                                                
##  Cedric Landerer                                                       
##  Maria Xose Rodriguez-Alvarez                                          
##  Dev Chakraborty                                                       
##  Kaspar Rufibach                                                       
##  "Garc<U+00ED>a-R<U+00F3>denas, <U+00C1>lvaro"                         
##  Natalia Novoselova                                                    
##  John Mount                                                            
##  Jean-Francois Plante                                                  
##  Huw Campbell                                                          
##  Selva Prabhakaran                                                     
##  Edmunds Cers                                                          
##  Luciano Garofano                                                      
##  Chang Cao                                                             
##  Wei-Chen Chen                                                         
##  ORPHANED                                                              
##  Erik Peter                                                            
##  Suman Kundu                                                           
##  Julien Prados                                                         
##  Jiaxing Lin<jiaxing.lin@duke.edu>                                     
##  ORPHANED                                                              
##  Michelle Winerip                                                      
##  Laure Cougnaud                                                        
##  Yoshinobu Kanda                                                       
##  Colin Tredoux                                                         
##  Martijn Heymans                                                       
##    @
##   7d
##   5M
##   7y
##   1y
##   1y
##   4M
##  10M
##  11M
##   4y
##   9y
##   5y
##   7y
##   8M
##   3M
##   3y
##  10M
##   1y
##   6y
##   3y
##   4M
##   3y
##   3y
##   2y
##   9M
##   2y
##   7y
##   4y
##   6y
##   3y
##   2M
##   7y
##   6y
##   4M
##  10M
##   4y
##   1y
##   3M
##   3y
##   8M
##   6y
##   7y
##   5M
##   3y
##   5y
##   7y
##   8d
##   6M
##   7y
##   6M
##   3M
##   1y
##   9M
##   3y
##   1M
##   1y
##   4d
##   3y
##   2y
##   3y
##   8y
##   4y
##   1y
##   2y
##   5M
##   4M
##   5y
##   4M
##   1y
##   2y
##   4y
##   1y
##   3M
##   1y
##   2M
head(rocPkg)
## - "ROC" ----------------------------------- 74 packages in 0.009 seconds - 
##   #     package     version 
##  1  100 pROC        1.15.3  
##  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                                                          
##    @
##   7d
##   5M
##   7y
##   1y
##   1y
##   4M

multiROC包

require(multiROC)
## Loading required package: multiROC
## Warning: package 'multiROC' was built under R version 3.6.1
data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
set.seed(123456)
total_number <- nrow(iris)
train_idx <- sample(total_number, round(total_number*0.6))
## 随机抽样分train,test组
train_df <- iris[train_idx, ]
test_df <- iris[-train_idx, ]

Random forest法

rf_res <- randomForest::randomForest(Species~., data = train_df, ntree = 100)
rf_res
## 
## Call:
##  randomForest(formula = Species ~ ., data = train_df, ntree = 100) 
##                Type of random forest: classification
##                      Number of trees: 100
## No. of variables tried at each split: 2
## 
##         OOB estimate of  error rate: 7.78%
## Confusion matrix:
##            setosa versicolor virginica class.error
## setosa         30          0         0   0.0000000
## versicolor      0         29         3   0.0937500
## virginica       0          4        24   0.1428571
rf_pred <- predict(rf_res, test_df, type = 'prob') 
rf_pred <- data.frame(rf_pred)
colnames(rf_pred) <- paste(colnames(rf_pred), "_pred_RF")

logistic回归模型

mn_res <- nnet::multinom(Species ~., data = train_df)
## # weights:  18 (10 variable)
## initial  value 98.875106 
## iter  10 value 12.524348
## iter  20 value 5.495452
## iter  30 value 5.352345
## iter  40 value 5.304605
## iter  50 value 5.251277
## iter  60 value 5.250840
## final  value 5.250449 
## converged
mn_pred <- predict(mn_res, test_df, type = 'prob')
mn_pred <- data.frame(mn_pred)
colnames(mn_pred) <- paste(colnames(mn_pred), "_pred_MN")

整合预测值与真实值

## 设置真实值1为TRUE
true_label <- dummies::dummy(test_df$Species, sep = ".")
## Warning in model.matrix.default(~x - 1, model.frame(~x - 1), contrasts =
## FALSE): non-list contrasts argument ignored
true_label <- data.frame(true_label)
colnames(true_label) <- gsub(".*?\\.", "", colnames(true_label))
colnames(true_label) <- paste(colnames(true_label), "_true")
## 整合
final_df <- cbind(true_label, rf_pred, mn_pred)
head(final_df)
##    setosa _true versicolor _true virginica _true setosa _pred_RF
## 6             1                0               0            1.00
## 7             1                0               0            1.00
## 17            1                0               0            1.00
## 18            1                0               0            1.00
## 19            1                0               0            0.98
## 22            1                0               0            1.00
##    versicolor _pred_RF virginica _pred_RF setosa _pred_MN
## 6                 0.00                  0               1
## 7                 0.00                  0               1
## 17                0.00                  0               1
## 18                0.00                  0               1
## 19                0.02                  0               1
## 22                0.00                  0               1
##    versicolor _pred_MN virginica _pred_MN
## 6         6.639968e-11       4.388237e-33
## 7         5.584571e-09       9.246104e-31
## 17        1.097355e-13       3.009961e-37
## 18        1.539783e-10       4.700440e-33
## 19        4.592017e-11       6.295323e-34
## 22        8.766548e-11       8.012250e-33

multiROC

force_diag=T, 则TPR,FPR的值转换为0-1之间

roc_res <- multi_roc(final_df, force_diag=T)
## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

## Warning in regularize.values(x, y, ties, missing(ties)): collapsing to
## unique 'x' values

plot

多出的MacroROC,MicroROC为计算的新的AUC值 data为plot_roc_df, x为1-特异度, y为敏感度

## 将数据转换为ggplot喜欢的格式
plot_roc_df <- plot_roc_data(roc_res)
head(plot_roc_df)
##   Specificity Sensitivity   Group AUC Method
## 1           1        0.00 setosa    1     RF
## 2           1        0.05 setosa    1     RF
## 3           1        0.10 setosa    1     RF
## 4           1        0.15 setosa    1     RF
## 5           1        0.20 setosa    1     RF
## 6           1        0.25 setosa    1     RF
## 绘图
require(ggplot2)
## Loading required package: ggplot2
ggplot(plot_roc_df, aes(x = 1-Specificity, y=Sensitivity)) +
  geom_path(aes(color = Group, linetype=Method), size=1.5) +
  geom_segment(aes(x = 0, y = 0, xend = 1, yend = 1), 
                        colour='grey', linetype = 'dotdash') +
  theme_bw() + 
  theme(plot.title = element_text(hjust = 0.5), 
                 legend.justification=c(1, 0), legend.position=c(.95, .05),
                 legend.title=element_blank(), 
                 legend.background = element_rect(fill=NULL, size=0.5, 
                                                           linetype="solid", colour ="black"))

总结