How to fill in AUC of a ROC plot in R?

2019-06-07 02:39发布

问题:

I have this ROC plot:

library(Epi)
library(pROC)
data(aSAH)
data <- aSAH
plot.roc(data$outcome, data$s100b)

I want to color the Area under the curve: 0.7314.

I tried variations on...

x <- seq(1, 0, by = - .001)
polygon(x, roc(data$outcome, data$s100b), 
        col = rgb(.35,0.31,0.61, alpha = 0.4), 
        border = rgb(.35,0.31,0.61, 0.4),
        lwd=2)

... and get the error message: Error in xy.coords(x, y) : 'x' and 'y' lengths differ.

How can I make the lengths work out?

回答1:

The plot.roc function from pROC has built parameters to enable (auc.polygon) and tune (auc.polygon.col, auc.polygon.border, etc., see ?plot.roc) the display of the AUC. In your case you could use:

plot.roc(data$outcome, data$s100b, 
    auc.polygon = TRUE, 
    auc.polygon.col=rgb(.35,0.31,0.61, alpha = 0.4), 
    auc.polygon.border=rgb(.35,0.31,0.61, 0.4))


回答2:

Try

library(Epi)
library(pROC)
data(aSAH)
data <- aSAH
res <- plot.roc(data$outcome, data$s100b)
polygon(with(res, cbind(specificities, sensitivities)), 
        col = rgb(.35,0.31,0.61, alpha = 0.4), 
        border = rgb(.35,0.31,0.61, 0.4),
        lwd=2)


标签: r roc