9 Linear Discriminant Analysis

Learning objectives:

  • Purpose of classifiers
  • What are generative classifiers
  • Gaussian Discriminant Analysis lead to curved decision boundaries
  • Linear Discriminant Analysis and linear decision boundaries
  • ScikitLearn approaches for Linear / Gaussian Discriminant Analysis
  • Explore Naive Bayes classification
  • Discuss Fisher’s linear discriminant analysis
library("bayesrules")
library("dplyr")
library("e1071")
library("ggplot2")
library("ggtext")
library("gt")
library("janitor")
library("magrittr") #need for "." in pipe acts
# library("MASS") #carefully use lda() later to not overwrite select()
library("patchwork")
library("tidyr")

sessionInfo()
## R version 4.4.0 (2024-04-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 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] tidyr_1.3.1      patchwork_1.2.0  magrittr_2.0.3   janitor_2.2.0   
##  [5] gt_0.10.1        ggtext_0.1.2     ggplot2_3.5.1    e1071_1.7-14    
##  [9] dplyr_1.1.4      bayesrules_0.0.2
## 
## loaded via a namespace (and not attached):
##   [1] gridExtra_2.3        inline_0.3.19        rlang_1.1.3         
##   [4] snakecase_0.11.1     matrixStats_1.3.0    compiler_4.4.0      
##   [7] loo_2.7.0            vctrs_0.6.5          reshape2_1.4.4      
##  [10] stringr_1.5.1        pkgconfig_2.0.3      fastmap_1.2.0       
##  [13] backports_1.5.0      utf8_1.2.4           threejs_0.3.3       
##  [16] promises_1.3.0       rmarkdown_2.27       markdown_1.12       
##  [19] nloptr_2.0.3         purrr_1.0.2          xfun_0.44           
##  [22] cachem_1.1.0         jsonlite_1.8.8       highr_0.11          
##  [25] later_1.3.2          parallel_4.4.0       R6_2.5.1            
##  [28] dygraphs_1.1.1.6     bslib_0.7.0          stringi_1.8.4       
##  [31] StanHeaders_2.32.9   boot_1.3-30          lubridate_1.9.3     
##  [34] jquerylib_0.1.4      Rcpp_1.0.12          bookdown_0.39       
##  [37] rstan_2.32.6         knitr_1.47           zoo_1.8-12          
##  [40] base64enc_0.1-3      bayesplot_1.11.1     httpuv_1.6.15       
##  [43] Matrix_1.7-0         splines_4.4.0        igraph_2.0.3        
##  [46] timechange_0.3.0     tidyselect_1.2.1     abind_1.4-5         
##  [49] yaml_2.3.8           codetools_0.2-20     miniUI_0.1.1.1      
##  [52] curl_5.2.1           pkgbuild_1.4.4       lattice_0.22-6      
##  [55] tibble_3.2.1         plyr_1.8.9           shiny_1.8.1.1       
##  [58] withr_3.0.0          groupdata2_2.0.3     posterior_1.5.0     
##  [61] evaluate_0.23        survival_3.5-8       proxy_0.4-27        
##  [64] RcppParallel_5.1.7   xml2_1.3.6           xts_0.13.2          
##  [67] pillar_1.9.0         tensorA_0.36.2.1     checkmate_2.3.1     
##  [70] DT_0.33              stats4_4.4.0         shinyjs_2.1.0       
##  [73] distributional_0.4.0 generics_0.1.3       rstantools_2.4.0    
##  [76] munsell_0.5.1        scales_1.3.0         minqa_1.2.7         
##  [79] gtools_3.9.5         xtable_1.8-4         class_7.3-22        
##  [82] glue_1.7.0           tools_4.4.0          shinystan_2.6.0     
##  [85] lme4_1.1-35.3        colourpicker_1.3.0   grid_4.4.0          
##  [88] QuickJSR_1.2.0       crosstalk_1.2.1      colorspace_2.1-0    
##  [91] nlme_3.1-164         cli_3.6.2            fansi_1.0.6         
##  [94] V8_4.4.2             gtable_0.3.5         sass_0.4.9          
##  [97] digest_0.6.35        htmlwidgets_1.6.4    htmltools_0.5.8.1   
## [100] lifecycle_1.0.4      mime_0.12            rstanarm_2.32.1     
## [103] gridtext_0.1.5       shinythemes_1.2.0    MASS_7.3-60.2