计算R中的AUC?
给定一个分数向量和一个实际类标签向量,如何计算R语言或简单英语中二进制分类器的单数AUC度量标准?计算R中的AUC?
第9页的"AUC: a Better Measure..."似乎需要知道类标签,这里是an example in MATLAB,我不明白
R(Actual == 1))
因为R(不要与R语言混淆)是指一个向量,但用作函数?
正如其他人所提到的,您可以使用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
将给AUC的近似。您也可以估算AUC的方差通过引导:
> aucs = replicate(1000,mean(sample(pos.scores,1000,replace=T) > sample(neg.scores,1000,replace=T)))
The ROCR package将计算AUC其他统计中:
auc.tmp <- performance(pred,"auc"); auc <- as.numeric([email protected])
与包pROC
可以使用函数auc()
像是从帮助页面下面的例子:
> data(aSAH)
>
> # Syntax (response, predictor):
> auc(aSAH$outcome, aSAH$s100b)
Area under the curve: 0.7314
除了Erik的响应线,你也应该能够直接从pos.scores和NEG比较所有可能的对数值计算的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)
肯定比样品的方法效率较低或PROC :: AUC,但比以前更稳定,需要比后者少安装。
相关:当我尝试这个时,它给出了与pROC的值类似的结果,但不完全相同(关闭0.02左右);结果更接近样本方法,N很高。如果有人有想法,为什么我可能会感兴趣。
无需任何额外的软件包:
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")
从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")
这给了这样一个情节:
我找到了一些解决方案在这里的是缓慢的和/或混乱(和他们中的一些不正确处理的关系),所以我在我的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
当前最高票答案是不正确的,因为它无视关系。当正面和负面分数相等时,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))
}
对于其他人谁不知道,显然AUC是我用过的“区域在[受试者工作特征(http://en.wikipedia.org/wiki/Receiver_operating_characteristic)曲线” – Justin 2011-02-04 21:30:40