-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
104 lines (79 loc) · 3.32 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
---
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%",
warning = F,
message = F
)
```
# ImbLassoRcpp
<!-- badges: start -->
[![R-CMD-check](https://github.com/UofUEpiBio/ImbLassoRcpp/workflows/R-CMD-check/badge.svg)](https://github.com/UofUEpiBio/ImbLassoRcpp/actions)
<!-- badges: end -->
The goal of ImbLassoRcpp is to handle imbalanced distribution in the binary outcome by employing stratified cross-validation and/or SMOTE
## Installation
You can install the development version of ImbLassoRcpp like so:
```{r, eval=FALSE}
# install.package("devtools")
devtools::install_github("Haojia-biostat/ImbLassoRcpp")
```
## Example
### 0. Preparation
I am going to apply the functions included in the `ImbLassoRcpp` package to the data edited based on the `Vehicle` from `mlbench` package. The details of the original dataset can be found [here](https://search.r-project.org/CRAN/refmans/mlbench/html/Vehicle.html).
In order to construct a dataset with imbalanced distributed binary outcome, I collapsed all the three categories in the `Vehicle$Class` other than "bus" together, and randomly removed 150 observations from the "bus" category. In this way, the data now has 9.8% (<10%) of observations with "bus" class.
```{r}
library(mlbench)
data("Vehicle")
# table(Vehicle$Class)
# bus opel saab van
# 218 212 217 199
bus_id <- which(Vehicle$Class == "bus")
set.seed(7045)
imbdata <- Vehicle[-sample(bus_id, 150),]
y <- as.integer(imbdata$Class == "bus")
# mean(y) # 0.09770115
X <- data.frame(lapply(imbdata[,which(colnames(imbdata) != "Class")], scale))
```
```{r}
library(ImbLassoRcpp)
# construct data with consideration of stratified cv and SMOTE
cv_smote_data_list <- list(
# without stratified for cross-validation and without SMOTE (reference)
stratified_cv_smote(as.matrix(X), y, stratified = F, SMOTE = F),
# without stratified for cross-validation and with SMOTE
stratified_cv_smote(as.matrix(X), y, stratified = F, SMOTE = T),
# with stratified for cross-validation and without SMOTE
stratified_cv_smote(as.matrix(X), y, stratified = T, SMOTE = F),
# with stratified for cross-validation and with SMOTE
stratified_cv_smote(as.matrix(X), y, stratified = T, SMOTE = T)
)
# do parallel computing for parameter tuning of lambda
smote_cv_lasso_list <- lapply(cv_smote_data_list, par_smote_cv_lasso)
```
### 1. Parameter tuning
```{r, fig.width=8, fig.height=6}
par(mfrow = c(2,2))
plot.cv.smote.lasso(smote_cv_lasso_list[[1]], main = "Stratified = F, SMOTE = F")
plot.cv.smote.lasso(smote_cv_lasso_list[[2]], main = "Stratified = F, SMOTE = T")
plot.cv.smote.lasso(smote_cv_lasso_list[[3]], main = "Stratified = T, SMOTE = F")
plot.cv.smote.lasso(smote_cv_lasso_list[[4]], main = "Stratified = T, SMOTE = T")
```
```{r}
# print.cv.smote.lasso(smote_cv_lasso_list[[1]])
# print.cv.smote.lasso(smote_cv_lasso_list[[2]])
# print.cv.smote.lasso(smote_cv_lasso_list[[3]])
print.cv.smote.lasso(smote_cv_lasso_list[[4]])
```
### 2. Final model
```{r}
# summary.cv.smote.lasso(smote_cv_lasso_list[[1]])
# summary.cv.smote.lasso(smote_cv_lasso_list[[2]])
# summary.cv.smote.lasso(smote_cv_lasso_list[[3]])
summary.cv.smote.lasso(smote_cv_lasso_list[[4]])
```