Home > database >  Superimposing another precision-recall curve on an existing one using pROC plot
Superimposing another precision-recall curve on an existing one using pROC plot

Time:06-09

I'm using R's pROC package to visualize the ROC and Precision-Recall curves of two models. The first task can be easily done using the package's plot.roc() function, and then overlay the ROC curve of the second model right after the first one using base R's lines() function.

However, the second task is a bit more involved. According to the package's manual, one needs to use its coords() function to convert a ROC object to something contains "precision" and "recall" metrics, and then feed the object to the plot() function. But then I was unable to overlay another Precision-Recall (P-R) curve on an existing P-R curve.

I list my working data and code at below. Hope someone could shed some light on this.

# require pROC
library(pROC)

# example data from two models
m1 <- readRDS(url("https://www.dropbox.com/s/7nes3pjclnuv8dw/m1.rds?dl=1"))
m2 <- readRDS(url("https://www.dropbox.com/s/condmtltovq0iqu/m2.rds?dl=1"))

# plotting ROC (TPR & FPR)
roc1 <- roc(m1$outcome, m1$probability, main = "Smoothing")
roc2 <- roc(m2$outcome, m2$probability, main = "Smoothing")

# generate a plot with two curves
plot.roc(roc1, percent = TRUE, main = "ROC curves", add =  FALSE, asp = NA)
lines(roc2, type = "l", lty = 2, col = "grey35")

# plotting Precision-Recall curves
plot(precision ~ recall, (coords(roc1, "all", ret = c("precision", "recall"))), type="l")

# how to superimpose the P-R curve of m2 on the graph above?

CodePudding user response:

pROC uses some magic to setup the plots, which makes it a bit tricky to add different non-ROC curves on top.

Therefore I would use the base plot function to draw the ROC curve.

I would also set the axes labels to something that makes sense (here I just remove them).

# Plot roc1
plot(sensitivity ~ `1-specificity`, coords(roc1, "all", ret = c("sensitivity", "1-specificity")),  main = "Several curves")

# Add the roc2 line
lines(sensitivity ~ `1-specificity`, coords(roc2, "all", ret = c("sensitivity", "1-specificity")),  type = "l", lty = 2, col = "grey35")

# Add the PR curve
lines(precision ~ recall, (coords(roc1, "all", ret = c("precision", "recall"))), type="l", col="blue")
  • Related