# Naive Bayes with Laplace Smoothing (k = 1)

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?

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

## 1 Answer

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.