Naive Bayes: In the continuation of Naive Bayes algorithm, let us look into the basic codes in R to implement Naive Bayes. We will start with installation of packages required for Naive Bayes then move onto the commands required for the implementation of algorithm.
We will try to predict probability of default/Non-Default using Naïve Bayes algorithm. Please download the data provided in the data repository section.
Step 1. Set up a working library. Setting up a working directory makes life very easy. We can refer to any file saved in working library without mentioning location again and again.
Step 2: Import the data set into R-Studio using following commands.
Step 3. : Install the package required for Naïve Bayes. We would install “e1071” for linear regression.
Step 4: Our data set has 5 variables but for analysis we would use just last three variables. So we would select last tree variable using following commands.
Step 5: Change the Response variable to Factors
Step 6: Develop Naive Bayes algorithm using following commands
Step 7. Predicting the output
Step 8: Checking the accuracy with confusion matrix.