Naive Bayes in R example Iris Data

Created by

Rischan Mafrur

Chonnam National University of South Korea

http://rischanlab.github.io

May 26, 2014

Before you use this source you need install package e1071,caret, you can install package in R using command "install.packages("packagename")"

First we need to load the Library

library("caret")
## Loading required package: lattice
## Loading required package: ggplot2

We can show the iris data with this command, just type "iris" for show the all data :

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Or we can use command "names" for show the label/column names

names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
## [5] "Species"

In this we assign the data from column 1-4 (features) to variable x, and the class to variable y

x = iris[,-5]
y = iris$Species

Creating the model with cross validation =10

model = train(x,y,'nb',trControl=trainControl(method='cv',number=10))
## Loading required package: klaR
## Loading required package: MASS

Show the summary model, just type "model"

model
## Naive Bayes 
## 
## 150 samples
##   4 predictors
##   3 classes: 'setosa', 'versicolor', 'virginica' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## 
## Summary of sample sizes: 135, 135, 135, 135, 135, 135, ... 
## 
## Resampling results across tuning parameters:
## 
##   usekernel  Accuracy  Kappa  Accuracy SD  Kappa SD
##   FALSE      1         0.9    0.04         0.07    
##   TRUE       1         0.9    0.03         0.05    
## 
## Tuning parameter 'fL' was held constant at a value of 0
## Accuracy was used to select the optimal model using  the largest value.
## The final values used for the model were fL = 0 and usekernel = TRUE.

use predict for getting prediction value, and result class:

predict(model$finalModel,x)
## $class
##   [1] setosa     setosa     setosa     setosa     setosa     setosa    
##   [7] setosa     setosa     setosa     setosa     setosa     setosa    
##  [13] setosa     setosa     setosa     setosa     setosa     setosa    
##  [19] setosa     setosa     setosa     setosa     setosa     setosa    
##  [25] setosa     setosa     setosa     setosa     setosa     setosa    
##  [31] setosa     setosa     setosa     setosa     setosa     setosa    
##  [37] setosa     setosa     setosa     setosa     setosa     setosa    
##  [43] setosa     setosa     setosa     setosa     setosa     setosa    
##  [49] setosa     setosa     versicolor versicolor versicolor versicolor
##  [55] versicolor versicolor versicolor versicolor versicolor versicolor
##  [61] versicolor versicolor versicolor versicolor versicolor versicolor
##  [67] versicolor versicolor versicolor versicolor virginica  versicolor
##  [73] versicolor versicolor versicolor versicolor versicolor virginica 
##  [79] versicolor versicolor versicolor versicolor versicolor virginica 
##  [85] versicolor versicolor versicolor versicolor versicolor versicolor
##  [91] versicolor versicolor versicolor versicolor versicolor versicolor
##  [97] versicolor versicolor versicolor versicolor virginica  virginica 
## [103] virginica  virginica  virginica  virginica  versicolor virginica 
## [109] virginica  virginica  virginica  virginica  virginica  virginica 
## [115] virginica  virginica  virginica  virginica  virginica  versicolor
## [121] virginica  virginica  virginica  virginica  virginica  virginica 
## [127] virginica  virginica  virginica  virginica  virginica  virginica 
## [133] virginica  versicolor virginica  virginica  virginica  virginica 
## [139] virginica  virginica  virginica  virginica  virginica  virginica 
## [145] virginica  virginica  virginica  virginica  virginica  virginica 
## Levels: setosa versicolor virginica
## 
## $posterior
##           setosa versicolor virginica
##   [1,] 1.000e+00  3.122e-09 8.989e-11
##   [2,] 1.000e+00  4.953e-08 1.362e-09
##   [3,] 1.000e+00  1.950e-08 1.153e-09
##   [4,] 1.000e+00  1.146e-08 6.617e-10
##   [5,] 1.000e+00  8.840e-10 8.567e-11
##   [6,] 1.000e+00  3.819e-09 5.966e-09
##   [7,] 1.000e+00  7.394e-09 6.703e-10
##   [8,] 1.000e+00  5.312e-09 1.920e-10
##   [9,] 1.000e+00  6.502e-09 3.194e-10
##  [10,] 1.000e+00  1.732e-07 5.532e-09
##  [11,] 1.000e+00  1.234e-09 4.373e-10
##  [12,] 1.000e+00  6.937e-09 4.553e-10
##  [13,] 1.000e+00  2.398e-07 8.627e-09
##  [14,] 1.000e+00  1.001e-07 5.967e-09
##  [15,] 1.000e+00  1.005e-08 1.607e-08
##  [16,] 1.000e+00  2.410e-08 1.560e-09
##  [17,] 1.000e+00  2.068e-09 3.230e-09
##  [18,] 1.000e+00  1.262e-08 3.634e-10
##  [19,] 1.000e+00  1.083e-08 6.921e-08
##  [20,] 1.000e+00  2.086e-10 4.810e-10
##  [21,] 1.000e+00  9.294e-08 2.352e-09
##  [22,] 1.000e+00  1.160e-09 3.870e-10
##  [23,] 1.000e+00  4.305e-09 1.046e-09
##  [24,] 1.000e+00  1.208e-06 2.530e-08
##  [25,] 1.000e+00  3.485e-08 2.288e-09
##  [26,] 1.000e+00  8.860e-08 1.755e-09
##  [27,] 1.000e+00  3.616e-08 1.307e-09
##  [28,] 1.000e+00  5.101e-09 1.046e-10
##  [29,] 1.000e+00  1.097e-08 1.859e-10
##  [30,] 1.000e+00  1.882e-08 1.113e-09
##  [31,] 1.000e+00  4.348e-08 1.817e-09
##  [32,] 1.000e+00  1.215e-07 3.075e-09
##  [33,] 1.000e+00  1.234e-09 6.099e-11
##  [34,] 1.000e+00  1.317e-09 4.680e-11
##  [35,] 1.000e+00  3.070e-08 9.806e-10
##  [36,] 1.000e+00  9.547e-08 2.581e-09
##  [37,] 1.000e+00  3.791e-08 1.661e-09
##  [38,] 1.000e+00  4.599e-09 6.184e-10
##  [39,] 1.000e+00  6.577e-09 3.821e-10
##  [40,] 1.000e+00  6.711e-09 1.598e-10
##  [41,] 1.000e+00  1.707e-08 7.462e-10
##  [42,] 1.000e+00  3.444e-07 3.593e-09
##  [43,] 1.000e+00  2.420e-09 1.919e-10
##  [44,] 1.000e+00  1.178e-07 5.150e-09
##  [45,] 1.000e+00  1.765e-09 4.069e-09
##  [46,] 1.000e+00  1.719e-07 6.183e-09
##  [47,] 1.000e+00  8.512e-11 1.963e-10
##  [48,] 1.000e+00  6.806e-09 4.614e-10
##  [49,] 1.000e+00  8.054e-10 2.034e-10
##  [50,] 1.000e+00  1.083e-08 3.441e-10
##  [51,] 5.773e-08  8.752e-01 1.248e-01
##  [52,] 2.167e-08  9.441e-01 5.594e-02
##  [53,] 3.569e-08  6.419e-01 3.581e-01
##  [54,] 6.349e-07  1.000e+00 9.469e-06
##  [55,] 2.765e-09  9.427e-01 5.733e-02
##  [56,] 3.435e-07  9.983e-01 1.704e-03
##  [57,] 5.440e-08  6.776e-01 3.224e-01
##  [58,] 3.468e-05  1.000e+00 2.573e-07
##  [59,] 4.949e-09  9.852e-01 1.478e-02
##  [60,] 2.292e-06  1.000e+00 1.663e-05
##  [61,] 1.974e-05  1.000e+00 4.397e-08
##  [62,] 6.261e-07  9.971e-01 2.921e-03
##  [63,] 8.587e-08  1.000e+00 8.413e-07
##  [64,] 1.796e-08  9.676e-01 3.241e-02
##  [65,] 2.894e-06  1.000e+00 1.620e-05
##  [66,] 1.570e-08  9.826e-01 1.735e-02
##  [67,] 1.926e-06  9.932e-01 6.804e-03
##  [68,] 2.775e-07  1.000e+00 4.831e-06
##  [69,] 1.406e-09  9.942e-01 5.764e-03
##  [70,] 2.792e-07  1.000e+00 8.656e-07
##  [71,] 3.910e-06  1.892e-01 8.108e-01
##  [72,] 9.899e-09  9.998e-01 1.864e-04
##  [73,] 7.104e-10  8.375e-01 1.625e-01
##  [74,] 1.303e-08  9.954e-01 4.573e-03
##  [75,] 4.382e-09  9.970e-01 3.031e-03
##  [76,] 8.746e-09  9.849e-01 1.509e-02
##  [77,] 4.580e-09  9.008e-01 9.921e-02
##  [78,] 1.487e-08  9.858e-02 9.014e-01
##  [79,] 1.024e-07  9.847e-01 1.534e-02
##  [80,] 4.266e-07  1.000e+00 4.841e-07
##  [81,] 1.300e-06  1.000e+00 1.821e-07
##  [82,] 1.808e-06  1.000e+00 9.432e-08
##  [83,] 2.332e-07  1.000e+00 1.208e-05
##  [84,] 8.811e-08  4.873e-01 5.127e-01
##  [85,] 5.329e-06  9.965e-01 3.485e-03
##  [86,] 2.941e-06  9.183e-01 8.169e-02
##  [87,] 1.890e-08  8.645e-01 1.355e-01
##  [88,] 1.131e-09  9.990e-01 1.002e-03
##  [89,] 1.885e-06  9.998e-01 1.513e-04
##  [90,] 1.944e-07  1.000e+00 2.738e-05
##  [91,] 1.838e-07  9.998e-01 1.678e-04
##  [92,] 2.684e-08  9.784e-01 2.160e-02
##  [93,] 7.517e-08  1.000e+00 2.769e-05
##  [94,] 4.864e-05  1.000e+00 1.026e-07
##  [95,] 2.184e-07  9.998e-01 2.488e-04
##  [96,] 1.935e-06  9.998e-01 1.485e-04
##  [97,] 7.830e-07  9.996e-01 3.603e-04
##  [98,] 3.522e-09  9.979e-01 2.111e-03
##  [99,] 2.627e-05  1.000e+00 1.170e-06
## [100,] 4.187e-07  9.998e-01 1.609e-04
## [101,] 8.943e-08  1.262e-06 1.000e+00
## [102,] 4.982e-07  2.784e-02 9.722e-01
## [103,] 2.384e-08  3.190e-07 1.000e+00
## [104,] 5.406e-09  2.071e-04 9.998e-01
## [105,] 1.008e-08  5.707e-07 1.000e+00
## [106,] 9.294e-08  1.235e-08 1.000e+00
## [107,] 7.723e-06  9.540e-01 4.600e-02
## [108,] 4.013e-08  3.421e-05 1.000e+00
## [109,] 1.102e-09  1.316e-04 9.999e-01
## [110,] 1.061e-06  1.289e-07 1.000e+00
## [111,] 1.607e-08  4.582e-04 9.995e-01
## [112,] 1.099e-09  2.028e-03 9.980e-01
## [113,] 1.220e-08  4.522e-06 1.000e+00
## [114,] 6.717e-07  9.693e-03 9.903e-01
## [115,] 1.996e-06  9.822e-04 9.990e-01
## [116,] 1.798e-08  3.245e-05 1.000e+00
## [117,] 7.871e-09  6.025e-04 9.994e-01
## [118,] 8.035e-07  4.610e-11 1.000e+00
## [119,] 1.115e-08  1.822e-08 1.000e+00
## [120,] 1.022e-07  9.462e-01 5.380e-02
## [121,] 3.123e-08  3.546e-07 1.000e+00
## [122,] 5.289e-06  1.517e-02 9.848e-01
## [123,] 3.583e-08  4.116e-08 1.000e+00
## [124,] 1.441e-09  8.278e-02 9.172e-01
## [125,] 3.394e-08  3.175e-07 1.000e+00
## [126,] 5.597e-08  2.185e-05 1.000e+00
## [127,] 4.674e-09  1.622e-01 8.378e-01
## [128,] 5.947e-08  1.004e-01 8.996e-01
## [129,] 3.101e-09  1.443e-06 1.000e+00
## [130,] 6.123e-08  3.401e-04 9.997e-01
## [131,] 1.389e-08  2.977e-06 1.000e+00
## [132,] 8.767e-07  2.312e-10 1.000e+00
## [133,] 3.229e-09  1.502e-06 1.000e+00
## [134,] 5.830e-09  5.464e-01 4.536e-01
## [135,] 2.148e-08  2.849e-02 9.715e-01
## [136,] 4.877e-08  3.680e-09 1.000e+00
## [137,] 6.368e-08  9.931e-07 1.000e+00
## [138,] 1.042e-08  5.448e-04 9.995e-01
## [139,] 5.004e-07  2.024e-01 7.976e-01
## [140,] 1.996e-08  1.166e-05 1.000e+00
## [141,] 2.000e-08  1.295e-06 1.000e+00
## [142,] 2.162e-08  1.231e-04 9.999e-01
## [143,] 4.982e-07  2.784e-02 9.722e-01
## [144,] 3.349e-08  4.857e-07 1.000e+00
## [145,] 7.669e-08  7.173e-07 1.000e+00
## [146,] 1.158e-08  8.915e-05 9.999e-01
## [147,] 7.841e-10  2.133e-02 9.787e-01
## [148,] 7.967e-09  3.438e-04 9.997e-01
## [149,] 5.762e-08  1.351e-05 1.000e+00
## [150,] 1.369e-06  5.710e-02 9.429e-01

The last one, we need to know how many error classified, so we need to compare the result of prediction with the class/iris species.

table(predict(model$finalModel,x)$class,y)
##             y
##              setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         47         3
##   virginica       0          3        47

If you want to plot the features with Naive Bayes, you can use this command:

naive_iris <- NaiveBayes(iris$Species ~ ., data = iris)
plot(naive_iris)
plot of chunk unnamed-chunk-9
plot of chunk unnamed-chunk-9
plot of chunk unnamed-chunk-9
plot of chunk unnamed-chunk-9