Using naive bayes to predict new values

Frits Verstraten picture Frits Verstraten · Apr 8, 2016 · Viewed 7.1k times · Source

I have a dataframe that looks like this

weather <- c("good", "good", "good", "bad", "bad", "good")
temp <- c("high", "low", "low", "high", "low", "low")
golf <- c("yes", "no", "yes", "no", "yes" , "no")
df <- data.frame(weather, temp, golf)

What I would like to do now is using the naive bayes method to get probability of this new dataset

df_new <- data.frame(weather = "good", temp = "low")

Therefore I try

library(e1071)
model <- naiveBayes(golf ~.,data=df)
predict(model, df_new)

But this gives me:

NO

Any idea how i can turn this into a probability?

Answer

phiver picture phiver · Apr 8, 2016

probabilities are returned if you use type = "raw"

predict(model, df_new, type = "raw")
no yes
[1,] 0.5 0.5

predict(model, df_new, type = "class")
[1] no
Levels: no yes