As seen below, the results of Naive Bayes Classifier is displayed. In order to achieve this, please do the following:
Since the Naive Bayes package is not installed, the following two commands will install and load it:
- install.packages("e1071")
- library(e1071)
Then, the following command will calculate NBC:
classifier<- naiveBayes(disease ~ weight + BP, mydata, laplace = 0, subset, na.action = na.pass)
The last command is explained as follow:
- In the naiveBayes command, it is important to note that the results are being saves as classifier (classifier<-)
Then, within the command, you will place the formula, (disease ~ BP + weight) where ~ means =.
After the comma, you tell the command the data you're basing the calculation on.
The laplace=0 disables Laplace smoothing. The number can be increased if Laplace smoothing is desired.
subset is for data given in a data frame, an index vector specifying the cases to be used in the training sample
na.pass is a function to specify the action to be taken if NAs are found. The default action is not to count them for the computation of the probability factors. An alternative is na.omit, which leads to rejection of cases with missing values on any required variable.
Results:
Naive Bayes Classifier for Discrete Predictors
Call:
naiveBayes.default(x = X, y = Y, laplace = laplace)
A-priori probabilities:
Y
0 1
0.47 0.53
Conditional probabilities:
weight
Y large small
0 0.06382979 0.93617021
1 0.75471698 0.24528302
BP
Y high low
0 0.12765957 0.87234043
1 0.01886792 0.98113208
P.S. In order for this to work properly, you must have a categorical dataset. The following links and attached files should work as a resource and a review:
http://en.wikipedia.org/wiki/Naive_Bayes_classifier
https://www.stat.berkeley.edu/~spector/R.pdf
http://ugrad.stat.ubc.ca/R/library/e107 ... Bayes.html
http://stackoverflow.com/questions/2009 ... ifier-in-r
https://stat.ethz.ch/R-manual/R-devel/l ... ct.lm.html
http://ugrad.stat.ubc.ca/R/library/e107 ... Bayes.html
http://www.stat.berkeley.edu/~s133/factors.html