计算R中的AUC?

And*_*rew 42 r machine-learning data-mining auc

给定分数向量和实际类标签的向量,如何计算R语言或简单英语中二进制分类器的单数AUC度量?

"AUC:一个更好的测量......"的第9页似乎需要知道类标签,这里是MATLAB中我不明白的例子

R(Actual == 1))
Run Code Online (Sandbox Code Playgroud)

因为R(不要与R语言混淆)被定义为向量但是用作函数?

sem*_*maj 38

ROCR包将计算其他统计数据中的AUC:

auc.tmp <- performance(pred,"auc"); auc <- as.numeric(auc.tmp@y.values)
Run Code Online (Sandbox Code Playgroud)

  • `auc.tmp < - 表演(pred,"auc"); auc < - as.numeric(auc.tmp@y.values)` (9认同)

J. *_*in. 31

使用该包,pROC您可以使用auc()帮助页面中的此示例功能:

> data(aSAH)
> 
> # Syntax (response, predictor):
> auc(aSAH$outcome, aSAH$s100b)
Area under the curve: 0.7314
Run Code Online (Sandbox Code Playgroud)


eri*_*rik 29

如其他人所述,您可以使用ROCR包计算AUC .使用ROCR软件包,您还可以绘制ROC曲线,升力曲线和其他模型选择度量.

通过使用AUC等于真阳性得分大于真阴性的概率,您可以直接计算AUC而不使用任何包.

例如,如果pos.scores是包含正例的分数的向量,并且neg.scores是包含负例的向量,则AUC近似为:

> mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T))
[1] 0.7261
Run Code Online (Sandbox Code Playgroud)

将给出近似的AUC.您还可以通过引导来估计AUC的方差:

> aucs = replicate(1000,mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T)))
Run Code Online (Sandbox Code Playgroud)

  • @Andrew @eric这是一个可怕的答案.你做*不*估计AUC的方差 - 你只估计重采样过程的方差.为了说服自己,尝试更改`sample`中的样本大小...将其除以10,将方差乘以10.将其乘以10,将方差除以10.这当然不是计算所需的行为. AUC的方差. (7认同)
  • 同意@Calimo,这不是一个bootstrap.要进行自举,您必须使用替换M次重新采样N个数据点,其中N是原始数据集的总大小,M可以是任何值(通常是几百或更多).N不是任意的.如果N未设置为完整数据集大小,您将获得有偏差的统计信息. (5认同)

AGS*_*AGS 17

没有任何额外的包:

true_Y = c(1,1,1,1,2,1,2,1,2,2)
probs = c(1,0.999,0.999,0.973,0.568,0.421,0.382,0.377,0.146,0.11)

getROC_AUC = function(probs, true_Y){
    probsSort = sort(probs, decreasing = TRUE, index.return = TRUE)
    val = unlist(probsSort$x)
    idx = unlist(probsSort$ix)  

    roc_y = true_Y[idx];
    stack_x = cumsum(roc_y == 2)/sum(roc_y == 2)
    stack_y = cumsum(roc_y == 1)/sum(roc_y == 1)    

    auc = sum((stack_x[2:length(roc_y)]-stack_x[1:length(roc_y)-1])*stack_y[2:length(roc_y)])
    return(list(stack_x=stack_x, stack_y=stack_y, auc=auc))
}

aList = getROC_AUC(probs, true_Y) 

stack_x = unlist(aList$stack_x)
stack_y = unlist(aList$stack_y)
auc = unlist(aList$auc)

plot(stack_x, stack_y, type = "l", col = "blue", xlab = "False Positive Rate", ylab = "True Positive Rate", main = "ROC")
axis(1, seq(0.0,1.0,0.1))
axis(2, seq(0.0,1.0,0.1))
abline(h=seq(0.0,1.0,0.1), v=seq(0.0,1.0,0.1), col="gray", lty=3)
legend(0.7, 0.3, sprintf("%3.3f",auc), lty=c(1,1), lwd=c(2.5,2.5), col="blue", title = "AUC")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Ben*_*Ben 7

我发现这里的一些解决方案很慢和/或令人困惑(有些解决方案没有正确处理关系)所以我在我的R包mltools中编写了我自己data.table的函数auc_roc().

library(data.table)
library(mltools)

preds <- c(.1, .3, .3, .9)
actuals <- c(0, 0, 1, 1)

auc_roc(preds, actuals)  # 0.875

auc_roc(preds, actuals, returnDT=TRUE)
   Pred CountFalse CountTrue CumulativeFPR CumulativeTPR AdditionalArea CumulativeArea
1:  0.9          0         1           0.0           0.5          0.000          0.000
2:  0.3          1         1           0.5           1.0          0.375          0.375
3:  0.1          1         0           1.0           1.0          0.500          0.875
Run Code Online (Sandbox Code Playgroud)


aru*_*run 6

结合来自ISL 9.6.3 ROC Curves和 @J 的代码。Won. 对这个问题的回答以及其他一些地方,下面绘制了 ROC 曲线并在图的右下角打印了 AUC。

下面probs是二元分类预测概率的数字向量,test$label包含测试数据的真实标签。

require(ROCR)
require(pROC)

rocplot <- function(pred, truth, ...) {
  predob = prediction(pred, truth)
  perf = performance(predob, "tpr", "fpr")
  plot(perf, ...)
  area <- auc(truth, pred)
  area <- format(round(area, 4), nsmall = 4)
  text(x=0.8, y=0.1, labels = paste("AUC =", area))

  # the reference x=y line
  segments(x0=0, y0=0, x1=1, y1=1, col="gray", lty=2)
}

rocplot(probs, test$label, col="blue")
Run Code Online (Sandbox Code Playgroud)

这给出了这样的情节:

在此处输入图片说明


Kam*_*ski 5

您可以在Miron Kursa的此博客文章中了解有关AUROC的更多信息:

https://mbq.me/blog/augh-roc/

他为AUROC提供了快速功能:

# By Miron Kursa https://mbq.me
auroc <- function(score, bool) {
  n1 <- sum(!bool)
  n2 <- sum(bool)
  U  <- sum(rank(score)[!bool]) - n1 * (n1 + 1) / 2
  return(1 - U / n1 / n2)
}
Run Code Online (Sandbox Code Playgroud)

让我们测试一下:

set.seed(42)
score <- rnorm(1e3)
bool  <- sample(c(TRUE, FALSE), 1e3, replace = TRUE)

pROC::auc(bool, score)
mltools::auc_roc(score, bool)
ROCR::performance(ROCR::prediction(score, bool), "auc")@y.values[[1]]
auroc(score, bool)

0.51371668847094
0.51371668847094
0.51371668847094
0.51371668847094
Run Code Online (Sandbox Code Playgroud)

auroc()pROC::auc()和快100倍computeAUC()

auroc()mltools::auc_roc()和快10倍ROCR::performance()

print(microbenchmark(
  pROC::auc(bool, score),
  computeAUC(score[bool], score[!bool]),
  mltools::auc_roc(score, bool),
  ROCR::performance(ROCR::prediction(score, bool), "auc")@y.values,
  auroc(score, bool)
))

Unit: microseconds
                                                             expr       min
                                           pROC::auc(bool, score) 21000.146
                            computeAUC(score[bool], score[!bool]) 11878.605
                                    mltools::auc_roc(score, bool)  5750.651
 ROCR::performance(ROCR::prediction(score, bool), "auc")@y.values  2899.573
                                               auroc(score, bool)   236.531
         lq       mean     median        uq        max neval  cld
 22005.3350 23738.3447 22206.5730 22710.853  32628.347   100    d
 12323.0305 16173.0645 12378.5540 12624.981 233701.511   100   c 
  6186.0245  6495.5158  6325.3955  6573.993  14698.244   100  b  
  3019.6310  3300.1961  3068.0240  3237.534  11995.667   100 ab  
   245.4755   253.1109   251.8505   257.578    300.506   100 a   
Run Code Online (Sandbox Code Playgroud)

  • 对于更大的样本量,`bigstatsr::AUC()` 甚至更快(用 C++ 实现)。免责声明:我是作者。 (2认同)