# Naive Bayes

## Initialization

Demo for binary data. First we generate some data. We have several binary covariates and a binary target variable y.

library(tidyverse)

set.seed(123)

x  = matrix(sample(0:1, 50, replace = TRUE), ncol = 5)
xf = map(data.frame(x), factor)
y  = sample(0:1, 10, prob = c(.25, .75), replace = TRUE)

## Comparison

We can use e1071 for comparison.

library(e1071)

m = naiveBayes(xf, y)

m

Naive Bayes Classifier for Discrete Predictors

Call:
naiveBayes.default(x = xf, y = y)

A-priori probabilities:
y
0   1
0.3 0.7

Conditional probabilities:
X1
y           0         1
0 0.6666667 0.3333333
1 0.5714286 0.4285714

X2
y           0         1
0 0.6666667 0.3333333
1 0.4285714 0.5714286

X3
y           0         1
0 0.6666667 0.3333333
1 0.5714286 0.4285714

X4
y           0         1
0 1.0000000 0.0000000
1 0.4285714 0.5714286

X5
y           0         1
0 0.3333333 0.6666667
1 0.8571429 0.1428571

Using base R for our model, we can easily obtain the ‘predictions’…

map(xf, function(var)
t(prop.table(table(' ' = var, y), margin = 2)))
$X1 y 0 1 0 0.6666667 0.3333333 1 0.5714286 0.4285714$X2

y           0         1
0 0.6666667 0.3333333
1 0.4285714 0.5714286

$X3 y 0 1 0 0.6666667 0.3333333 1 0.5714286 0.4285714$X4

y           0         1
0 1.0000000 0.0000000
1 0.4285714 0.5714286

\$X5

y           0         1
0 0.3333333 0.6666667
1 0.8571429 0.1428571

## Source

Original code available at https://github.com/m-clark/Miscellaneous-R-Code/blob/master/ModelFitting/naivebayes.R