discipulus - 10 months ago 57

R Question

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

`coords`

`pROC`

`# 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`

For example you can do that in

`caret`

`confusionMatrix`

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

factor(aSAH$outcome,levels=c('0','1')), positive='1')

for

`1`

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

factor(aSAH$outcome,levels=c('0','1')), positive='0')

for

`0`

`pROC`

Answer Source

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.