library(conflicted)
library(dplyr)
library(tidyr)
library(ggplot2)
library(DiagrammeR)
library(DiagrammeRsvg)
library(caret)
## Loading required package: lattice
library(naivebayes)
## naivebayes 0.9.7 loaded
library(psych)
train_data <- read.csv("sensor_prepare_train_data.csv",stringsAsFactors = FALSE)
test_data <- read.csv("sensor_prepare_test_data.csv",stringsAsFactors = FALSE)
features_column <- c("Seismic_Max","Seismic_Min","Seismic_Mean","Seismic_Range","Gyro_Gx",
"Gyro_Gy","Gyro_Gz")
target_column <- c("Event_Actual")
train_data_features <- train_data[,features_column]
# Summary train data features
summary(train_data_features)
## Seismic_Max Seismic_Min Seismic_Mean Seismic_Range
## Min. :0.02802 Min. :-3.59999 Min. :-0.3224 Min. :0.7593
## 1st Qu.:2.75628 1st Qu.:-3.43122 1st Qu.: 0.6112 1st Qu.:5.5405
## Median :3.20628 Median :-3.17808 Median : 0.7429 Median :6.1985
## Mean :3.06429 Mean :-2.88286 Mean : 0.7419 Mean :5.9472
## 3rd Qu.:3.45941 3rd Qu.:-2.64375 3rd Qu.: 0.8763 3rd Qu.:6.6656
## Max. :3.59989 Max. : 0.00924 Max. : 1.5837 Max. :7.1999
## Gyro_Gx Gyro_Gy Gyro_Gz
## Min. :-9.7710 Min. :-10.4733 Min. : 7.809
## 1st Qu.:-7.6641 1st Qu.: -8.9160 1st Qu.:125.008
## Median :-2.7634 Median : -2.3206 Median :125.557
## Mean :-2.9722 Mean : -2.6277 Mean :125.496
## 3rd Qu.: 0.7634 3rd Qu.: 0.8855 3rd Qu.:126.107
## Max. : 8.7328 Max. : 13.0076 Max. :128.489
train_data_target <- train_data[,target_column]
#Summary train data target
summary(train_data_target)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 2.000 2.594 4.000 7.000
test_data_features <- test_data[,features_column]
# Summary test data features
summary(test_data_features)
## Seismic_Max Seismic_Min Seismic_Mean Seismic_Range
## Min. :0.5062 Min. :-3.60000 Min. :-0.07793 Min. :1.266
## 1st Qu.:2.7844 1st Qu.:-3.43122 1st Qu.: 0.62230 1st Qu.:5.513
## Median :3.2063 Median :-3.17809 Median : 0.74292 Median :6.188
## Mean :3.0661 Mean :-2.84109 Mean : 0.74299 Mean :5.907
## 3rd Qu.:3.4594 3rd Qu.:-2.67184 3rd Qu.: 0.87471 3rd Qu.:6.638
## Max. :3.5999 Max. :-0.02807 Max. : 1.38095 Max. :7.172
## Gyro_Gx Gyro_Gy Gyro_Gz
## Min. :-9.2824 Min. :-10.5038 Min. :123.3
## 1st Qu.:-7.6336 1st Qu.: -9.0687 1st Qu.:125.1
## Median :-3.8473 Median : -2.5038 Median :125.6
## Mean :-3.0997 Mean : -2.8223 Mean :125.7
## 3rd Qu.: 0.7634 3rd Qu.: 0.7634 3rd Qu.:126.2
## Max. : 8.3359 Max. : 12.7634 Max. :128.2
test_data_target <- test_data[,target_column]
#Summary test data target
summary(test_data_target)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 2.000 2.431 4.000 7.000
train_data$Event_Actual <- as.factor(train_data$Event_Actual)
train_data_target <- as.factor(train_data$Event_Actual)
table(train_data_target)
## train_data_target
## 0 1 2 3 4 5 6 7
## 1399 650 544 687 546 553 328 339
test_data$Event_Actual <- as.factor((test_data$Event_Actual))
test_data_target <- as.factor((test_data$Event_Actual))
table(test_data_target)
## test_data_target
## 0 1 2 3 4 5 6 7
## 400 143 139 171 135 118 65 86
GS_T0 <- Sys.time()
nb_model <- naive_bayes(Event_Actual~Seismic_Max+Seismic_Min+Seismic_Mean+Seismic_Range+Gyro_Gx+Gyro_Gy+Gyro_Gz,
data=train_data, usekernel =TRUE)
print(nb_model)
##
## ================================== Naive Bayes ==================================
##
## Call:
## naive_bayes.formula(formula = Event_Actual ~ Seismic_Max + Seismic_Min +
## Seismic_Mean + Seismic_Range + Gyro_Gx + Gyro_Gy + Gyro_Gz,
## data = train_data, usekernel = TRUE)
##
## ---------------------------------------------------------------------------------
##
## Laplace smoothing: 0
##
## ---------------------------------------------------------------------------------
##
## A priori probabilities:
##
## 0 1 2 3 4 5 6
## 0.27724931 0.12881490 0.10780816 0.13614744 0.10820452 0.10959176 0.06500198
## 7
## 0.06718193
##
## ---------------------------------------------------------------------------------
##
## Tables:
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::0 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (1399 obs.); Bandwidth 'bw' = 0.1036
##
## x y
## Min. :1.686 Min. :0.0000452
## 1st Qu.:2.235 1st Qu.:0.2162987
## Median :2.784 Median :0.3899749
## Mean :2.784 Mean :0.4546912
## 3rd Qu.:3.334 3rd Qu.:0.6410093
## Max. :3.883 Max. :1.2447272
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::1 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (650 obs.); Bandwidth 'bw' = 0.1205
##
## x y
## Min. :0.9601 Min. :0.0000574
## 1st Qu.:1.7105 1st Qu.:0.0047631
## Median :2.4608 Median :0.2796045
## Mean :2.4608 Mean :0.3328279
## 3rd Qu.:3.2112 3rd Qu.:0.5440228
## Max. :3.9615 Max. :1.0864367
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::2 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (544 obs.); Bandwidth 'bw' = 0.1231
##
## x y
## Min. :1.543 Min. :0.0001299
## 1st Qu.:2.150 1st Qu.:0.1453855
## Median :2.756 Median :0.3434161
## Mean :2.756 Mean :0.4116893
## 3rd Qu.:3.363 3rd Qu.:0.5872777
## Max. :3.969 Max. :1.1956715
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::3 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (687 obs.); Bandwidth 'bw' = 0.1106
##
## x y
## Min. :1.665 Min. :0.0003218
## 1st Qu.:2.232 1st Qu.:0.2155148
## Median :2.798 Median :0.3341064
## Mean :2.798 Mean :0.4406840
## 3rd Qu.:3.365 3rd Qu.:0.6415554
## Max. :3.932 Max. :1.2254525
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::4 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (546 obs.); Bandwidth 'bw' = 0.1165
##
## x y
## Min. :-0.1528 Min. :0.0000000
## 1st Qu.: 0.8728 1st Qu.:0.0000812
## Median : 1.8984 Median :0.0151050
## Mean : 1.8984 Mean :0.2434986
## 3rd Qu.: 2.9240 3rd Qu.:0.3889846
## Max. : 3.9495 Max. :1.1274510
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::5 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (553 obs.); Bandwidth 'bw' = 0.1177
##
## x y
## Min. :-0.3251 Min. :0.0000000
## 1st Qu.: 0.7444 1st Qu.:0.0000828
## Median : 1.8140 Median :0.0100025
## Mean : 1.8140 Mean :0.2334922
## 3rd Qu.: 2.8835 3rd Qu.:0.3817010
## Max. : 3.9530 Max. :1.1340721
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::6 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (328 obs.); Bandwidth 'bw' = 0.1305
##
## x y
## Min. :0.2273 Min. :0.0000041
## 1st Qu.:1.1683 1st Qu.:0.0038006
## Median :2.1093 Median :0.0704560
## Mean :2.1093 Mean :0.2653567
## 3rd Qu.:3.0503 3rd Qu.:0.3670381
## Max. :3.9913 Max. :1.3223233
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Max::7 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (339 obs.); Bandwidth 'bw' = 0.1242
##
## x y
## Min. :1.090 Min. :0.000107
## 1st Qu.:1.811 1st Qu.:0.018420
## Median :2.531 Median :0.239476
## Mean :2.531 Mean :0.346550
## 3rd Qu.:3.252 3rd Qu.:0.508001
## Max. :3.972 Max. :1.107783
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::0 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (1399 obs.); Bandwidth 'bw' = 0.1242
##
## x y
## Min. :-3.9727 Min. :0.0007928
## 1st Qu.:-2.8933 1st Qu.:0.0505252
## Median :-1.8140 Median :0.0844102
## Mean :-1.8140 Mean :0.2313771
## 3rd Qu.:-0.7347 3rd Qu.:0.2930763
## Max. : 0.3446 Max. :1.1027279
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::1 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (650 obs.); Bandwidth 'bw' = 0.181
##
## x y
## Min. :-4.1430 Min. :0.000818
## 1st Qu.:-2.9785 1st Qu.:0.049951
## Median :-1.8140 Median :0.092263
## Mean :-1.8140 Mean :0.214440
## 3rd Qu.:-0.6495 3rd Qu.:0.267633
## Max. : 0.5150 Max. :0.919930
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::2 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (544 obs.); Bandwidth 'bw' = 0.1661
##
## x y
## Min. :-4.0984 Min. :0.0006893
## 1st Qu.:-2.9562 1st Qu.:0.0370405
## Median :-1.8140 Median :0.0797548
## Mean :-1.8140 Mean :0.2186328
## 3rd Qu.:-0.6718 3rd Qu.:0.3082309
## Max. : 0.4703 Max. :0.9119156
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::3 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (687 obs.); Bandwidth 'bw' = 0.1253
##
## x y
## Min. :-3.9759 Min. :0.0004757
## 1st Qu.:-2.8950 1st Qu.:0.0254624
## Median :-1.8140 Median :0.0600794
## Mean :-1.8140 Mean :0.2310257
## 3rd Qu.:-0.7331 3rd Qu.:0.3407021
## Max. : 0.3479 Max. :1.1176282
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::4 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (546 obs.); Bandwidth 'bw' = 0.1392
##
## x y
## Min. :-4.0177 Min. :0.0003663
## 1st Qu.:-2.9065 1st Qu.:0.0337531
## Median :-1.7954 Median :0.0647263
## Mean :-1.7954 Mean :0.2247437
## 3rd Qu.:-0.6842 3rd Qu.:0.3085367
## Max. : 0.4270 Max. :1.0409784
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::5 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (553 obs.); Bandwidth 'bw' = 0.1442
##
## x y
## Min. :-4.0327 Min. :0.0005088
## 1st Qu.:-2.9234 1st Qu.:0.0297057
## Median :-1.8140 Median :0.0829888
## Mean :-1.8140 Mean :0.2251133
## 3rd Qu.:-0.7047 3rd Qu.:0.3318257
## Max. : 0.4046 Max. :0.9970591
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::6 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (328 obs.); Bandwidth 'bw' = 0.1601
##
## x y
## Min. :-4.0803 Min. :0.001646
## 1st Qu.:-2.9472 1st Qu.:0.026356
## Median :-1.8140 Median :0.086191
## Mean :-1.8140 Mean :0.220367
## 3rd Qu.:-0.6809 3rd Qu.:0.299777
## Max. : 0.4523 Max. :0.984744
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Min::7 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (339 obs.); Bandwidth 'bw' = 0.1296
##
## x y
## Min. :-3.9888 Min. :0.0002484
## 1st Qu.:-2.9014 1st Qu.:0.0128539
## Median :-1.8140 Median :0.0989175
## Mean :-1.8140 Mean :0.2296539
## 3rd Qu.:-0.7266 3rd Qu.:0.3036119
## Max. : 0.3607 Max. :1.0402333
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::0 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (1399 obs.); Bandwidth 'bw' = 0.04258
##
## x y
## Min. :-0.07556 Min. :0.0000761
## 1st Qu.: 0.36209 1st Qu.:0.0128205
## Median : 0.79973 Median :0.1818059
## Mean : 0.79973 Mean :0.5706788
## 3rd Qu.: 1.23738 3rd Qu.:1.0588494
## Max. : 1.67502 Max. :1.9269018
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::1 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (650 obs.); Bandwidth 'bw' = 0.04584
##
## x y
## Min. :-0.3318 Min. :0.0001527
## 1st Qu.: 0.1562 1st Qu.:0.0164770
## Median : 0.6442 Median :0.1815094
## Mean : 0.6442 Mean :0.5118085
## 3rd Qu.: 1.1322 3rd Qu.:0.7606033
## Max. : 1.6202 Max. :2.2673211
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::2 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (544 obs.); Bandwidth 'bw' = 0.05232
##
## x y
## Min. :-0.2953 Min. :0.0001579
## 1st Qu.: 0.1689 1st Qu.:0.0227890
## Median : 0.6332 Median :0.1797921
## Mean : 0.6332 Mean :0.5379797
## 3rd Qu.: 1.0974 3rd Qu.:0.9803248
## Max. : 1.5617 Max. :2.0059875
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::3 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (687 obs.); Bandwidth 'bw' = 0.04836
##
## x y
## Min. :-0.2130 Min. :0.0001357
## 1st Qu.: 0.2237 1st Qu.:0.0279846
## Median : 0.6605 Median :0.1757849
## Mean : 0.6605 Mean :0.5718686
## 3rd Qu.: 1.0972 3rd Qu.:1.0967741
## Max. : 1.5339 Max. :2.0655190
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::4 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (546 obs.); Bandwidth 'bw' = 0.04605
##
## x y
## Min. :-0.2851 Min. :0.0001811
## 1st Qu.: 0.2167 1st Qu.:0.0186303
## Median : 0.7184 Median :0.1638339
## Mean : 0.7184 Mean :0.4977862
## 3rd Qu.: 1.2201 3rd Qu.:0.7351594
## Max. : 1.7218 Max. :2.1562696
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::5 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (553 obs.); Bandwidth 'bw' = 0.04792
##
## x y
## Min. :-0.2381 Min. :0.0001713
## 1st Qu.: 0.2045 1st Qu.:0.0396605
## Median : 0.6471 Median :0.2698159
## Mean : 0.6471 Mean :0.5642511
## 3rd Qu.: 1.0898 3rd Qu.:0.9422134
## Max. : 1.5324 Max. :2.2967403
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::6 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (328 obs.); Bandwidth 'bw' = 0.06806
##
## x y
## Min. :-0.526637 Min. :0.0002024
## 1st Qu.: 0.009716 1st Qu.:0.0154017
## Median : 0.546068 Median :0.1982202
## Mean : 0.546068 Mean :0.4656526
## 3rd Qu.: 1.082420 3rd Qu.:0.8093224
## Max. : 1.618773 Max. :1.7119778
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Mean::7 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (339 obs.); Bandwidth 'bw' = 0.05362
##
## x y
## Min. :-0.1275 Min. :0.0003609
## 1st Qu.: 0.2573 1st Qu.:0.1275825
## Median : 0.6421 Median :0.3449694
## Mean : 0.6421 Mean :0.6490425
## 3rd Qu.: 1.0269 3rd Qu.:1.1049619
## Max. : 1.4117 Max. :2.1733790
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::0 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (1399 obs.); Bandwidth 'bw' = 0.1908
##
## x y
## Min. :1.621 Min. :0.0000639
## 1st Qu.:3.152 1st Qu.:0.0296326
## Median :4.683 Median :0.0817252
## Mean :4.683 Mean :0.1631531
## 3rd Qu.:6.214 3rd Qu.:0.2768348
## Max. :7.744 Max. :0.5736045
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::1 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (650 obs.); Bandwidth 'bw' = 0.2482
##
## x y
## Min. :1.421 Min. :0.0000433
## 1st Qu.:3.045 1st Qu.:0.0231923
## Median :4.669 Median :0.0897385
## Mean :4.669 Mean :0.1537920
## 3rd Qu.:6.293 3rd Qu.:0.2531470
## Max. :7.917 Max. :0.5067893
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::2 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (544 obs.); Bandwidth 'bw' = 0.2519
##
## x y
## Min. :1.466 Min. :0.000039
## 1st Qu.:3.082 1st Qu.:0.021107
## Median :4.697 Median :0.071270
## Mean :4.697 Mean :0.154609
## 3rd Qu.:6.312 3rd Qu.:0.279705
## Max. :7.928 Max. :0.481304
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::3 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (687 obs.); Bandwidth 'bw' = 0.1841
##
## x y
## Min. :1.613 Min. :0.0000748
## 1st Qu.:3.148 1st Qu.:0.0160576
## Median :4.683 Median :0.0452995
## Mean :4.683 Mean :0.1627235
## 3rd Qu.:6.217 3rd Qu.:0.2989899
## Max. :7.752 Max. :0.5647049
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::4 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (546 obs.); Bandwidth 'bw' = 0.1968
##
## x y
## Min. :0.7596 Min. :0.000042
## 1st Qu.:2.5172 1st Qu.:0.006644
## Median :4.2749 Median :0.040476
## Mean :4.2749 Mean :0.142091
## 3rd Qu.:6.0326 3rd Qu.:0.245822
## Max. :7.7903 Max. :0.597056
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::5 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (553 obs.); Bandwidth 'bw' = 0.1976
##
## x y
## Min. :0.1663 Min. :0.000000
## 1st Qu.:2.0729 1st Qu.:0.001862
## Median :3.9796 Median :0.031574
## Mean :3.9796 Mean :0.130993
## 3rd Qu.:5.8862 3rd Qu.:0.198366
## Max. :7.7928 Max. :0.597217
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::6 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (328 obs.); Bandwidth 'bw' = 0.2431
##
## x y
## Min. :1.183 Min. :0.0000566
## 1st Qu.:2.863 1st Qu.:0.0049521
## Median :4.542 Median :0.0676238
## Mean :4.542 Mean :0.1486930
## 3rd Qu.:6.222 3rd Qu.:0.2805033
## Max. :7.901 Max. :0.5264778
##
## ---------------------------------------------------------------------------------
## ::: Seismic_Range::7 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (339 obs.); Bandwidth 'bw' = 0.1915
##
## x y
## Min. :2.069 Min. :0.0001384
## 1st Qu.:3.489 1st Qu.:0.0173561
## Median :4.908 Median :0.0659038
## Mean :4.908 Mean :0.1759744
## 3rd Qu.:6.327 3rd Qu.:0.3476085
## Max. :7.746 Max. :0.6516849
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::0 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (1399 obs.); Bandwidth 'bw' = 0.08911
##
## x y
## Min. :-9.000 Min. :0.0000363
## 1st Qu.:-8.202 1st Qu.:0.0153334
## Median :-7.405 Median :0.1910303
## Mean :-7.405 Mean :0.3130592
## 3rd Qu.:-6.607 3rd Qu.:0.5670849
## Max. :-5.809 Max. :0.9308265
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::1 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (650 obs.); Bandwidth 'bw' = 0.6433
##
## x y
## Min. :-2.60167 Min. :0.000061
## 1st Qu.: 0.01214 1st Qu.:0.005146
## Median : 2.62595 Median :0.044482
## Mean : 2.62595 Mean :0.095469
## 3rd Qu.: 5.23977 3rd Qu.:0.151782
## Max. : 7.85358 Max. :0.413836
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::2 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (544 obs.); Bandwidth 'bw' = 0.1106
##
## x y
## Min. :-0.2706 Min. :0.0000752
## 1st Qu.: 0.6281 1st Qu.:0.0136521
## Median : 1.5267 Median :0.0976180
## Mean : 1.5267 Mean :0.2779221
## 3rd Qu.: 2.4254 3rd Qu.:0.5431846
## Max. : 3.3240 Max. :0.8996228
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::3 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (687 obs.); Bandwidth 'bw' = 0.7163
##
## x y
## Min. :-3.73683 Min. :3.319e-05
## 1st Qu.:-0.08216 1st Qu.:9.805e-03
## Median : 3.57252 Median :2.961e-02
## Mean : 3.57252 Mean :6.834e-02
## 3rd Qu.: 7.22720 3rd Qu.:9.875e-02
## Max. :10.88187 Max. :2.898e-01
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::4 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (546 obs.); Bandwidth 'bw' = 0.979
##
## x y
## Min. :-7.7920 Min. :0.0001052
## 1st Qu.:-3.7434 1st Qu.:0.0072256
## Median : 0.3053 Median :0.0448445
## Mean : 0.3053 Mean :0.0616841
## 3rd Qu.: 4.3540 3rd Qu.:0.1145852
## Max. : 8.4027 Max. :0.1721027
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::5 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (553 obs.); Bandwidth 'bw' = 1.042
##
## x y
## Min. :-12.897 Min. :1.631e-05
## 1st Qu.: -8.227 1st Qu.:2.465e-03
## Median : -3.557 Median :2.216e-02
## Mean : -3.557 Mean :5.348e-02
## 3rd Qu.: 1.113 3rd Qu.:1.019e-01
## Max. : 5.782 Max. :1.743e-01
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::6 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (328 obs.); Bandwidth 'bw' = 0.1391
##
## x y
## Min. :-10.165 Min. :0.0000988
## 1st Qu.: -9.191 1st Qu.:0.0077658
## Median : -8.218 Median :0.1198180
## Mean : -8.218 Mean :0.2564485
## 3rd Qu.: -7.244 3rd Qu.:0.5506295
## Max. : -6.270 Max. :0.7118933
##
## ---------------------------------------------------------------------------------
## ::: Gyro_Gx::7 (KDE)
## ---------------------------------------------------------------------------------
##
## Call:
## density.default(x = x, na.rm = TRUE)
##
## Data: x (339 obs.); Bandwidth 'bw' = 0.1119
##
## x y
## Min. :-9.466 Min. :0.000119
## 1st Qu.:-8.695 1st Qu.:0.020081
## Median :-7.924 Median :0.212940
## Mean :-7.924 Mean :0.323960
## 3rd Qu.:-7.153 3rd Qu.:0.564324
## Max. :-6.382 Max. :0.994602
##
## ---------------------------------------------------------------------------------
##
## # ... and 2 more tables
##
## ---------------------------------------------------------------------------------
GS_T1<-Sys.time()
training_time <- GS_T1-GS_T0
# ใช้เวลาในการฝึกสอน
training_time
## Time difference of 0.05327606 secs
filename <- 'nb_model.rds'
saveRDS(nb_model,file=filename)
nb_predictions <- predict(nb_model, train_data_features)
(tab1 <-table(nb_predictions, train_data_target))
## train_data_target
## nb_predictions 0 1 2 3 4 5 6 7
## 0 1313 0 0 0 0 34 135 176
## 1 0 628 1 23 119 12 0 0
## 2 0 0 532 72 0 0 0 0
## 3 0 0 9 449 0 12 0 0
## 4 0 22 2 28 427 1 0 0
## 5 1 0 0 115 0 295 9 7
## 6 35 0 0 0 0 88 132 46
## 7 50 0 0 0 0 111 52 110
confusionMatrix(nb_predictions,train_data_target)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1 2 3 4 5 6 7
## 0 1313 0 0 0 0 34 135 176
## 1 0 628 1 23 119 12 0 0
## 2 0 0 532 72 0 0 0 0
## 3 0 0 9 449 0 12 0 0
## 4 0 22 2 28 427 1 0 0
## 5 1 0 0 115 0 295 9 7
## 6 35 0 0 0 0 88 132 46
## 7 50 0 0 0 0 111 52 110
##
## Overall Statistics
##
## Accuracy : 0.7701
## 95% CI : (0.7583, 0.7817)
## No Information Rate : 0.2772
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.7249
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 0 Class: 1 Class: 2 Class: 3 Class: 4 Class: 5
## Sensitivity 0.9385 0.9662 0.9779 0.65357 0.78205 0.53345
## Specificity 0.9054 0.9647 0.9840 0.99518 0.98822 0.97062
## Pos Pred Value 0.7919 0.8020 0.8808 0.95532 0.88958 0.69087
## Neg Pred Value 0.9746 0.9948 0.9973 0.94799 0.97394 0.94414
## Prevalence 0.2772 0.1288 0.1078 0.13615 0.10820 0.10959
## Detection Rate 0.2602 0.1245 0.1054 0.08898 0.08462 0.05846
## Detection Prevalence 0.3286 0.1552 0.1197 0.09314 0.09512 0.08462
## Balanced Accuracy 0.9220 0.9654 0.9810 0.82437 0.88514 0.75204
## Class: 6 Class: 7
## Sensitivity 0.40244 0.32448
## Specificity 0.96418 0.95475
## Pos Pred Value 0.43854 0.34056
## Neg Pred Value 0.95869 0.95151
## Prevalence 0.06500 0.06718
## Detection Rate 0.02616 0.02180
## Detection Prevalence 0.05965 0.06401
## Balanced Accuracy 0.68331 0.63962
nb_predictions <- predict(nb_model, test_data_features)
(tab2 <-table(nb_predictions, test_data_target))
## test_data_target
## nb_predictions 0 1 2 3 4 5 6 7
## 0 378 0 0 0 0 8 27 50
## 1 0 137 0 9 29 6 0 0
## 2 0 0 138 15 0 0 0 0
## 3 0 0 1 109 0 5 0 0
## 4 0 6 0 3 106 0 0 0
## 5 3 0 0 35 0 58 3 2
## 6 6 0 0 0 0 18 19 9
## 7 13 0 0 0 0 23 16 25
confusionMatrix(nb_predictions,test_data_target)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1 2 3 4 5 6 7
## 0 378 0 0 0 0 8 27 50
## 1 0 137 0 9 29 6 0 0
## 2 0 0 138 15 0 0 0 0
## 3 0 0 1 109 0 5 0 0
## 4 0 6 0 3 106 0 0 0
## 5 3 0 0 35 0 58 3 2
## 6 6 0 0 0 0 18 19 9
## 7 13 0 0 0 0 23 16 25
##
## Overall Statistics
##
## Accuracy : 0.7717
## 95% CI : (0.7475, 0.7946)
## No Information Rate : 0.3182
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.7205
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 0 Class: 1 Class: 2 Class: 3 Class: 4 Class: 5
## Sensitivity 0.9450 0.9580 0.9928 0.63743 0.78519 0.49153
## Specificity 0.9008 0.9605 0.9866 0.99448 0.99198 0.96225
## Pos Pred Value 0.8164 0.7569 0.9020 0.94783 0.92174 0.57426
## Neg Pred Value 0.9723 0.9944 0.9991 0.94571 0.97461 0.94810
## Prevalence 0.3182 0.1138 0.1106 0.13604 0.10740 0.09387
## Detection Rate 0.3007 0.1090 0.1098 0.08671 0.08433 0.04614
## Detection Prevalence 0.3683 0.1440 0.1217 0.09149 0.09149 0.08035
## Balanced Accuracy 0.9229 0.9593 0.9897 0.81595 0.88858 0.72689
## Class: 6 Class: 7
## Sensitivity 0.29231 0.29070
## Specificity 0.97232 0.95559
## Pos Pred Value 0.36538 0.32468
## Neg Pred Value 0.96183 0.94831
## Prevalence 0.05171 0.06842
## Detection Rate 0.01512 0.01989
## Detection Prevalence 0.04137 0.06126
## Balanced Accuracy 0.63231 0.62315
Test data Accuracy is 77.17 % ## Plot nb model
plot(nb_model)