The HouseVotes84 dataset was used for applying the naive Bayes algorithm. library (e1071)
Naive Bayes Classifier for Discrete Predictors: we use again the Congressional Voting Records of 1984
Note refusals to vote have been treated as missing values!
data (HouseVotes84, package="mlbench") model <- naiveBayes(Class ~ ., data = HouseVotes84) head(HouseVotes84)
Class V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15
1 republican n y n y y y n n n y <NA> y y y n
2 republican n y n y y y n n n n n y y y n
3 democrat <NA> y y <NA> y y n n n n y n y y n
4 democrat n y y n <NA> y n n n n y n y n n
5 democrat y y y n y y n n n n y <NA> y y y
6 democrat n y y n y y n n n n n n y y y
V16
1 y
2 <NA>
3 n
4 y
5 y
6 y
predict the outcome of the first 20 records
predict(model, HouseVotes84[1:20,-1])
[1] republican republican republican democrat democrat democrat ## [7] republican republican republican democrat republican republican
[13] democrat democrat republican republican democrat democrat ## [19] republican democrat ## Levels: democrat republican
same but displaying posteriors
predict(model, HouseVotes84[1:20,-1], type = "raw")