discipulus discipulus - 28 days ago 10
R Question

Specying the positive class in pROC package

I want to calculate different classification metrics (sensitivity, specificity) using pROC package. For that, I can use

coords
function in
pROC
package as:

# Load library
library(pROC)
# Load data
data(aSAH)
#Convert Good and Poor to 1 and 0
aSAH$outcome <- ifelse(aSAH$outcome=="Good", 1, 0)
# Calculate ROC
rocobj <- roc(aSAH$outcome, aSAH$s100b)
# Get sensitivity and specificity
coords(rocobj, 0.55)


Here it takes
1
as positive class, i.e. may be the class that is most prevalent but I am not sure. I was wondering, if it possible to use '0' as the positive class.
For example you can do that in
caret
package's
confusionMatrix
function as:

confusionMatrix(factor(as.numeric(aSAH$s100b<0.55),levels=c('0','1')),
factor(aSAH$outcome,levels=c('0','1')), positive='1')


for
1
as positive and

confusionMatrix(factor(as.numeric(aSAH$s100b<0.55),levels=c('0','1')),
factor(aSAH$outcome,levels=c('0','1')), positive='0')


for
0
as positive class. I am using pROC package as it provides other functions such as determining the best cutoffs etc. which is not possible in caret. However, is there a way to specify positive and negative class in
pROC
package?

Answer

Use the levels argument:

levels: the value of the response for controls and cases
          respectively.

Here "control" means a negative observation, and "case" is a positive one. The choice is not based on prevalence, simply on the first two values of levels(as.factor(response)).

To change it, pass a vector of length two such as:

rocobj <- roc(aSAH$outcome, aSAH$s100b, levels = c(1, 0))

Note that it won't make a difference to your curve until you set the direction argument, which is on "auto" by default.