-
Notifications
You must be signed in to change notification settings - Fork 0
/
splendid_ensemble.R
53 lines (50 loc) · 1.74 KB
/
splendid_ensemble.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
#' Combine classification models into an ensemble
#'
#' @inheritParams splendid
#' @param sm a `splendid_model` object
#' @export
#' @examples
#' dat <- iris[, 1:4]
#' class <- iris$Species
#' sm <- splendid_model(dat, class, n = 3, algorithms = c("xgboost", "slda"))
#' se <- splendid_ensemble(sm, dat, class)
splendid_ensemble <- function(sm, data, class, top = 3, seed_rank = 1,
rfe = FALSE, sequential = FALSE) {
# vector of best performing algorithms from each bootstrap replicate
bests <- sm$evals %>%
do.call(cbind, .) %>%
t() %>%
as.data.frame() %>%
dplyr::mutate(logloss = -logloss,
boot = gsub(".*\\.", "\\1", rownames(.))) %>%
split(.$boot) %>%
purrr::map(dplyr::select, -dplyr::matches("\\.|boot")) %>%
purrr::map(~ purrr::map(., ~ names(sm$models)[order(
rank(-., ties.method = "random"))])) %>%
purrr::map(~ rlang::exec(rbind, !!!.)) %>%
purrr::map_chr(~ {
if (ncol(.) > 1) {
RankAggreg::RankAggreg(., ncol(.), method = "GA", seed = seed_rank,
verbose = FALSE)$top.list[1]
} else {
.[1]
}
})
# Distinct number of top algorithms and models on full data
ensemble_algs <- bests %>%
table() %>%
sort() %>%
rev() %>%
utils::head(top) %>%
names()
ensemble_mods <- ensemble_algs %>%
purrr::map(classification, data = data, class = class, rfe = rfe)
# Conditionally evaluate sequential model and prediction on full data
if (sequential) {
seq_mods <- sequential_train(sm, data, class)
seq_preds <- sequential_pred(seq_mods, sm, data, class)
} else {
seq_mods <- seq_preds <- NULL
}
tibble::lst(bests, ensemble_algs, ensemble_mods, seq_mods, seq_preds)
}