使用 ROCR 和 pROC (R) 计算平均 AUC 的差异
Difference in average AUC computation using ROCR and pROC (R)
我正在处理来自使用 caret
包生成的 SVM-RFE 模型的交叉验证数据(10 倍重复 5 次)。我知道 caret
包在计算指标时与 pROC
包一起使用,但我需要使用 ROCR
包才能获得平均 ROC。但是,我注意到使用每个包时的平均 AUC 值并不相同,所以我不确定是否应该模糊地使用两个包。
我用来证明的代码是:
predictions_NG3<-list()
labels_NG3<-list()
optSize <- svmRFE_NG3$optsize
resamples<-(split(svmRFE_NG3$pred,svmRFE_NG3$pred$Variables))
resamplesFOLD<-(split(resamples[[optSize]],resamples[[optSize]]$Resample))
auc_pROC <- vector()
auc_ROCR <- vector()
for (i in 1:50){
predictions_NG3[[i]]<-resamplesFOLD[[i]]$LUNG
labels_NG3[[i]]<-resamplesFOLD[[i]]$obs
#WITH pROC
rocCurve <- roc(response = labels_NG3[[i]],
predictor = predictions_NG3[[i]],
levels = c("BREAST","LUNG")) #LUNG POSITIVE
auc_pROC <- c(auc_pROC,auc(rocCurve))
#WITH ROCR
pred_ROCR <- prediction(predictions_NG3[[i]], labels_NG3[[i]],
label.ordering = c("BREAST","LUNG")) #LUNG POSITIVE
auc_ROCR <- c(auc_ROCR,performance(pred_ROCR,"auc")@y.values[[1]])
}
auc_mean_pROC <- mean(auc_pROC)
auc_sd_pROC <- sd(auc_pROC)
auc_mean_ROCR <- mean(auc_ROCR)
auc_sd_ROCR <- sd(auc_ROCR)
结果略有不同:
auc_mean_pROC auc_sd_pROC auc_mean_ROCR auc_sd_ROCR
1 0.8755556 0.1524801 0.8488889 0.2072751
我注意到平均 AUC 计算在许多情况下会给我不同的结果,例如 [5]
、[22]
和 [25]
:
> auc_pROC
[1] 0.8333333 0.8333333 1.0000000 1.0000000 0.6666667 0.8333333 0.3333333 0.8333333 1.0000000 1.0000000 1.0000000 1.0000000
[13] 0.8333333 0.5000000 0.8888889 1.0000000 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 0.6666667 0.6666667 0.8888889
[25] 0.8333333 0.6666667 1.0000000 0.6666667 1.0000000 0.6666667 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 1.0000000
[37] 0.8333333 1.0000000 0.8333333 1.0000000 0.8333333 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[49] 1.0000000 1.0000000
> auc_ROCR
[1] 0.8333333 0.8333333 1.0000000 1.0000000 0.3333333 0.8333333 0.3333333 0.8333333 1.0000000 1.0000000 1.0000000 1.0000000
[13] 0.8333333 0.5000000 0.8888889 1.0000000 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 0.3333333 0.6666667 0.8888889
[25] 0.1666667 0.6666667 1.0000000 0.6666667 1.0000000 0.6666667 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 1.0000000
[37] 0.8333333 1.0000000 0.8333333 1.0000000 0.8333333 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[49] 1.0000000 1.0000000
我试过其他SVM-RFE模型,问题依旧。为什么会这样?我做错了什么吗?
默认情况下,pROC 中的 roc
函数会尝试检测控制和案例观察的响应水平(您通过设置 levels
参数覆盖默认值)以及控制是否应该具有比案例更高或更低的值。您还没有使用 direction
参数来设置后者。
当您对数据重新采样时,每个样本都会进行这种自动检测。如果您的样本量较小,或者您的 AUC 接近 0.5,则可能并且将会发生一些 ROC 曲线将以相反的方向生成,从而使您的平均值偏向更高的值。
因此,当您对 ROC 曲线或类似曲线重新采样时,您应该始终明确设置 direction
参数,例如:
rocCurve <- roc(response = labels_NG3[[i]],
predictor = predictions_NG3[[i]],
direction = "<",
levels = c("BREAST","LUNG"))
我正在处理来自使用 caret
包生成的 SVM-RFE 模型的交叉验证数据(10 倍重复 5 次)。我知道 caret
包在计算指标时与 pROC
包一起使用,但我需要使用 ROCR
包才能获得平均 ROC。但是,我注意到使用每个包时的平均 AUC 值并不相同,所以我不确定是否应该模糊地使用两个包。
我用来证明的代码是:
predictions_NG3<-list()
labels_NG3<-list()
optSize <- svmRFE_NG3$optsize
resamples<-(split(svmRFE_NG3$pred,svmRFE_NG3$pred$Variables))
resamplesFOLD<-(split(resamples[[optSize]],resamples[[optSize]]$Resample))
auc_pROC <- vector()
auc_ROCR <- vector()
for (i in 1:50){
predictions_NG3[[i]]<-resamplesFOLD[[i]]$LUNG
labels_NG3[[i]]<-resamplesFOLD[[i]]$obs
#WITH pROC
rocCurve <- roc(response = labels_NG3[[i]],
predictor = predictions_NG3[[i]],
levels = c("BREAST","LUNG")) #LUNG POSITIVE
auc_pROC <- c(auc_pROC,auc(rocCurve))
#WITH ROCR
pred_ROCR <- prediction(predictions_NG3[[i]], labels_NG3[[i]],
label.ordering = c("BREAST","LUNG")) #LUNG POSITIVE
auc_ROCR <- c(auc_ROCR,performance(pred_ROCR,"auc")@y.values[[1]])
}
auc_mean_pROC <- mean(auc_pROC)
auc_sd_pROC <- sd(auc_pROC)
auc_mean_ROCR <- mean(auc_ROCR)
auc_sd_ROCR <- sd(auc_ROCR)
结果略有不同:
auc_mean_pROC auc_sd_pROC auc_mean_ROCR auc_sd_ROCR
1 0.8755556 0.1524801 0.8488889 0.2072751
我注意到平均 AUC 计算在许多情况下会给我不同的结果,例如 [5]
、[22]
和 [25]
:
> auc_pROC
[1] 0.8333333 0.8333333 1.0000000 1.0000000 0.6666667 0.8333333 0.3333333 0.8333333 1.0000000 1.0000000 1.0000000 1.0000000
[13] 0.8333333 0.5000000 0.8888889 1.0000000 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 0.6666667 0.6666667 0.8888889
[25] 0.8333333 0.6666667 1.0000000 0.6666667 1.0000000 0.6666667 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 1.0000000
[37] 0.8333333 1.0000000 0.8333333 1.0000000 0.8333333 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[49] 1.0000000 1.0000000
> auc_ROCR
[1] 0.8333333 0.8333333 1.0000000 1.0000000 0.3333333 0.8333333 0.3333333 0.8333333 1.0000000 1.0000000 1.0000000 1.0000000
[13] 0.8333333 0.5000000 0.8888889 1.0000000 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 0.3333333 0.6666667 0.8888889
[25] 0.1666667 0.6666667 1.0000000 0.6666667 1.0000000 0.6666667 1.0000000 1.0000000 0.8333333 0.8333333 0.8333333 1.0000000
[37] 0.8333333 1.0000000 0.8333333 1.0000000 0.8333333 1.0000000 1.0000000 0.6666667 1.0000000 1.0000000 1.0000000 1.0000000
[49] 1.0000000 1.0000000
我试过其他SVM-RFE模型,问题依旧。为什么会这样?我做错了什么吗?
默认情况下,pROC 中的 roc
函数会尝试检测控制和案例观察的响应水平(您通过设置 levels
参数覆盖默认值)以及控制是否应该具有比案例更高或更低的值。您还没有使用 direction
参数来设置后者。
当您对数据重新采样时,每个样本都会进行这种自动检测。如果您的样本量较小,或者您的 AUC 接近 0.5,则可能并且将会发生一些 ROC 曲线将以相反的方向生成,从而使您的平均值偏向更高的值。
因此,当您对 ROC 曲线或类似曲线重新采样时,您应该始终明确设置 direction
参数,例如:
rocCurve <- roc(response = labels_NG3[[i]],
predictor = predictions_NG3[[i]],
direction = "<",
levels = c("BREAST","LUNG"))