以R计算AUC?

以R计算AUC?,r,machine-learning,data-mining,auc,R,Machine Learning,Data Mining,Auc,给定分数向量和实际类标签向量,如何计算R语言或简单英语中二进制分类器的单个数字AUC度量 第9页似乎需要知道类标签,这是我不理解的地方 R(Actual == 1)) 因为R(不要与R语言混淆)定义为向量,但用作函数?将计算AUC和其他统计数据: auc.tmp <- performance(pred,"auc"); auc <- as.numeric(auc.tmp@y.values) auc.tmp使用软件包pROC可以使用函数auc(),如帮助页面中的示例所示: >

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

第9页似乎需要知道类标签,这是我不理解的地方

R(Actual == 1))
因为R(不要与R语言混淆)定义为向量,但用作函数?

将计算AUC和其他统计数据:

auc.tmp <- performance(pred,"auc"); auc <- as.numeric(auc.tmp@y.values)

auc.tmp使用软件包
pROC
可以使用函数
auc()
,如帮助页面中的示例所示:

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

我通常使用Diagnostimed包中的函数。我喜欢它生成的图形。AUC随其置信区间一起返回,图中也提到了AUC

ROC(classLabels,scores,Full=TRUE)

正如其他人提到的,您可以使用包计算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
将给出AUC的近似值。您还可以通过自举法估计AUC的方差:

> aucs = replicate(1000,mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T)))

按照erik的回答,您还应该能够通过比较pos.scores和neg.scores中所有可能的值对来直接计算ROC:

score.pairs <- merge(pos.scores, neg.scores)
names(score.pairs) <- c("pos.score", "neg.score")
sum(score.pairs$pos.score > score.pairs$neg.score) / nrow(score.pairs)

score.pairs无任何附加包:

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

结合来自的代码,以及@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")
require(ROCR)
要求(程序)

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

库(data.table)
图书馆(mltools)

preds目前得票最多的答案是不正确的,因为它忽略了关系。当阳性和阴性分数相等时,AUC应为0.5。下面是一个正确的例子

computeAUC <- function(pos.scores, neg.scores, n_sample=100000) {
  # Args:
  #   pos.scores: scores of positive observations
  #   neg.scores: scores of negative observations
  #   n_samples : number of samples to approximate AUC

  pos.sample <- sample(pos.scores, n_sample, replace=T)
  neg.sample <- sample(neg.scores, n_sample, replace=T)
  mean(1.0*(pos.sample > neg.sample) + 0.5*(pos.sample==neg.sample))
}

computeAUC在这篇博文中,您可以通过以下方式了解有关AUROC的更多信息:

他为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)
}
使用软件包计算AUC非常简单明了:

library(Metrics)

actual <- c(0, 0, 1, 1)
predicted <- c(.1, .3, .3, .9)

auc(actual, predicted)

0.875
库(度量)

实际对于任何不知道的人来说,显然AUC是我使用ROCR绘制性能的“曲线下面积”,但我不知道它如何计算“单个数字AUC度量”(来自原始问题)。
AUC.tmp对于我的测试数据集,你的复制值与@jonw的非常相似(是0.8504,你的是0.850591)除了我不需要安装pROC。感谢you@Andrew@eric这是个糟糕的回答。您不估计AUC的方差-您只估计重采样过程的方差。要说服自己,请尝试在
sample
中更改样本大小。。。除以10,你的方差乘以10。把它乘以10,你的方差除以10。这当然不是计算AUC方差的理想行为。此外,答案应注意,估计值与重复数一样好。转到无穷大,你就得到了实际的AUC。同意@Calimo,这不是引导。要进行引导,您必须对N个数据点进行重采样,替换次数为M次,其中N是原始数据集的总大小,M可以是任意值(通常为几百或更多)。N不是任意的。如果N未设置为完整数据集大小,则会得到有偏差的统计数据。我对所示的基本R方法有点不清楚。是否可以纯粹从混淆矩阵计算?在给定的混淆矩阵中,
pos.scores
neg.scores
是什么?不准确的一个来源是处理关系。从技术上讲,你应该考虑积极案例分数严格大于消极分数+1/2*的概率,前提是它们相等。如果所有分数都是唯一的,这不会是问题。如果复制粘贴此代码并在plot.window(…)中收到
错误:需要有限的“xlim”值,可能是因为标签为0-1,当@AGS使用标签1-2时,如果两个观测值具有相同的概率且观测顺序不是随机的,则不会给出真实的AUC。否则代码会很好很快。我不知道为什么这个解决方案对我的数据不起作用,到2016年7月20日,我的问题没有标准化到[0,1]以内。这个链接说,
包'DiagnosisMed'已从CRAN存储库中删除。
这个解决方案比pROC包中的auc()方法快得多!如果必须计算多类或多输出回归问题的auc分数,pROC包中的auc()方法会非常慢。对于较大的样本量,
bigstatsr::auc()
甚至更快(用C++实现)。免责声明:我是作者。
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
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   
library(Metrics)

actual <- c(0, 0, 1, 1)
predicted <- c(.1, .3, .3, .9)

auc(actual, predicted)

0.875