Chapter 14 Naive Bayes Classification

Learning objectives:

  • explore the pros and cons of naive Bayes classification
  • generalize classification tasks for more than two categories
library("bayesrules")
library("dplyr")
library("e1071")
library("ggplot2")
library("ggtext")
library("janitor")
library("tidyr")

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0
## 
## locale:
##  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
##  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
##  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
## [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
## 
## time zone: UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] ggtext_0.1.2        e1071_1.7-13        tidybayes_3.0.4    
##  [4] rstanarm_2.21.4     Rcpp_1.0.11         broom.mixed_0.2.9.4
##  [7] patchwork_1.1.2     bayesplot_1.10.0    rstan_2.21.8       
## [10] StanHeaders_2.26.27 latex2exp_0.9.6     gt_0.9.0           
## [13] janitor_2.2.0       bayesrules_0.0.2    lubridate_1.9.2    
## [16] forcats_1.0.0       stringr_1.5.0       dplyr_1.1.2        
## [19] purrr_1.0.1         readr_2.1.4         tidyr_1.3.0        
## [22] tibble_3.2.1        ggplot2_3.4.2       tidyverse_2.0.0    
## 
## loaded via a namespace (and not attached):
##   [1] RColorBrewer_1.1-3   tensorA_0.36.2       rstudioapi_0.15.0   
##   [4] jsonlite_1.8.7       magrittr_2.0.3       farver_2.1.1        
##   [7] nloptr_2.0.3         rmarkdown_2.23       vctrs_0.6.3         
##  [10] minqa_1.2.5          base64enc_0.1-3      htmltools_0.5.5     
##  [13] distributional_0.3.2 broom_1.0.5          parallelly_1.36.0   
##  [16] sass_0.4.6           bslib_0.5.0          htmlwidgets_1.6.2   
##  [19] plyr_1.8.8           zoo_1.8-12           cachem_1.0.8        
##  [22] igraph_1.5.0         mime_0.12            lifecycle_1.0.3     
##  [25] pkgconfig_2.0.3      colourpicker_1.2.0   Matrix_1.5-4.1      
##  [28] R6_2.5.1             fastmap_1.1.1        future_1.33.0       
##  [31] shiny_1.7.4.1        snakecase_0.11.0     digest_0.6.33       
##  [34] colorspace_2.1-0     furrr_0.3.1          ps_1.7.5            
##  [37] crosstalk_1.2.0      labeling_0.4.2       fansi_1.0.4         
##  [40] timechange_0.2.0     mgcv_1.8-42          abind_1.4-5         
##  [43] compiler_4.3.1       proxy_0.4-27         withr_2.5.0         
##  [46] backports_1.4.1      inline_0.3.19        shinystan_2.6.0     
##  [49] pkgbuild_1.4.2       highr_0.10           MASS_7.3-60         
##  [52] gtools_3.9.4         loo_2.6.0            tools_4.3.1         
##  [55] httpuv_1.6.11        threejs_0.3.3        glue_1.6.2          
##  [58] DiagrammeR_1.0.10    callr_3.7.3          nlme_3.1-162        
##  [61] promises_1.2.0.1     gridtext_0.1.5       grid_4.3.1          
##  [64] checkmate_2.2.0      reshape2_1.4.4       generics_0.1.3      
##  [67] gtable_0.3.3         tzdb_0.4.0           class_7.3-22        
##  [70] hms_1.1.3            xml2_1.3.5           utf8_1.2.3          
##  [73] ggdist_3.3.0         pillar_1.9.0         markdown_1.7        
##  [76] posterior_1.4.1      later_1.3.1          splines_4.3.1       
##  [79] lattice_0.21-8       survival_3.5-5       tidyselect_1.2.0    
##  [82] miniUI_0.1.1.1       knitr_1.43           arrayhelpers_1.1-0  
##  [85] gridExtra_2.3        bookdown_0.34        groupdata2_2.0.3    
##  [88] stats4_4.3.1         xfun_0.39            matrixStats_1.0.0   
##  [91] DT_0.28              visNetwork_2.1.2     stringi_1.7.12      
##  [94] yaml_2.3.7           boot_1.3-28.1        evaluate_0.21       
##  [97] codetools_0.2-19     cli_3.6.1            RcppParallel_5.1.7  
## [100] shinythemes_1.2.0    xtable_1.8-4         munsell_0.5.0       
## [103] processx_3.8.2       jquerylib_0.1.4      globals_0.16.2      
## [106] coda_0.19-4          png_0.1-8            svUnit_1.0.6        
## [109] parallel_4.3.1       rstantools_2.3.1     ellipsis_0.3.2      
## [112] prettyunits_1.1.1    dygraphs_1.1.1.6     listenv_0.9.0       
## [115] lme4_1.1-34          ggthemes_4.2.4       ggridges_0.5.4      
## [118] scales_1.2.1         xts_0.13.1           crayon_1.5.2        
## [121] rlang_1.1.1          shinyjs_2.1.0