# Install and load the required package
#install.packages("e1071")
library(e1071)
# Create the dataset
data <- data.frame(
Charges_Filed = c('y', 'n', 'n', 'n', 'n', 'n', 'y', 'y', 'n', 'y'),
Company_Size = c('small', 'small', 'large', 'large', 'small', 'small', 'small', 'large', 'large', 'large'),
Status = c('truthful', 'truthful', 'truthful', 'truthful', 'truthful', 'truthful', 'fraudulent', 'fraudulent', 'fraudulent', 'fraudulent')
)
# Fit the naive Bayes model without Laplace smoothing
model <- naiveBayes(Status ~ ., data = data)
model
##
## Naive Bayes Classifier for Discrete Predictors
##
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
##
## A-priori probabilities:
## Y
## fraudulent truthful
## 0.4 0.6
##
## Conditional probabilities:
## Charges_Filed
## Y n y
## fraudulent 0.2500000 0.7500000
## truthful 0.8333333 0.1666667
##
## Company_Size
## Y large small
## fraudulent 0.7500000 0.2500000
## truthful 0.3333333 0.6666667
# Fit the naive Bayes model with Laplace smoothing
model2 <- naiveBayes(Status ~ ., data = data,laplace =1)
model2
##
## Naive Bayes Classifier for Discrete Predictors
##
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
##
## A-priori probabilities:
## Y
## fraudulent truthful
## 0.4 0.6
##
## Conditional probabilities:
## Charges_Filed
## Y n y
## fraudulent 0.5000000 1.0000000
## truthful 1.0000000 0.3333333
##
## Company_Size
## Y large small
## fraudulent 1.0000000 0.5000000
## truthful 0.5000000 0.8333333
# Predict the probabilities for the dataset
predictions <- predict(model, data, type = "raw")
predictions2 <- predict(model2, data, type = "raw")
# Print the predictions
print(predictions) # Without Laplace
## fraudulent truthful
## [1,] 0.52941176 0.4705882
## [2,] 0.06976744 0.9302326
## [3,] 0.31034483 0.6896552
## [4,] 0.31034483 0.6896552
## [5,] 0.06976744 0.9302326
## [6,] 0.06976744 0.9302326
## [7,] 0.52941176 0.4705882
## [8,] 0.87096774 0.1290323
## [9,] 0.31034483 0.6896552
## [10,] 0.87096774 0.1290323
print(predictions2) # With Laplace
## fraudulent truthful
## [1,] 0.5454545 0.4545455
## [2,] 0.1666667 0.8333333
## [3,] 0.4000000 0.6000000
## [4,] 0.4000000 0.6000000
## [5,] 0.1666667 0.8333333
## [6,] 0.1666667 0.8333333
## [7,] 0.5454545 0.4545455
## [8,] 0.8000000 0.2000000
## [9,] 0.4000000 0.6000000
## [10,] 0.8000000 0.2000000