用R表示线性判别分析中的Roc曲线

时间:2017-01-08 14:33:25

标签: r machine-learning lda roc auc

我想计算Roc曲线,然后计算线性判别模型的AUC。你知道我怎么能这样做吗?这里有代码:

##LDA
require(MASS)
library(MASS)
lda.fit = lda(Negative ~., trainSparse)
lda.fit
plot(lda.fit)
###prediction on the test set
lda.pred=predict(lda.fit,testSparse)
table(testSparse$Negative,lda.pred$class)

2 个答案:

答案 0 :(得分:1)

试试这个:

library(ROCR)
# choose the posterior probability column carefully, it may be 
# lda.pred$posterior[,1] or lda.pred$posterior[,2], depending on your factor levels 
pred <- prediction(lda.pred$posterior[,2], testSparse$Negative) 
perf <- performance(pred,"tpr","fpr")
plot(perf,colorize=TRUE)

enter image description here

答案 1 :(得分:0)

我会这样做。 因为在这里您还获得了AUC量度,而且看起来超级苗条

install.packages("pROC")

library(pROC)

par(pty = "s")

roc(testSparse$Negative,lda.pred$posterior[,2],plot=TRUE, legacy.axes = TRUE, 
percent =TRUE, xlab="False Positive Percentage", ylab="True Positive Percentage")