【问题标题】:Plotting mean ROC curve for multiple ROC curves, R绘制多条 ROC 曲线的平均 ROC 曲线,R
【发布时间】:2019-02-27 06:59:34
【问题描述】:

我有一个包含 100 个样本的数据集,每个样本有 195 个突变及其对应的已知临床意义(“RealClass”)和根据某些预测工具的预测值(“PredictionValues”)

为了演示,这是一个与我的数据集具有相同结构的随机数据集:

predictions_100_samples<-as.data.frame(matrix(nrow=19500,ncol=3))
colnames(predictions_100_samples)<-c("Sample","PredictionValues","RealClass")
predictions_100_samples$Sample<-rep(c(1:100), each = 195)
predictions_100_samples$PredictionValues<-sample(seq(0,1,length.out=19500))
predictions_100_samples$RealClass<-rep(c("pathogenic","benign"),each=10)
colours_for_ROC_curves<-rainbow(n=100)

我通过 PROC 包将所有这 100 个样本绘制为 ROC 曲线:

library("pROC")
roc_both <- plot(roc(predictor=predictions_100_samples[1:195,2],response = predictions_100_samples[1:195,3]), col = colours_for_ROC_curves[1],main="100 samples ROC curves",legacy.axes=TRUE,lwd=1)
i=2
for(i in 1:100){
    set.seed(500)
    roc_both <- plot(roc(predictor=predictions_100_samples[(((i-1)*195)+1):(i*195),2],response = predictions_100_samples[(((i-1)*195)+1):(i*195),3]), col = colours_for_ROC_curves[i], add = TRUE,lwd=1)
                     i=i+1
}

这就是最终情节的样子:

现在,我想将所有 100 条绘制的 ROC 曲线的平均 ROC 曲线添加到同一个图中。 我尝试使用我编写的循环中通过“roc”函数为每个阈值计算的灵敏度和特异性(可以通过roc_both$sensitivitiesroc_both$specificitiesroc_both$thresholds 实现)

但主要问题是选择的阈值是随机的,并且在我绘制的 100 条 ROC 曲线上并不相等,因此我无法手动计算平均 ROC 曲线。

是否有不同的软件包可以让我生成多个 ROC 曲线的平均 ROC 曲线?或者是否有一个包允许手动设置计算灵敏度和特异性的阈值,这样我以后就可以计算平均 ROC 曲线? 您可能对我的问题有不同的解决方案吗?

谢谢!

【问题讨论】:

    标签: r plot mean proc roc


    【解决方案1】:

    您可以使用cutpointr 通过oc_manual 函数手动指定阈值。我稍微改变了数据生成,使 ROC 曲线看起来更好一些。

    我们对所有样本应用相同的阈值序列,并取每个阈值的灵敏度和特异性的平均值,以获得“平均 ROC 曲线”。

    predictions_100_samples <- data.frame(
        Sample = rep(c(1:100), times = 195),
        PredictionValues = c(rnorm(n = 9750), rnorm(n = 9750, mean = 1)),
        RealClass = c(rep("benign", times = 9750), rep("pathogenic", times = 9750))
    )
    
    library(cutpointr)
    library(tidyverse)
    mean_roc <- function(data, cutoffs = seq(from = -5, to = 5, by = 0.5)) {
        map_df(cutoffs, function(cp) {
            out <- cutpointr(data = data, x = PredictionValues, class = RealClass,
                             subgroup = Sample, method = oc_manual, cutpoint = cp,
                             pos_class = "pathogenic", direction = ">=")
            data.frame(cutoff = cp, 
                       sensitivity = mean(out$sensitivity),
                       specificity = mean(out$specificity))
        })
    }
    
    mr <- mean_roc(predictions_100_samples)
    ggplot(mr, aes(x = 1 - specificity, y = sensitivity)) + 
        geom_step() + geom_point() +
        theme(aspect.ratio = 1)
    

    您可以使用cutpointr 以这种方式绘制单独的 ROC 曲线和添加的平均 ROC 曲线:

    cutpointr(data = predictions_100_samples, 
              x = PredictionValues, class = RealClass, subgroup = Sample,
              pos_class = "pathogenic", direction = ">=") %>% 
        plot_roc(display_cutpoint = F) + theme(legend.position="none") +
        geom_line(data = mr, mapping = aes(x = 1 - specificity, y = sensitivity), 
                  color = "black")
    

    或者,您可能需要研究关于汇总 ROC 曲线 (SROC) 的理论,以拟合组合多个 ROC 曲线的参数模型。

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2014-02-17
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2016-02-04
      • 2021-01-30
      • 1970-01-01
      • 2018-12-24
      相关资源
      最近更新 更多