I'm currently using naiveBayes from {e1071}. My response is simply a prediction based on my independent variables. Is there a way to get the probability for each possible prediction as a response with naiveBayes? So if I was trying to predict the outcome of a race, the predicted results would look like this :

Name    position  prob
Tom          1    0.1
Tom          2    0.2
Tom          3    0.7
Jim          1    0.2
Jim          2    0.5
Jim          3    0.3
Elizabeth    1    0.8
Elizabeth    2    0.1
Elizabeth    3    0.1

As opposed to just

Name        position
Tom            3
Jim            2
Elizabeth      1

If this is not possible with Naive Bayes, are there any other standard models that would support this? Like a Random Forest for instance? I would appreciate any guidance/literature on the matter.


1 Answer 1


You just need to set type = "raw" in the predict object.


fitted = naiveBayes(Species~., data = iris)
pred = predict(fitted, iris, "raw")
  • $\begingroup$ Thank you @Firebug. Just a few follow up questions. What other values can be given to the type argument? I couldn't find much here: stat.ethz.ch/R-manual/R-devel/library/stats/html/…. Also will the prediction always be given with the responses as features and probabilities as observations? (in which case I would just have to pivot it right?). Thanks again $\endgroup$
    – jgozal
    May 31, 2016 at 0:06
  • $\begingroup$ @jgozal the way R documentation is structured you should actually look at the packages in question, so look for naiveBayes in the e1071 manual. The only types really are classes and raw. And yeah, all R predict functions I know will output predicted probabilities in that format. $\endgroup$
    – Firebug
    May 31, 2016 at 1:21
  • $\begingroup$ ah interesting. I wasn't aware e1071 and R packages in general could introduce arguments into stats functions. Thank you! $\endgroup$
    – jgozal
    May 31, 2016 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.