-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
124 lines (81 loc) · 5.49 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# folda <a href="http://iamwangsiyu.com/folda/"><img src="man/figures/logo.png" align="right" height="139" alt="folda website" /></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/folda)](https://CRAN.R-project.org/package=folda)
[![R-CMD-check](https://github.com/Moran79/folda/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Moran79/folda/actions/workflows/R-CMD-check.yaml)
![CRAN Downloads](https://cranlogs.r-pkg.org/badges/grand-total/folda)
<!-- badges: end -->
The `folda` package is an R modeling tool designed for fitting Forward Stepwise Linear Discriminant Analysis (LDA) and Uncorrelated Linear Discriminant Analysis (ULDA). If you're unfamiliar with stepwise LDA or ULDA, please refer to the following resources:
* For **stepwise LDA using Wilks' Lambda**, see Section 6.11.1 in *Methods of Multivariate Analysis, Third Edition* by Alvin C. Rencher and William F. Christensen (2012).
* For **ULDA**, refer to Ye, J., & Yu, B. (2005). *Characterization of a family of algorithms for generalized discriminant analysis on undersampled problems.* Journal of Machine Learning Research, 6(4).
[Link](https://www.jmlr.org/papers/volume6/ye05a/ye05a.pdf).
* For **a combination of ULDA and forward LDA using Pillai's trace**, see Wang, S. (2024). *A New Forward Discriminant Analysis Framework Based on Pillai's Trace and ULDA*. arXiv preprint arXiv:2409.03136. [Link](https://arxiv.org/abs/2409.03136).
## Installation
``` r
install.packages("folda")
```
You can install the development version of `folda` from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("Moran79/folda")
```
## Overview
If you've ever been frustrated by the warnings and errors from `MASS::lda()`, you will appreciate the ULDA implementation in `folda()`. It offers several key improvements:
* **No more "constant within group" errors!** ULDA can handle constant columns and perfect separation of groups.
* **Automatic missing value handling!** The implementation seamlessly integrates automatic missing value imputation during both training and testing phases.
* **Fast!** ULDA is implemented using the Generalized Singular Value Decomposition (GSVD) method, which diagonalizes both within-class and total scatter matrices simultaneously, offering a speed advantage over the sequential diagonalization used in `MASS::lda()` (see Howland et al., 2003 for more details). We have also rewritten the matrix decomposition modules (SVD, QR) using `RcppEigen`, further improving computational efficiency by leveraging optimized C++ code.
* **Better visualization!** `folda` uses `ggplot2` to provide visualizations of class separation in projected 2D spaces (or 1D histograms), offering valuable insights.
For the forward LDA implementation, `folda` offers the following advantages over the classical framework:
* **No issues with multicollinearity or perfect linear dependency!**
* **Handles perfect separation and offers greater power!** The classical approach using Wilks' Lambda has known limitations, including premature stopping when some (not all) groups are perfectly separated. Pillai's trace, as used in `folda()`, not only effectively addresses perfect separation, but has also been shown to generally have greater statistical power than Wilks' Lambda (Rencher et al., 2002).
## Basic Usage
```{r}
library(folda)
mpg <- as.data.frame(ggplot2::mpg) # Prepare the data
datX <- mpg[, -5] # All predictors without Y
response <- mpg[, 5] # we try to predict "cyl" (number of cylinders)
```
Build a ULDA model with all variables:
```{r}
fit <- folda(datX = datX, response = response, subsetMethod = "all")
```
Build a ULDA model with forward selection via Pillai's trace:
```{r}
fit <- folda(datX = datX, response = response, subsetMethod = "forward", testStat = "Pillai")
print(fit) # 6 out of 11 variables are selected, displ is the most important among them
```
Plot the results:
```{r, fig.asp=0.618,out.width = "70%",fig.align = "center"}
plot(fit, datX = datX, response = response)
```
One-dimensional plot:
```{r, fig.asp=0.618,out.width = "70%",fig.align = "center"}
# A 1D plot is created when there is only one feature
# or for binary classification problems.
mpgSmall <- mpg[, c("cyl", "displ")]
fitSmall <- folda(mpgSmall[, -1, drop = FALSE], mpgSmall[, 1])
plot(fitSmall, mpgSmall, mpgSmall[, 1])
```
Make predictions:
```{r}
head(predict(fit, datX, type = "response"))
head(predict(fit, datX, type = "prob")) # Posterior probabilities
```
More examples can be found in the [vignette](https://iamwangsiyu.com/folda/articles/folda.html).
## References
* Howland, P., Jeon, M., & Park, H. (2003). Structure preserving dimension reduction for clustered text data based on the generalized singular value decomposition. *SIAM Journal on Matrix Analysis and Applications*, 25(1), 165-179.
* Rencher, A. C., & Christensen, W. F. (2002). *Methods of Multivariate Analysis* (Vol. 727). John Wiley & Sons.
* Wang, S. (2024). A new forward discriminant analysis framework based on Pillai's trace and ULDA. *arXiv preprint*, arXiv:2409.03136. Retrieved from https://arxiv.org/abs/2409.03136.
## Getting help
If you encounter a clear bug, please file an issue with a minimal reproducible example on [GitHub](https://github.com/Moran79/folda/issues)