Roc curve in linear discriminant analysis with R

9.9k views Asked by At

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

There are 2 answers

0
Adam Brüel-Andersen On

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")
3
Sandipan Dey On

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)

enter image description here