0
$\begingroup$

I used R to predict the event A = "yes" given certain parameters for B1, B2. The results are P(A = "yes"|B1 = "a", B2 = "b") = 0.88. And P(A = "no"|B1 = "a", B2 = "b") = 0.12. But when I did it in Excel, I got P(A = "yes"|B1 = "a", B2 = "b") = 0.81. It cannot be right as all other conditional probabilities in R and Excel are precisely the same. What formula do you we use in R to calculate P(A = "yes"|B1 = "a", B2 = "b"). I used 1. Laplace smoothing (k = 1) So I used (count +k)/(N + k * |class|) for prior probabilities and added 1 to all counts prior to calculating conditional probabilities. 2. I used Bayes' Thm. P(A = "yes" | B1 = "a", B2 = "b") = (P(B1 = "a")|A= "yes") * P(B2 = "b"|A = "yes") * P(A = "yes"))/P(B1 = "a", B2 = "b"), where P(B1 = "b1", B2 = "b2") = P(B1 = "a"|A= "yes") * P(B2 = "b"|A = "yes") * P(A = "yes") + P(B1 = "a"|A= "no") * P(B2 = "b"|A = "no") * P(A = "no")

What am I doing wrong? Why are my answers not the same?

$\endgroup$
6
  • 1
    $\begingroup$ could you please reformat your text to be easier to read, right now it's really hard to parse. $\endgroup$ Nov 9, 2017 at 7:04
  • 1
    $\begingroup$ Also "I used R" is not really sufficient to understand what were you doing given the fact that there is >10 000 R packages. $\endgroup$
    – Tim
    Nov 9, 2017 at 7:21
  • $\begingroup$ I used the naive bayes package to predict the probability: $\endgroup$
    – Yelena
    Nov 9, 2017 at 16:20
  • $\begingroup$ classifier <- naiveBayes(train1, train1$Conversion,laplace=1) $\endgroup$
    – Yelena
    Nov 9, 2017 at 16:20
  • $\begingroup$ predictions_1 <- predict(classifier,test1,type="raw") # Predict classes $\endgroup$
    – Yelena
    Nov 9, 2017 at 16:20

1 Answer 1

1
$\begingroup$

The answer is that when we calculate the posterior probability with Laplace smoothing, Laplace smoothing with k= 1 is applied only to conditional probabilities but not prior probabilities (since they are not zeros). That is the reason why the answer was a bit off.

$\endgroup$

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.