在 pROC 包中指定正数 class

Specying the positive class in pROC package

我想使用 pROC 包计算不同的 class化指标(灵敏度、特异性)。为此,我可以在 pROC 包中使用 coords 函数作为:

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

这里将 1 作为正值 class,即可能是最普遍的 class,但我不确定。我在想,是否可以使用“0”作为正数 class。 例如,您可以在 caret 包的 confusionMatrix 函数中这样做:

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

对于 1 为正并且

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

0 为阳性 class。我正在使用 pROC 包,因为它提供了其他功能,例如确定最佳截止值等,这在插入符号中是不可能的。但是,有没有办法在 pROC 包中指定正负 class?

使用levels参数:

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

这里"control"表示消极观察,"case"是积极观察。默认选择不基于普遍性,仅基于 levels(as.factor(response)) 的前两个值的顺序。

要更改它,传递一个长度为 2 的向量,例如:

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

请注意,在您设置 direction 参数之前,它不会对您的曲线产生影响,该参数默认为 "auto"