/
Empirical_Study_of_Ensemble_Learning_Methods.R
839 lines (564 loc) · 30 KB
/
Empirical_Study_of_Ensemble_Learning_Methods.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
### Getting Started
rm(list = ls())
# models
library(ranger) # random forest, extremely randomized trees
library(xgboost) # xgboost
library(MASS) # linear discriminant analysis, quadratic discriminant analysis
library(mda) # flexible discriminant analysis, mixture discriminant analysis
library(neuralnet) # artificial neural networks
library(class) # k-nearest neighbors
library(glmnet) # elastic net
library(kernlab) # gaussian processes
library(e1071) # support vector machines, naive bayes
library(partykit) # conditional inference tree
# misc
library(caret) # confusionMatrix()
library(parallel) # parallel computing
library(foreach) # parallel computing
library(doParallel) # parallel computing
library(tictoc) # time processes
library(beepr) # sound an alert when a process is finished
# helper functions
cv_parallel <- function(n, expr, ...) {
# function to perform parallel repeated k-fold cross-validation
# accepts as input a function kfcv() (defined for each base learner)
rowMeans(simplify2array(mclapply(
integer(n), eval.parent(substitute(function(...) expr)), ...)))
}
# Cohen's Kappa: a loss function appropriate for imbalanced classes
Kappa <- function(pred, actual) {confusionMatrix(pred, actual)[[3]][2]}
# convert any binary outcome to a factor with levels 0 and 1
binary <- function(vec) {factor(ifelse(vec == unique(vec)[1], 1, 0))}
# center and scale numeric variables (and leave factors alone)
standardize <- function(df) {rapply(df, scale, c("numeric", "integer"), how = "replace")}
# convert to a design matrix with one-hot-encoding
ohe <- function(df) {model.matrix(Y ~ 0 + ., df)}
# one-hot-encoding (dataframe output)
ohe.data.frame <- function(df) {data.frame(Y = df$Y, model.matrix(Y ~ 0 + ., df))}
phon <- "https://raw.githubusercontent.com/jbrownlee/Datasets/master/phoneme.csv"
phon <- read.csv(phon)[ , c(6, 1:5)]
colnames(phon) <- c("Y", paste0("X", 1:5))
phon$Y <- binary(phon$Y)
spam <- "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
spam <- read.csv(spam)[ , c(58, 1:57)]
colnames(spam) <- c("Y", paste0("X", 1:57))
spam$Y <- binary(spam$Y)
wdbc <- "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data"
wdbc <- read.csv(wdbc)[ , -1]
colnames(wdbc) <- c("Y", paste0("X", 1:30))
wdbc$Y <- binary(wdbc$Y)
adult <- "http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
adult <- read.csv(adult)[ , c(15, 1:14)]
colnames(adult) <- c("Y", paste0("X", 1:14))
adult$Y <- binary(adult$Y)
park <- "http://archive.ics.uci.edu/ml/machine-learning-databases/parkinsons/parkinsons.data"
park <- read.csv(park)[ , c(18, 2:17, 19:24)]
colnames(park) <- c("Y", paste0("X", 1:22))
park$Y <- binary(park$Y)
# For faster computations, make k, c, and g smaller
k <- 5 # number of folds in cross-validation
c <- 4 # number of times to repeat cross-validation
g <- 50 # max rows to sample in random grid search
df <- wdbc # current data set
#########################################################################################
### Parameter Tuning: XGBoost
xgb.DMatrix <- xgb.DMatrix(sparse.model.matrix(Y ~ 0 + ., data = df),
label = as.numeric(df$Y) - 1)
searchGrid <- expand.grid(subsample = c(0.40, 0.55, 0.70, 0.85, 1.0),
colsample_bytree = c(0.5, 0.6, 0.7, 0.8, 0.9, 1.0),
colsample_bynode = c(0.5, 0.75, 1),
max_depth = c(4, 6, 8, 10, 12, 14, 16),
eta = c(0.001, 0.01, .1, 0.3))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g*3)
cv_error <- foreach (i = 1:c, .combine = 'list', .multicombine = TRUE) %do% {
tictoc::tic()
cv_error <- t(apply(searchGrid, 1, function(params) {
cv_log <- xgb.cv(data = xgb.DMatrix, # training sample
nround = 5000, # maximum number of trees
early_stopping_rounds = 20, # stopping threshold if no improvement
objective = "binary:logistic", # objective function
eval_metric = "error", # evaluation metric
maximize = FALSE, # want to MINIMIZE error
max.depth = params[["max_depth"]], # tree depth
eta = params[["eta"]], # learning rate
gamma = 0, # minimum loss reduction
subsample = params[["subsample"]], # sample fraction of original data
colsample_bytree = params[["colsample_bytree"]], # how many features sampled, each tree
colsample_bynode = params[["colsample_bynode"]], # how many features sampled, each node
verbose = FALSE,
showsd = FALSE,
nfold = k, # number of cv folds
stratified = TRUE) # stratify folds to balance classes
best_error <- min(cv_log$evaluation_log[ , test_error_mean])
best_rounds <- match(best_error, cv_log$evaluation_log[ , test_error_mean])
return(c("error" = best_error, "trees" = best_rounds, params))
})); tictoc::toc()
return(cv_error)
}
cv_error <- apply(simplify2array(cv_error), 1:2, mean) # mean error across all cycles
head(cv_error[order(cv_error[ , 1]), ]) # minimum error
xgb.opt <- cv_error[order(cv_error[ , 1])[1], ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Extremely Randomized Trees
searchGrid <- expand.grid("mtry" = unique(floor(seq(5, ncol(df) - 1, length.out = 15))),
"max.depth" = c(4, seq(5, 50, by = 5)))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
# k-fold cross-validation
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(x, y) {
model <- ranger(Y ~ . ,
splitrule = "extratrees",
replace = F,
sample.fraction = 1,
data = train,
num.trees = 300,
mtry = x,
max.depth = y)
Kappa(predict(model, test)$predictions, df$Y[fold == i])
}, x = searchGrid$mtry, y = searchGrid$max.depth)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(max(c, 3), kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
xt.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Random Forest
searchGrid <- expand.grid("mtry" = unique(floor(seq(5, ncol(df) - 1, length.out = 15))),
"max.depth" = c(4, seq(5, 50, by = 5)))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
# k-fold cross-validation
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(x, y) {
model <- ranger(Y ~ . ,
data = train,
num.trees = 300,
mtry = x,
max.depth = y)
Kappa(predict(model, test)$predictions, df$Y[fold == i])
}, x = searchGrid$mtry, y = searchGrid$max.depth)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(max(c, 3), kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
rf.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Elastic Net
alpha <-seq(0, 1, by = 0.025) # elastic net mixing parameter
# function to find best shrinkage parameter (learning rate) for each value of alpha
best_lambda <- function(alpha) {
# glmnet standardizes variables internally
cv.glmnet(ohe(df),
df$Y,
type.measure = "deviance",
alpha = alpha,
nfolds = k,
family = "binomial")$lambda.min}
tictoc::tic()
lambda <- mclapply(alpha, function(x) {mean(replicate(c, best_lambda(x)))})
tictoc::toc()
searchGrid <- data.frame(alpha = alpha, lambda = unlist(lambda))
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- ohe(df)[fold != i, ]
test <- ohe(df)[fold == i, ]
mapply(function(x, y) {
model <- glmnet(train, df$Y[fold != i],
alpha = x,
lambda = y,
family = "binomial")
Kappa(factor(predict(model, test, type = "class")),
df$Y[fold == i])
}, x = searchGrid$alpha, y = searchGrid$lambda)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
en.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: k-Nearest Neighbors
neighbors <- min(g, 30, floor(((k-1)/k)*nrow(df)) - 1)
neighbors <- sort(sample(neighbors)[1:g])
# k-fold cross-validation
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- ohe(standardize(df))[fold != i, ]
test <- ohe(standardize(df))[fold == i, ]
unlist(mclapply(neighbors, function(x) {Kappa(knn(train, test,
cl = df$Y[fold != i], k = x),
df$Y[fold == i])},
mc.cores = detectCores() - 1))
}
return(rowMeans(cv_error))
}
tic()
cv_error <- data.frame(neighbors, kappa = rowMeans(replicate(c, kfcv())))
toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
knn.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
# Parameter Tuning: Single Hidden Layer Neural Network
searchGrid <- expand.grid(hidden = floor(seq(0.25*(ncol(ohe(df))),
1.50*(ncol(ohe(df))),
by = 1)),
algorithm = c("rprop+", "rprop-"))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- ohe.data.frame(standardize(df))[fold != i, ]
test <- ohe.data.frame(standardize(df))[fold == i, -1]
mapply(function(x, y) {
nn <- neuralnet(Y ~ . ,
data = train,
hidden = x,
algorithm = y,
rep = 1,
stepmax = 1e5,
threshold = 0.3,
linear.output = FALSE)
Kappa(factor(round(predict(nn, test)[ , 2])), df$Y[fold == i])
}, x = searchGrid$hidden, y = searchGrid$algorithm)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
nn.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Conditional Inference Tree
searchGrid <- expand.grid(alpha = c(0.001, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25),
maxdepth = c(1:5, Inf))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(x, y) {
model <- ctree(Y ~ ., alpha = x, maxdepth = y, data = train)
Kappa(factor(predict(model, test)), df$Y[fold == i])
}, x = searchGrid$alpha, y = searchGrid$maxdepth)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
ct.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Support Vector Machine (Linear Kernel)
searchGrid <- expand.grid(kernel = "linear",
cost = c(0.0001, 0.0005, 0.001, 0.005, 0.01, 0.04, 0.05, 0.08, 0.1, 1, 5),
epsilon = c(0.001, 0.01, 0.1, 0.5))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(x, y, z) {
model <- svm(Y ~ .,
data = train,
kernel = x,
cost = y,
epsilon = z)
Kappa(factor(predict(model, test)), df$Y[fold == i])
}, x = searchGrid$kernel, y = searchGrid$cost, z = searchGrid$epsilon)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
svml.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Support Vector Machine (Polynomial Kernel)
searchGrid <- expand.grid(kernel = "polynomial",
degree = 1:5,
coef0 = c(0.0001, 0.0005, 0.001, 0.01, 0.05, 0.1, 0.5, 1, 2, 5, 10),
cost = c(0.0001, 0.0005, 0.001, 0.005, 0.01, 0.03, 0.05, 0.1),
epsilon = c(0.001, 0.01, 0.1))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(v, w, x, y, z) {
model <- svm(Y ~ .,
data = train,
kernel = v,
degree = w,
coef0 = x,
cost = y,
epsilon = z)
Kappa(factor(predict(model, test)), df$Y[fold == i])
}, v = searchGrid$kernel, w = searchGrid$degree, x = searchGrid$coef0,
y = searchGrid$cost, z = searchGrid$epsilon)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
svmp.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
#########################################################################################
### Parameter Tuning: Support Vector Machine (Radial Kernel)
searchGrid <- expand.grid(kernel = "radial",
cost = c(0.0001, 0.0005, 0.001, 0.005, 0.01, 0.04, 0.05, 0.08, 0.1, 1, 5),
gamma = c(0.001, 0.005, 0.1, 0.5, 1, 2, 3, 4))
searchGrid <- head(searchGrid[sample(1:nrow(searchGrid), nrow(searchGrid)), ], g)
kfcv <- function(i = k) {
fold <- sample(cut(1:nrow(df), breaks = i, labels = FALSE))
cv_error <- foreach (i = 1:k, .combine = 'cbind') %do% {
train <- df[fold != i, ]
test <- df[fold == i, -1]
mapply(function(x, y, z) {
model <- svm(Y ~ .,
data = train,
kernel = x,
cost = y,
gamma = z)
Kappa(factor(predict(model, test)), df$Y[fold == i])
}, x = searchGrid$kernel, y = searchGrid$cost, z = searchGrid$gamma)
}
return(rowMeans(cv_error))
}
tic(); cv_error <- cbind(searchGrid, kappa = cv_parallel(c, kfcv())); toc()
head(cv_error[rev(order(cv_error$kappa)), ]) # minimum error
svmr.opt <- cv_error[which.max(cv_error$kappa), ] # optimal hyperparameters
# best kernel (radial, linear, polynomial)
svm.list <- list(svmr.opt, svml.opt, svmp.opt)
svm.opt <- svm.list[[which.max(sapply(svm.list, `[`, c("kappa")))]]
svm.opt # best support vector machine in the discovery set
#########################################################################################
################################ ENSEMBLE EXPERIMENTS ###################################
#########################################################################################
k <- 5; # number of folds in k-fold cross validation
c <- 40; # number of times k-fold CV is repeated
# initialize vectors
cycles.ct <- c(); CV.ct <- NULL; # conditional inference tree
cycles.knn <- c(); CV.knn <- NULL; # k-nearest neighbors
cycles.rf <- c(); CV.rf <- NULL; # random forest
cycles.xt <- c(); CV.xt <- NULL; # extremely randomized trees
cycles.xgb <- c(); CV.xgb <- NULL; # extreme gradient boosting
cycles.gp <- c(); CV.gp <- NULL; # gaussian processes
cycles.qda <- c(); CV.qda <- NULL; # quadratic discriminant analysis
cycles.lda <- c(); CV.lda <- NULL; # linear discriminant analysis
cycles.nn <- c(); CV.nn <- NULL; # neural network
cycles.glm <- c(); CV.glm <- NULL; # logistic regression
cycles.en <- c(); CV.en <- NULL; # elastic net
cycles.svm <- c(); CV.svm <- NULL; # support vector machine
cycles.nb <- c(); CV.nb <- NULL; # naive bayes classifier
cycles.fda <- c(); CV.fda <- NULL; # flexible discriminant analysis with MARS
cycles.ens <- c(); CV.ens <- NULL; # voting ensemble
x <- matrix(list(), nrow = c, ncol = k) # cross-validation error
tictoc::tic(); for (j in 1:c) {
fold <- sample(cut(1:nrow(df), breaks = k, labels = FALSE))
for (i in 1:k){
cat("Fold", i, "Cycle", j, "\n")
# partition into k folds
train <- df[fold != i, ]
test <- df[fold == i, -1]
# conditional inference trees
ctree <- ctree(Y ~ ., alpha = ct.opt$alpha, maxdepth = ct.opt$maxdepth, data = train)
CV.ct[[i]] <- Kappa(predict(ctree, test), df$Y[fold == i])
# random forest
rf <- ranger(Y ~ . , mtry = rf.opt$mtry, max.depth = rf.opt$max.depth,
data = train, num.trees = 300)
CV.rf[[i]] <- Kappa(predict(rf, test)$predictions, df$Y[fold == i])
# extremely randomized trees
xt <- ranger(Y ~ . , mtry = xt.opt$mtry, max.depth = xt.opt$max.depth,
data = train, num.trees = 300,
splitrule = "extratrees", replace = F, sample.fraction = 1)
CV.xt[[i]] <- Kappa(predict(xt, test)$predictions, df$Y[fold == i])
# XGBoost
train_xgb <- sparse.model.matrix(Y ~ 0 + . , data = train)
test_xgb <- sparse.model.matrix(Y ~ 0 + . , data = df[fold == i, ])
params <- list(gamma = 0,
booster = "gbtree",
objective = "binary:logistic",
eta = xgb.opt["eta"],
subsample = xgb.opt["subsample"],
colsample_bytree = xgb.opt["colsample_bytree"],
colsample_bynode = xgb.opt["colsample_bynode"],
max_depth = xgb.opt["max_depth"])
xgb <- xgboost(data = train_xgb, label = as.numeric(train$Y) - 1,
params = params, nrounds = xgb.opt["trees"], verbose = FALSE)
CV.xgb[[i]] <- Kappa(factor(round(predict(xgb, test_xgb))), df$Y[fold == i])
# knn
train_knn <- ohe(standardize(df))[fold != i, ]
test_knn <- ohe(standardize(df))[fold == i, ]
knn <- knn(train_knn, test_knn, cl = train$Y, k = knn.opt$neighbors)
CV.knn[[i]] <- Kappa(knn, df$Y[fold == i])
# quadratic discriminant analysis
qda <- qda(Y ~ . , data = train)
CV.qda[[i]] <- Kappa(predict(qda, test)$class, df$Y[fold == i])
# linear discriminant analysis
lda <- lda(Y ~ . , data = train)
CV.lda[[i]] <- Kappa(predict(lda, test)$class, df$Y[fold == i])
# logistic regression
glm <- glm(Y ~ ., data = train, family = "binomial", control = list(maxit = 1000))
CV.glm[[i]] <- Kappa(factor(round(predict(glm, test, type = "response"))), df$Y[fold == i])
# neural network
train_nn <- ohe.data.frame(standardize(df))[fold != i, ]
test_nn <- ohe.data.frame(standardize(df))[fold == i, -1]
nn <- neuralnet(Y ~ . , data = train_nn, hidden = nn.opt$hidden, algorithm = nn.opt$algorithm,
rep = 1, stepmax = 1e5, linear.output = FALSE, threshold = 0.3)
CV.nn[[i]] <- Kappa(factor(round(predict(nn, test_nn)[ , 2])), df$Y[fold == i])
# elastic net
train_en <- ohe(df)[fold != i, ]
test_en <- ohe(df)[fold == i, ]
en <- glmnet(train_en, train$Y, alpha = en.opt$alpha, lambda = en.opt$lambda,
family = "binomial")
CV.en[[i]] <- Kappa(factor(round(predict(en, test_en, type = "response"))), df$Y[fold == i])
# support vector machine
if (svm.opt$kernel == "linear") {
svm <- svm(Y ~ . , data = train, kernel = svm.opt$kernel, cost = svm.opt$cost,
epsilon = svm.opt$epsilon)
} else if (svm.opt$kernel == "radial") {
svm <- svm(Y ~ . , data = train, kernel = svm.opt$kernel, cost = svm.opt$cost,
gamma = svm.opt$gamma)
} else if (svm.opt$kernel == "sigmoid") {
svm <- svm(Y ~ . , data = train, kernel = svm.opt$kernel, cost = svm.opt$cost,
gamma = svm.opt$gamma, coef0 = svm.opt$coef0)
} else if (svm.opt$kernel == "polynomial") {
svm <- svm(Y ~ . , data = train, kernel = svm.opt$kernel, cost = svm.opt$cost,
degree = svm.opt$degree, coef0 = svm.opt$coef0, epsilon = svm.opt$epsilon)
}
CV.svm[[i]] <- Kappa(predict(svm, test), df$Y[fold == i])
# naive bayes
nb <- naiveBayes(Y ~ . , data = train)
CV.nb[[i]] <- Kappa(predict(nb, test), df$Y[fold == i])
# flexible discriminant analysis (MARS)
fda <- fda(Y ~ ., data = train, method = mars)
CV.fda[[i]] <- Kappa(predict(fda, test), df$Y[fold == i])
# gaussian processes
gp <- gausspr(Y ~ ., data = train, kpar = list(sigma = 0.1),
type = "classification")
CV.gp[[i]] <- Kappa(predict(gp, test), df$Y[fold == i])
# all predictions
x[[j, i]] <- data.frame(ct = predict(ctree, test),
knn = knn,
gp = predict(gp, test),
rf = predict(rf, test)$predictions,
xt = predict(xt, test)$predictions,
xgb = factor(round(predict(xgb, test_xgb))),
qda = predict(qda, test)$class,
lda = predict(lda, test)$class,
nn = factor(round(predict(nn, test_nn)[ , 2])),
glm = factor(round(predict(glm, test, type = "response"))),
en = factor(round(predict(en, test_en, type = "response"))),
svm = predict(svm, test),
nb = predict(nb, test),
fda = predict(fda, test),
actual = df$Y[fold == i])
# (post-hoc) simple majority vote ensemble with three of the best base learners
ens <- data.frame(x[[j, i]]$rf, x[[j, i]]$svm, x[[j, i]]$en)
mv <- factor(apply(ens, 1, function(x) names(which.max(table(x)))))
CV.ens[[i]] <- Kappa(mv, df$Y[fold == i])
x[[j, i]] <- data.matrix(x[[j, i]]) - 1
}
cycles.ct <- c(cycles.ct, mean(CV.ct ))
cycles.knn <- c(cycles.knn, mean(CV.knn))
cycles.rf <- c(cycles.rf, mean(CV.rf ))
cycles.xt <- c(cycles.xt, mean(CV.xt ))
cycles.xgb <- c(cycles.xgb, mean(CV.xgb))
cycles.qda <- c(cycles.qda, mean(CV.qda))
cycles.lda <- c(cycles.lda, mean(CV.lda))
cycles.glm <- c(cycles.glm, mean(CV.glm))
cycles.nn <- c(cycles.nn, mean(CV.nn ))
cycles.en <- c(cycles.en, mean(CV.en ))
cycles.svm <- c(cycles.svm, mean(CV.svm))
cycles.nb <- c(cycles.nb, mean(CV.nb ))
cycles.fda <- c(cycles.fda, mean(CV.fda))
cycles.gp <- c(cycles.gp, mean(CV.gp ))
cycles.ens <- c(cycles.ens, mean(CV.ens))
# beepr::beep()
}; beepr::beep(3); tictoc::toc()
# format convenient for visualization
all <- data.frame(kappa = c(cycles.fda, cycles.nb, cycles.ct, cycles.en, cycles.svm,
cycles.ens, cycles.knn, cycles.nn, cycles.rf, cycles.xt,
cycles.glm, cycles.lda, cycles.qda, cycles.xgb, cycles.gp),
model = rep(c("fda", "nb", "ct", "en", "svm", "ens", "knn", "nn",
"rf", "xt", "glm", "lda", "qda", "xgb", "gp"),
each = c))
all$model <- with(all, reorder(model, 1 - kappa, median))
aggregate(kappa ~ model, all, mean) # mean accuracy
boxplot(all$kappa ~ all$model, outline = FALSE, xlab = "Model", ylab = "Accuracy",
main = paste("Model Performance Across", c, "Cycles of k-Fold CV"))
n <- ncol(x[[1, 1]]) - 1 # number of models
for(i in 1:(n + 1)){
levelProportions <- summary(all$model)/nrow(all)
values <- all[all$model == levels(all$model)[i], "kappa"]
jitter <- jitter(rep(i, length(values)), amount=levelProportions[i]/0.8)
points(jitter, values, pch = 19, col = rgb(1, 0, 0, .3), cex = 0.5)
}
#########################################################################################
########################### EVALUATE ALL POSSIBLE ENSEMBLES #############################
#########################################################################################
voters <- do.call(expand.grid, replicate(n, list(0:1)))[-1, ]
voters <- voters[apply(voters, 1, function(x) sum(x) %% 2 != 0), ]
voters[voters == 0] <- NA
tictoc::tic()
cl <- makeCluster(detectCores() - 1)
registerDoParallel(cl)
scores <- rowMeans(sapply(1:c, function(l){
rowMeans(sapply(1:k, function(j){
foreach(i = 1:nrow(voters), .combine=rbind, .packages = "caret",
.export = c("Kappa", "x", "l", "k", "n", "voters", "df")) %dopar% {
Kappa(factor(ifelse(apply(data.frame(mapply(`*`, data.frame(x[[l, j]][ , -(n + 1)]), as.numeric(voters[i, ]))),
1, function(x){mean(x, na.rm = TRUE)}) > 0.5, 1, 0)), factor(x[[l, j]][ , n + 1])) *
(nrow(x[[1, j]])/nrow(df))/(1/k)
}}))}))
stopCluster(cl)
tictoc::toc() # 4696.182 sec elapsed
names(voters) <- colnames(x[[1, 1]][ , -(n + 1)])
# best model
scores[which.max(scores)] # over 96% accuracy
voters[which.max(scores), ] # best ensemble
# best models
cbind.data.frame(voters[head(order(scores, decreasing = TRUE), 30), ], # good choices
kappa = head(sort(scores, decreasing = TRUE), 30))
# worst models
cbind.data.frame(voters[head(order(scores), 10), ],
kappa = head(sort(scores), 10))
#########################################################################################
########################### LOOKING AT RESULTS FOR WDBC DATA ############################
#########################################################################################
# best models that do not include elastic net or svm
new.mat <- scores[is.na(voters$en) & is.na(voters$svm)]
voters.without.en <- subset(voters, is.na(voters$en) & is.na(voters$svm)) # 95% accuracy
cbind.data.frame(1:30,
voters.without.en[head(order(new.mat, decreasing = TRUE), 30), ],
kappa = head(sort(new.mat, decreasing = TRUE), 30))
# ensemble that has the best performance increase relative to its base learners
base <- aggregate(kappa ~ model, subset(all, model != "ve"), mean)
base <- base[match(colnames(voters), base$model), ]
base <- matrix(rep(base$kappa, nrow(voters)), nrow = nrow(voters), byrow = TRUE)
diff <- cbind.data.frame(base*voters, scores,
100*(scores/apply(base*voters, 1, max, na.rm = TRUE) - 1))
colnames(diff) <- c(colnames(voters), "kappa", "percent_diff")
head(round(diff[order(diff$percent_diff, decreasing = TRUE), ], 2), 10)
# One ensemble -- conditional inference tree, linear discriminant analysis, and logistic
# regression -- achieves kappa = 0.93, a 2.56% increase over the best base learner.
head(round(diff[order(diff$percent_diff), ], 3), 10)
# One ensemble -- conditional inference tree, elastic net, and naive bayes --
# achieves kappa = 0.912, a 4.37% decrease over the best base learner.
hist(subset(diff, percent_diff != min(percent_diff))$percent_diff,
xlim = c(-4.4, 2.6), breaks = 25, main = "Most Voting Ensembles Do Worse, Not Better",
xlab = "Percent Increase/Decrease in Performance Over the Best Base Learner",
col = "darkblue",
ylab = "Number of Voting Ensembles")
abline(v = 0, col = "red", lwd = 1, lty = 2)
#########################################################################################