lsamarahan
lsamarahan

Reputation: 149

How to get specific tpr from a sequence of fpr in R?

I want to get the true positive rates at specified false positive rates, says, at fpr = 0, 0.002, 0.01, .... is there an easy way to do that? I tried

performance <- performance(rocr,"tpr","fpr") [email protected][[1]] [email protected][[1]] which I can get all the fpr and tpr. How shall I proceed from here? Many thanks!

Upvotes: 1

Views: 496

Answers (1)

Calimo
Calimo

Reputation: 7959

Doing something like TC Zhang suggests in comment is not going to work:

tpr[which(fpr == yourfpr)]

This is because, although it is called a "curve", a ROC curve really is a set of discrete points, and the probability that your target FPR exactly matches one in the ROC curve is low.

Therefore you need to do a bit of interpolation to get the TPR at given FPR points. Fortunately the pROC package does that for you (dislaimer: I am the author of pROC). So let's say you have the following ROC curve:

library(pROC)
data(aSAH)
myroc <- roc(aSAH$outcome, aSAH$wfns)

First, you need to convert your FPR rates to specificity that pROC understands:

target.fpr <- c(0, 0.002, 0.01, 0.2, 0.1, 0.2, 1)
target.sp <- 1 - target.fpr

Then you use the coords function:

coords(myroc, x = target.sp, input = "specificity", ret = c("se", "sp"))

This takes the target specificities as input, and returns them along with the matching sensitivities, which are equal to the True Positive Rates or TPR you're interested in. To get the TPR as a vector, do:

tpr <- coords(myroc, x = target.sp, input = "specificity", ret = "se")[1,]

Upvotes: 1

Related Questions