【问题标题】:How to draw multiple roc curves with confidence interval in pROC?如何在pROC中绘制具有置信区间的多条roc曲线?
【发布时间】:2021-02-17 21:07:56
【问题描述】:

这段代码可以绘制一条带置信区间的roc曲线:

ciobj <- ci.se(obj, specificities=seq(0, 1, l=25))
dat.ci <- data.frame(x = as.numeric(rownames(ciobj)),
                     lower = ciobj[, 1],
                     upper = ciobj[, 3])

ggroc(obj) + theme_minimal() + geom_abline(slope=1, intercept = 1, linetype = "dashed", alpha=0.7, color = "grey") + coord_equal() + 
  geom_ribbon(data = dat.ci, aes(x = x, ymin = lower, ymax = upper), fill = "steelblue", alpha= 0.2) + ggtitle(capture.output(obj$ci))

而且这段代码可以同时绘制多条roc曲线

roc.list <- roc(outcome ~ s100b + ndka + wfns, data = aSAH)
g.list <- ggroc(roc.list)
g.list

但我无法将它们组合在一起:

ggroc(roc.list) + theme_minimal() + geom_abline(slope=1, intercept = 1, linetype = "dashed", alpha=0.7, color = "grey") + coord_equal() + 
  geom_ribbon(data = dat.ci, aes(x = x, ymin = lower, ymax = upper), fill = "steelblue", alpha= 0.2) + ggtitle(capture.output(obj$ci))

它会返回一个错误:

【问题讨论】:

    标签: r roc proc-r-package


    【解决方案1】:

    可能有更优雅的方式来做到这一点,但无论如何,这对我有用:

    library(pROC)
    roc.list <- roc(outcome ~ s100b + ndka + wfns, data = aSAH)
    
    
    ci.list <- lapply(roc.list, ci.se, specificities = seq(0, 1, l = 25))
    
    dat.ci.list <- lapply(ci.list, function(ciobj) 
      data.frame(x = as.numeric(rownames(ciobj)),
                 lower = ciobj[, 1],
                 upper = ciobj[, 3]))
    
    p <- ggroc(roc.list) + theme_minimal() + geom_abline(slope=1, intercept = 1, linetype = "dashed", alpha=0.7, color = "grey") + coord_equal()
    
    for(i in 1:3) {
      p <- p + geom_ribbon(
        data = dat.ci.list[[i]],
        aes(x = x, ymin = lower, ymax = upper),
        fill = i + 1,
        alpha = 0.2,
        inherit.aes = F) 
      } 
    
    p
    

    我不得不删除标题,并添加参数inherit.aes = F

    结果如下:

    【讨论】:

    • 谢谢!这正是我想要的。
    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2019-02-27
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2021-03-03
    相关资源
    最近更新 更多