简体   繁体   中英

Roc curve in linear discriminant analysis with R

I want to compute the Roc curve and then the AUC from the linear discriminant model. Do you know how can I do this? here there is the code:

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

Simply try this:

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)

在此处输入图片说明

I would do it like this. Because here you get AUC measure as well + looks super lean and slick

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM