How to do it...

Perform the following steps to classify the churn data with the Naïve Bayes classifier:

  1. Load the e1071 library and employ the naiveBayes function to build the classifier:
        > library(e1071) 
        > classifier=naiveBayes(trainset[, !names(trainset)
%in% c("churn")], trainset$churn)
  1. Type classifier to examine the function call, a-priori probability, and conditional probability:
> classifier
Output
Naive Bayes Classifier for Discrete Predictors

Call:
naiveBayes.default(x = trainset[, !names(trainset) %in% c("churn")],
y = trainset$churn)

A-priori probabilities:
trainset$churn
yes no
0.1477322 0.8522678

Conditional probabilities:
international_plan
trainset$churn no yes
yes 0.70467836 0.29532164
no 0.93512418 0.06487582
  1. Next, you can generate a classification table for the testing dataset:
        > bayes.table = table(predict(classifier, testset[,
!names(testset) %in% c("churn")]), testset$churn)
> bayes.table
Output
yes no
yes 68 45
no 73 832
  1. Lastly, you can generate a confusion matrix from the classification table:
> confusionMatrix(bayes.table)
Output
Confusion Matrix and Statistics


yes no
yes 68 45
no 73 832

Accuracy : 0.8841
95% CI : (0.8628, 0.9031)
No Information Rate : 0.8615
P-Value [Acc > NIR] : 0.01880

Kappa : 0.4701
Mcnemar's Test P-Value : 0.01294

Sensitivity : 0.4823
Specificity : 0.9487
Pos Pred Value : 0.6018
Neg Pred Value : 0.9193
Prevalence : 0.1385
Detection Rate : 0.0668
Detection Prevalence : 0.1110
Balanced Accuracy : 0.7155

'Positive' Class : yes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset