/
README.Rmd
101 lines (71 loc) · 3.45 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
---
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%"
)
```
# LDATree <a href="http://iamwangsiyu.com/LDATree/"><img src="man/figures/logo.png" align="right" height="139" alt="LDATree website" /></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/LDATree)](https://CRAN.R-project.org/package=LDATree)
[![R-CMD-check](https://github.com/Moran79/LDATree/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Moran79/LDATree/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
`LDATree` is an R modeling package for fitting classification trees. If you are unfamiliar with classification trees, here is a [tutorial](http://www.sthda.com/english/articles/35-statistical-machine-learning-essentials/141-cart-model-decision-tree-essentials/) about the traditional CART and its R implementation `rpart`.
## Overview
Compared to other similar trees, `LDATree` sets itself apart in the following ways:
* It applies the idea of LDA (Linear Discriminant Analysis) when selecting variables, finding splits, and fitting models in terminal nodes.
* It addresses certain limitations of the R implementation of LDA (`MASS::lda`), such as handling missing values, dealing with more features than samples, and constant values within groups.
* Re-implement LDA using the Generalized Singular Value Decomposition (GSVD), LDATree offers quick response, particularly with large datasets.
* The package also includes several visualization tools to provide deeper insights into the data.
## Installation
``` r
install.packages("LDATree")
```
The CRAN version is an outdated one from 08/2023. As of 06/2024, please use the command below for the current version, and the official updated CRAN release will be coming soon!
```{r,fig.asp=0.618,out.width = "80%",fig.align = "center", eval=FALSE}
library(devtools)
install_github('Moran79/LDATree')
```
## Usage
To build an LDATree:
```{r,fig.asp=0.618,out.width = "100%",fig.align = "center"}
library(LDATree)
set.seed(443)
mpg <- as.data.frame(ggplot2::mpg)
datX <- mpg[, -5] # All predictors without Y
response <- mpg[, 5] # we try to predict "cyl" (number of cylinders)
fit <- Treee(datX = datX, response = response, verbose = FALSE)
```
To plot the LDATree:
```{r,fig.asp=0.618,out.width = "80%",fig.align = "center", eval=FALSE}
# View the overall tree.
plot(fit)
```
<img src="man/figures/README-plot1-1.png" width="80%" style="display: block; margin: auto;" />
```{r plot2,fig.asp=0.618,out.width = "80%",fig.align = "center", echo=TRUE}
# Three types of individual plots
# 1. Scatter plot on first two LD scores
plot(fit, datX = datX, response = response, node = 1)
# 2. Density plot on the first LD score
plot(fit, datX = datX, response = response, node = 3)
# 3. A message
plot(fit, datX = datX, response = response, node = 2)
```
To make predictions:
```{r,fig.asp=0.618,out.width = "100%",fig.align = "center", echo=TRUE}
# Prediction only.
predictions <- predict(fit, datX)
head(predictions)
```
```{r,fig.asp=0.618,out.width = "100%",fig.align = "center", echo=TRUE}
# A more informative prediction
predictions <- predict(fit, datX, type = "all")
head(predictions)
```
## Getting help
If you encounter a clear bug, please file an issue with a minimal reproducible example on [GitHub](https://github.com/Moran79/LDATree/issues)