0
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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

require(e1071)

fitted = naiveBayes(Species~., data = iris)
pred = predict(fitted, iris, "raw")
pred
| cite | improve this answer | |
$\endgroup$
  • $\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 '16 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 '16 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 '16 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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