-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification.R
410 lines (395 loc) · 13.1 KB
/
classification.R
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#' Multiclass classification
#'
#' Run a multiclass classification algorithm on a given dataset and reference
#' class.
#'
#' Some of the classification algorithms implemented use pre-defined values that
#' specify settings and options while others need to tune hyperparameters.
#' `"multinom"` and `"nnet"` use a maximum number of weights of 2000, in case
#' `data` is high dimensional and classification is time-consuming. `"nnet"`
#' also tunes the number of nodes (1-5) in the hidden layer. `"pam"` considers
#' 100 thresholds when training, and uses a uniform prior. `"adaboost"` calls
#' [maboost::maboost()] instead of [adabag::boosting()] for faster performance.
#' As a result, we use the `"entrop"` option, which uses the KL-divergence
#' method and mimics adaboost. However, `"adaboost_m1"` calls
#' [adabag::boosting()] which supports hyperparameter tuning.
#'
#' When `alg = "knn"`, the return value is `NULL` because [class::knn()] does
#' not output an intermediate model object. The modelling and prediction is
#' performed in one step. However, the class attribute "knn" is still assigned
#' to the result in order to call the respective [prediction()] method. An
#' additional class "ova" is added if `ova = TRUE`.
#'
#' @inheritParams splendid
#' @inheritSection splendid Algorithms
#' @param algorithms character string of algorithm to use for supervised
#' learning. See \strong{Algorithms} section for possible options.
#' @param ova logical; if `TRUE`, use the One-Vs-All approach for the `knn`
#' algorithm.
#' @param sizes the range of sizes of features to test RFE algorithm
#' @return The model object from running the classification `algorithm`
#'
#' @author Derek Chiu
#' @export
#' @examples
#' data(hgsc)
#' class <- attr(hgsc, "class.true")
#' classification(hgsc, class, "xgboost")
classification <- function(data, class, algorithms, rfe = FALSE, ova = FALSE,
standardize = FALSE,
sampling = c("none", "up", "down", "smote"),
seed_samp = NULL, sizes = NULL, trees = 100,
tune = FALSE, seed_alg = NULL, convert = FALSE) {
algorithms <- match.arg(algorithms, ALG.NAME)
# Process the data, allow subsampling on the training data
sp <- splendid_process(data, class, algorithms, convert, standardize,
sampling, seed_samp)
data <- sp[["data"]]
class <- sp[["class"]]
switch(
algorithms,
pam = pam_model(data, class, seed_alg),
svm = rfe_model(data, class, "svm", rfe, sizes, tune, seed_alg = seed_alg),
rf = rfe_model(data, class, "rf", rfe, sizes, tune, trees, seed_alg),
lda = rfe_model(data, class, "lda", rfe, sizes, tune),
slda = sda_model(data, class, "slda"),
sdda = sda_model(data, class, "sdda"),
mlr_glm = mlr_model(data, class, "mlr_glm"),
mlr_lasso = cv_mlr_model(data, class, "mlr_lasso", seed_alg),
mlr_ridge = cv_mlr_model(data, class, "mlr_ridge", seed_alg),
mlr_enet = enet_model(data, class, seed_alg),
mlr_nnet = mlr_model(data, class, "mlr_nnet"),
nnet = nnet_model(data, class),
nbayes = nbayes_model(data, class),
adaboost = boost_model(data, class, "adaboost", trees, seed_alg),
adaboost_m1 = rfe_model(data, class, "adaboost_m1", rfe, sizes,
tune, trees),
xgboost = boost_model(data, class, "xgboost", seed_alg = seed_alg),
knn = knn_model(class, "knn", ova)
)
}
#' pam model using uniform prior probabilities for class representation
#' optimal threshold delta is selected as largest threshold among those with the
#' smallest cross-validated error
#' @noRd
pam_model <- function(data, class, seed_alg = NULL) {
if (!is.null(seed_alg)) set.seed(seed_alg)
nc <- dplyr::n_distinct(class)
pamr_data <- list(x = t(data), y = class)
if (!requireNamespace("pamr", quietly = TRUE)) {
stop("Package \"pamr\" is needed. Please install it.",
call. = FALSE)
} else {
mod <- sink_output(pamr::pamr.train(
data = pamr_data,
n.threshold = 100,
prior = rep(1 / nc, nc)
))
mod_cv <- sink_output(pamr::pamr.cv(
fit = mod,
data = pamr_data,
nfold = 5
))
delta <-
mod_cv$threshold[max(which(mod_cv$error == min(mod_cv$error)))]
mod <- c(mod, delta = delta)
class(mod) <- "pamrtrained"
mod
}
}
#' RFE model
#' @noRd
rfe_model <- function(data, class, algorithms, rfe, sizes, tune, trees = NULL,
seed_alg = NULL) {
method <- rfe_method(algorithms)
sizes <- rfe_sizes(sizes, class)
tune_args <- tibble::lst(class, method, trees, seed_alg)
if (method == "AdaBoost.M1") names(data) <- make.names(names(data))
if (rfe) {
if (!requireNamespace("caret", quietly = TRUE)) {
stop("Package \"caret\" is needed. Please install it.",
call. = FALSE)
} else {
mod <- suppressWarnings(
caret::rfe(
x = data,
y = class,
sizes = sizes,
metric = "Accuracy",
rfeControl = caret::rfeControl(method = "cv", number = 2),
trControl = caret::trainControl(method = "none"),
method = method,
tuneGrid = param_grids(data, method, type = "default")
)
)
}
data <- data[mod[["optVariables"]]]
}
if (!is.null(seed_alg)) set.seed(seed_alg)
if (tune) {
suppressWarnings(rlang::exec(tune_model, !!!tune_args, data = data))
} else {
switch(
algorithms,
rf = {
if (!requireNamespace("randomForest", quietly = TRUE)) {
stop("Package \"randomForest\" is needed. Please install it.",
call. = FALSE)
} else {
randomForest::randomForest(x = data, y = class)
}
},
lda = {
if (!requireNamespace("MASS", quietly = TRUE)) {
stop("Package \"MASS\" is needed. Please install it.",
call. = FALSE)
} else {
suppressWarnings(MASS::lda(x = data, grouping = class))
}
},
svm = {
if (!requireNamespace("e1071", quietly = TRUE)) {
stop("Package \"e1071\" is needed. Please install it.",
call. = FALSE)
} else {
e1071::svm(x = data, y = class, probability = TRUE)
}
},
adaboost_m1 = {
if (!requireNamespace("adabag", quietly = TRUE)) {
stop("Package \"adabag\" is needed. Please install it.",
call. = FALSE)
} else {
adabag::boosting(formula = class ~ .,
data = cbind(data, class),
mfinal = 3)
}
}
)
}
}
#' RFE methods
#' @noRd
rfe_method <- function(algorithms) {
switch(
algorithms,
lda = "lda",
rf = "rf",
svm = "svmRadial",
adaboost_m1 = "AdaBoost.M1"
)
}
#' RFE sizes by default are equal to every 25th integer up to one-half of the
#' smallest class size. If class sizes are too small, use size = 1
#' @noRd
rfe_sizes <- function(sizes, class) {
sizes <- sizes %||% {
class %>%
table() %>%
min() %>%
magrittr::divide_by_int(2) %>%
seq_len(.) %>%
magrittr::extract(. %% 25 == 0)
}
if (length(sizes) == 0) 1 else sizes
}
#' Hyperparameter search grids. A single set is used for type "default" whereas
#' combinations of values are used for type "range"
#' @noRd
param_grids <- function(data, method, type = c("default", "range")) {
type <- match.arg(type)
switch(
type,
default = switch(
method,
lda = NULL,
rf = data.frame(mtry = floor(sqrt(ncol(data)))),
svmRadial = data.frame(sigma = mean(kernlab::sigest(as.matrix(data))[-2]),
C = 1),
AdaBoost.M1 = data.frame(mfinal = 3, maxdepth = 5, coeflearn = "Breiman")
),
range = switch(
method,
lda = NULL,
rf = data.frame(mtry = (1:5) ^ 2),
svmRadial = expand.grid(sigma = 1 / ncol(data) * 2 ^ (0:4),
C = 2 ^ (0:4)),
AdaBoost.M1 = expand.grid(
mfinal = 1:5,
maxdepth = 1:5,
coeflearn = c("Breiman", "Freund", "Zhu")
)
)
)
}
#' Tune models with pre-specified search grids for hyperparameters
#' @noRd
tune_model <- function(data, class, method, trees, seed_alg) {
tune_grid <- param_grids(data, method, type = "range")
if (is.null(seed_alg)) {
seeds <- NA
} else {
seeds <- c(purrr::map(1:5, ~ sample(10 ^ 6, nrow(tune_grid))), seed_alg)
}
tune_args <- list(
x = data,
y = class,
method = method,
metric = "Accuracy",
trControl = caret::trainControl(
method = "cv",
number = 5,
classProbs = TRUE,
seeds = seeds
),
tuneGrid = tune_grid
)
if (is.null(trees)) {
rlang::exec(caret::train, !!!tune_args)
} else {
rlang::exec(caret::train, !!!tune_args, ntree = trees)
}
}
#' sda model
#' @noRd
sda_model <- function(data, class, algorithms) {
diagonal <- switch(algorithms, slda = FALSE, sdda = TRUE)
if (!requireNamespace("sda", quietly = TRUE)) {
stop("Package \"sda\" is needed. Please install it.",
call. = FALSE)
} else {
sda::sda(as.matrix(data), class, diagonal = diagonal, verbose = FALSE)
}
}
#' Multinomial Logistic Regression
#' @noRd
mlr_model <- function(data, class, algorithms) {
switch(algorithms,
mlr_nnet = {
if (!requireNamespace("nnet", quietly = TRUE)) {
stop("Package \"nnet\" is needed. Please install it.",
call. = FALSE)
} else {
nnet::multinom(class ~ ., data, MaxNWts = 2000, trace = FALSE)
}
},
mlr_glm = {
if (!requireNamespace("glmnet", quietly = TRUE)) {
stop("Package \"glmnet\" is needed. Please install it.",
call. = FALSE)
} else {
glmnet::glmnet(as.matrix(data),
class,
lambda = 0,
family = "multinomial")
}
})
}
#' Cross-validated regularized MLR
#' @noRd
cv_mlr_model <- function(data, class, algorithms, seed_alg = NULL) {
if (!is.null(seed_alg)) set.seed(seed_alg)
alpha <- switch(algorithms, mlr_lasso = 1, mlr_ridge = 0)
if (!requireNamespace("glmnet", quietly = TRUE)) {
stop("Package \"glmnet\" is needed. Please install it.",
call. = FALSE)
} else {
glmnet::cv.glmnet(as.matrix(data), class, alpha = alpha, family = "multinomial")
}
}
#' Elastic net model
#' @noRd
enet_model <- function(data, class, seed_alg = NULL) {
if (!is.null(seed_alg)) set.seed(seed_alg)
if (!requireNamespace("caret", quietly = TRUE)) {
stop("Package \"caret\" is needed. Please install it.",
call. = FALSE)
} else {
caret::train(
x = as.matrix(data),
y = class,
method = "glmnet",
tuneLength = 10,
trControl = caret::trainControl(
method = "cv",
number = 5,
classProbs = TRUE
)
)
}
}
#' neural network model
#' @noRd
nnet_model <- function(data, class) {
if (!"package:nnet" %in% search()) attachNamespace("nnet")
if (!requireNamespace("e1071", quietly = TRUE)) {
stop("Package \"e1071\" is needed. Please install it.",
call. = FALSE)
} else {
suppressWarnings(e1071::best.nnet(
class ~ .,
data = cbind(data, class),
size = seq_len(5),
decay = seq(0, 0.5, length.out = 5),
MaxNWts = 2000,
tunecontrol = e1071::tune.control(sampling = "fix")
))
}
}
#' naive bayes model
#' @noRd
nbayes_model <- function(data, class) {
if (!requireNamespace("e1071", quietly = TRUE)) {
stop("Package \"e1071\" is needed. Please install it.",
call. = FALSE)
} else {
e1071::naiveBayes(data, class)
}
}
#' boosting models
#' @noRd
boost_model <- function(data, class, algorithms, trees, seed_alg = NULL) {
if (!is.null(seed_alg)) set.seed(seed_alg)
switch(algorithms,
adaboost = {
if (!requireNamespace("maboost", quietly = TRUE)) {
stop("Package \"maboost\" is needed. Please install it.",
call. = FALSE)
} else {
sink_output(maboost::maboost(
x = data,
y = class,
breg = "entrop",
iter = trees,
minsplit = 2
))
}
},
xgboost = {
if (!requireNamespace("xgboost", quietly = TRUE)) {
stop("Package \"xgboost\" is needed. Please install it.",
call. = FALSE)
} else {
xgboost::xgb.train(
params = list(
"objective" = "multi:softprob",
"eval_metric" = "mlogloss",
"num_class" = nlevels(class)
),
data = xgboost::xgb.DMatrix(data = as.matrix(data),
label = as.integer(class) - 1),
nrounds = 2
)
}
})
}
#' knn "dummy model" returns training class
#' @noRd
knn_model <- function(class, algorithms, ova) {
if (ova) {
structure(list(unique(class[class != "class_0"])),
class = c(algorithms, "ova"))
} else {
structure(class, class = c(algorithms, "factor"))
}
}