【问题标题】:plot.roc for multiclass.roc in pROC package?pROC 包中的 multiclass.roc 的 plot.roc?
【发布时间】:2016-03-14 04:37:35
【问题描述】:

我正在尝试绘制多类 ROC 曲线,但在 pROC 包中没有发现任何成果。这是一些开始代码:

data(iris)
library(randomForest)
library(pROC)
set.seed(1000)
# 3-class in response variable
rf = randomForest(Species~., data = iris, ntree = 100)
# predict(.., type = 'prob') returns a probability matrix
predictions <- as.numeric(predict(rf, iris, type = 'response'))
roc.multi <- multiclass.roc(iris$Species, predictions)
auc(roc.multi)

如何绘制各个类的 ROC 曲线?

【问题讨论】:

    标签: r roc auc


    【解决方案1】:

    查看roc.multi 的名称,您应该会找到一个名为rocs 的名称,其中存储了每个类的单独的roc 曲线信息。

    所以你可以使用plot.roclines.roc 来可视化它们:

    rs <- roc.multi[['rocs']]
    plot.roc(rs[[1]])
    sapply(2:length(rs),function(i) lines.roc(rs[[i]],col=i))
    

    【讨论】:

    • 这是一个绝妙的答案 - 非常有帮助!您知道如何修改它以添加具有相应颜色的图例吗?
    • @rastrast 同样的问题
    【解决方案2】:

    我也在寻找同样的东西,this 也可能有帮助

    require(multiROC)
    data(iris)
    head(iris)
    
    set.seed(123456)
    total_number <- nrow(iris)
    train_idx <- sample(total_number, round(total_number*0.6))
    train_df <- iris[train_idx, ]
    test_df <- iris[-train_idx, ]
    
    rf_res <- randomForest::randomForest(Species~., data = train_df, ntree = 100)
    rf_pred <- predict(rf_res, test_df, type = 'prob') 
    rf_pred <- data.frame(rf_pred)
    colnames(rf_pred) <- paste(colnames(rf_pred), "_pred_RF")
    
    mn_res <- nnet::multinom(Species ~., data = train_df)
    mn_pred <- predict(mn_res, test_df, type = 'prob')
    mn_pred <- data.frame(mn_pred)
    colnames(mn_pred) <- paste(colnames(mn_pred), "_pred_MN")
    
    true_label <- dummies::dummy(test_df$Species, sep = ".")
    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)
    
    roc_res <- multi_roc(final_df, force_diag=F)
    pr_res <- multi_pr(final_df, force_diag=F)
    
    plot_roc_df <- plot_roc_data(roc_res)
    plot_pr_df <- plot_pr_data(pr_res)
    
    require(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"))
    
    ggplot(plot_pr_df, aes(x=Recall, y=Precision)) + 
      geom_path(aes(color = Group, linetype=Method), size=1.5) + 
      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"))
    

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 2020-07-05
      • 2014-04-27
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2019-07-28
      • 2013-12-29
      相关资源
      最近更新 更多