Search code examples
rnaivebayesroc

ROC curve error


I have dataset like this

train <- sample(1:nrow(df), nrow(df)*0.80)
train <- df[train, ]
test <- df[-train, ]


NaiveBayes1 <-naiveBayes(purchased ~ .,data=train)
pre1 <- predict(NaiveBayes1,test,probability = TRUE)

library(pROC)
roc1 <- roc(test$purchased, pre1$posterior[,1])

And I get this error:

Error in pre1$posterior : $ operator is invalid for atomic vectors

I have done in the class using LDA and it worked but for naivebays, it doesn't work. Any help will be appreciated.


Solution

  • Change your last two lines from:

    pre1 <- predict(NaiveBayes1, test, probability = TRUE)
    roc1 <- roc(test$purchased, pre1$posterior[,1])
    

    To:

    pre1 <- predict(NaiveBayes1, test, type = "raw")
    roc1 <- roc(test$purchased, pre1[,1])