/
2_3_textTrainRandomForest.R
1331 lines (1192 loc) · 50.2 KB
/
2_3_textTrainRandomForest.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Select evaluation measure and compute it (also used in logistic regression)
#'
#' @param eval_measure Measure used to evaluate and select models default: "roc_auc", see also
#' "accuracy", "bal_accuracy", "sens", "spec", "precision", "kappa", "f_measure".
#' @param holdout_pred data with observed (truth) and predicted
#' @param truth name of the truth column
#' @param estimate name of the predicted estimate
#' @return tibble with .metric column (i.e., type of evaluation measure), .estimator (e.g., binary)
#' and .estimate (i.e., the resulting value).
#' @importFrom dplyr all_of
#' @importFrom yardstick accuracy bal_accuracy sens spec precision kap f_meas roc_auc rmse rsq
#' @importFrom tibble tibble
#' @importFrom stats cor.test
#' @noRd
select_eval_measure_val <- function(eval_measure = "bal_accuracy",
holdout_pred = NULL,
truth = y,
estimate = .pred_class, class,
...) {
# Get accuracy
if (eval_measure == "accuracy") {
eval_measure_val <- yardstick::accuracy(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "bal_accuracy") {
eval_measure_val <- yardstick::bal_accuracy(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "sens") {
eval_measure_val <- yardstick::sens(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "spec") {
eval_measure_val <- yardstick::spec(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "precision") {
eval_measure_val <- yardstick::precision(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "kappa") {
eval_measure_val <- yardstick::kap(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "f_measure") {
eval_measure_val <- yardstick::f_meas(holdout_pred, truth = y, estimate = .pred_class)
} else if (eval_measure == "roc_auc") {
class1_name <- eval(class)
eval_measure_val <- yardstick::roc_auc(holdout_pred, truth = y, dplyr::all_of(class1_name))
} else if (eval_measure == "rmse") {
eval_measure_val <- yardstick::rmse(holdout_pred, truth = y, estimate = .pred)
} else if (eval_measure == "rsq") {
eval_measure_val <- yardstick::rsq(holdout_pred, truth = y, estimate = .pred)
} else if (eval_measure == "cor_test") {
cor_testing <- stats::cor.test(holdout_pred$y, holdout_pred$.pred, na.action = na.omit)
estimate1 <- cor_testing[[4]][[1]]
metric <- "cor_test"
estimator <- "standard"
eval_measure_val <- tibble::tibble(metric, estimator, as.numeric(estimate1))
colnames(eval_measure_val) <- c(".metric", ".estimator", ".estimate")
}
eval_measure_val
}
#' Select evaluation measure and compute it (also used in logistic regression)
#'
#' @param outputlist_results_outer Results from outer predictions.
#' @param id_nr ID numbers
#' @param simulate.p.value (Boolean) From fisher.test: a logical indicating whether to
#' compute p-values by Monte Carlo simulation, in larger than 2 × 2 tables.
#' @return returns sorted predictions and truth, chi-square test, fisher test, and all evaluation metrics/measures
#' @importFrom tidyr unnest
#' @importFrom tibble is_tibble
#' @importFrom dplyr full_join arrange bind_rows
#' @importFrom stats fisher.test
#' @importFrom yardstick accuracy bal_accuracy sens spec precision kap f_meas roc_auc rmse rsq
#' @importFrom ggplot2 autoplot
#' @noRd
classification_results <- function(outputlist_results_outer,
id_nr = NA,
simulate.p.value = simulate.p.value,
...) {
# Unnest predictions and y
predy_y <- tibble::tibble(
tidyr::unnest(outputlist_results_outer$truth, cols = c(truth)),
tidyr::unnest(outputlist_results_outer$estimate, cols = c(estimate)),
tidyr::unnest(outputlist_results_outer$.pred_1, cols = c(.pred_1)),
tidyr::unnest(outputlist_results_outer$.pred_2, cols = c(.pred_2)),
tidyr::unnest(outputlist_results_outer$id_nr, cols = c(id_nr))
)
if (tibble::is_tibble(id_nr)) {
predy_y <- predy_y %>% dplyr::full_join(id_nr, by = "id_nr")
}
predy_y <- predy_y %>% dplyr::arrange(id_nr)
# Correlate predictions and observed help(all_of)
chisq <- suppressWarnings(chisq.test(table(predy_y$truth, predy_y$estimate)))
fisher <- stats::fisher.test(predy_y$truth,
predy_y$estimate,
simulate.p.value = simulate.p.value
)
accuracy <- yardstick::accuracy(predy_y, truth, estimate, ...
)
bal_accuracy <- yardstick::bal_accuracy(predy_y, truth, estimate, ...
)
sens <- yardstick::sens(predy_y, truth, estimate, ...
)
spec <- yardstick::spec(predy_y, truth, estimate, ...
)
precision <- yardstick::precision(predy_y, truth, estimate, ...
)
kappa <- yardstick::kap(predy_y, truth, estimate, ...
)
f_measure <- yardstick::f_meas(predy_y, truth, estimate, ...
)
roc_auc <- yardstick::roc_auc(predy_y, truth, colnames(predy_y[3]))
roc_curve_data <- yardstick::roc_curve(predy_y, truth, colnames(predy_y[3]))
roc_curve_plot <- ggplot2::autoplot(yardstick::roc_curve(predy_y, truth, colnames(predy_y[3])))
results_collected <- dplyr::bind_rows(
accuracy,
bal_accuracy,
sens,
spec,
precision,
kappa,
f_measure,
roc_auc
)
output <- list(predy_y, roc_curve_data, roc_curve_plot, fisher, chisq, results_collected)
names(output) <- c("predy_y", "roc_curve_data", "roc_curve_plot", "fisher", "chisq", "results_collected")
output
}
#' Select evaluation measure and compute it (also used in logistic regression)
#'
#' @param outputlist_results_outer Results from outer predictions.
#' @param id_nr ID numbers
#' @param simulate.p.value (Boolean) From fisher.test: a logical indicating whether to
#' compute p-values by Monte Carlo simulation, in larger than 2 × 2 tables.
#' @return returns sorted predictions and truth, chi-square test, fisher test, and all evaluation metrics/measures
#' @importFrom tidyr unnest
#' @importFrom tibble is_tibble
#' @importFrom dplyr full_join arrange bind_rows
#' @importFrom stats fisher.test
#' @importFrom yardstick accuracy bal_accuracy sens spec precision kap f_meas roc_auc rmse rsq
#' @importFrom ggplot2 autoplot
#' @noRd
classification_results_multi <- function(outputlist_results_outer,
id_nr = NA,
simulate.p.value = FALSE,
...) {
predy_y <- tibble::tibble(
tidyr::unnest(outputlist_results_outer$truth, cols = c(truth)),
tidyr::unnest(outputlist_results_outer$estimate, cols = c(estimate)),
tidyr::unnest(outputlist_results_outer$id_nr, cols = c(id_nr))
)
for (i in 1:(length(unique(levels(outputlist_results_outer$truth$truth[[1]])))))
{
predy_y[3 + i] <- tibble::tibble(tidyr::unnest(outputlist_results_outer[[i]], cols = c(paste(".pred_", i, sep = ""))))
}
if (tibble::is_tibble(id_nr)) {
predy_y <- predy_y %>% dplyr::full_join(id_nr, by = "id_nr")
}
predy_y <- predy_y %>% dplyr::arrange(id_nr)
# Correlate predictions and observed help(all_of)
chisq <- suppressWarnings(chisq.test(table(predy_y$truth, predy_y$estimate)))
fisher <- stats::fisher.test(predy_y$truth,
predy_y$estimate,
simulate.p.value = simulate.p.value
)
accuracy <- yardstick::accuracy(predy_y, truth, estimate, ...
)
bal_accuracy <- yardstick::bal_accuracy(predy_y, truth, estimate, ...
)
sens <- yardstick::sens(predy_y, truth, estimate, ...
)
spec <- yardstick::spec(predy_y, truth, estimate, ...
)
precision <- yardstick::precision(predy_y, truth, estimate, ...
)
kappa <- yardstick::kap(predy_y, truth, estimate, ...
)
f_measure <- yardstick::f_meas(predy_y, truth, estimate, ...
)
roc_auc <- yardstick::roc_auc(predy_y, truth, colnames(predy_y[4:(length(predy_y))]))
roc_curve_data <- yardstick::roc_curve(predy_y, truth, colnames(predy_y[4:(length(predy_y))]))
roc_curve_plot <- ggplot2::autoplot(yardstick::roc_curve(predy_y, truth, colnames(predy_y[4:(length(predy_y))])))
results_collected <- dplyr::bind_rows(
accuracy,
bal_accuracy,
sens,
spec,
precision,
kappa,
f_measure,
roc_auc
)
output <- list(predy_y, roc_curve_data, roc_curve_plot, fisher, chisq, results_collected)
names(output) <- c("predy_y", "roc_curve_data", "roc_curve_plot", "fisher", "chisq", "results_collected")
output
}
#' Function to fit a model and compute accuracy
#'
#' @param object An rsplit object (from results_nested_resampling tibble)
#' object = results_nested_resampling$splits[[1]]
#' @param mode_rf default "classification"; see also "unknown" and "regression".
#' @param extremely_randomised_splitrule default: "extratrees", see also: "gini" or "hellinger"
#' @param mtry hyperparameter for random forest.
#' @param min_n hyperparameter for random forest.
#' @param trees number of trees
#' @param eval_measure Measure used to evaluate and select models default: "roc_auc", see also
#' "accuracy", "bal_accuracy", "sens", "spec", "precision", "kappa", "f_measure".
#' @return Accuracy
#' @noRd
fit_model_accuracy_rf <- function(object,
mode_rf = "classification",
mtry = 1,
min_n = 1,
trees = 1000,
preprocess_step_center = TRUE,
preprocess_scale_center = TRUE,
preprocess_PCA = NA,
variable_name_index_pca = NA,
eval_measure = "bal_accuracy",
extremely_randomised_splitrule = NULL) {
data_train <- rsample::analysis(object)
data_train <- tibble::as_tibble(data_train)
# Get number of embeddings provided
n_embeddings <- as.numeric(comment(eval_measure))
# Recipe for one embedding input
if (n_embeddings == 1) {
xy_recipe <- rsample::analysis(object) %>%
recipes::recipe(y ~ .) %>%
recipes::update_role(id_nr, new_role = "id variable") %>% # New
# recipes::update_role(-id_nr, new_role = "predictor") %>% # New
recipes::update_role(y, new_role = "outcome") # %>% # New
if ("strata" %in% colnames(data_train)) {
xy_recipe <- xy_recipe %>%
recipes::update_role(strata, new_role = "strata")
}
xy_recipe <- xy_recipe %>%
recipes::step_naomit(recipes::all_predictors(), skip = FALSE) # %>%
# recipes::step_BoxCox(recipes::all_predictors()) %>%
if (preprocess_step_center) {
xy_recipe <- recipes::step_center(xy_recipe, recipes::all_predictors())
}
if (preprocess_scale_center) {
xy_recipe <- recipes::step_scale(xy_recipe, recipes::all_predictors())
}
# If preprocess_PCA is not NULL add PCA step with number of component of % of variance to retain specification
xy_recipe <- xy_recipe %>%
# If preprocess_PCA is not NULL add PCA step with number of component of % of variance to retain specification
{
if (!is.na(preprocess_PCA)) {
if (preprocess_PCA >= 1) {
recipes::step_pca(., recipes::all_predictors(), num_comp = preprocess_PCA)
} else if (preprocess_PCA < 1) {
recipes::step_pca(., recipes::all_predictors(), threshold = preprocess_PCA)
} else {
.
}
} else {
.
}
}
xy_recipe_prep <- recipes::prep(xy_recipe)
# Recipe for multiple word embedding input (with possibility of separate PCAs)
} else {
xy_recipe <- rsample::analysis(object) %>%
recipes::recipe(y ~ .) %>%
# recipes::step_BoxCox(all_predictors()) %>% preprocess_PCA = NULL, preprocess_PCA = 0.9 preprocess_PCA = 2
recipes::update_role(id_nr, new_role = "id variable") %>%
# recipes::update_role(-id_nr, new_role = "predictor") %>%
recipes::update_role(y, new_role = "outcome") # %>%
if ("strata" %in% colnames(data_train)) {
xy_recipe <- xy_recipe %>%
recipes::update_role(strata, new_role = "strata")
}
xy_recipe <- xy_recipe %>%
recipes::step_naomit(recipes::all_predictors(), skip = FALSE) %>%
recipes::step_center(recipes::all_predictors()) %>%
recipes::step_scale(recipes::all_predictors())
# Adding a PCA in each loop; first selecting all variables starting with i="Dim_we1"; and then "Dim_we2" etc
if (!is.na(preprocess_PCA)) {
if (preprocess_PCA >= 1) {
for (i in variable_name_index_pca) {
xy_recipe <-
xy_recipe %>%
# !! slices the current name into the `matches()` function.
# We use a custom prefix so there are no name collisions for the
# results of each PCA step.
recipes::step_pca(dplyr::matches(!!i), num_comp = preprocess_PCA, prefix = paste("PCA", i, sep = "_"))
}
} else if (preprocess_PCA < 1) {
for (i in variable_name_index_pca) {
xy_recipe <-
xy_recipe %>%
recipes::step_pca(dplyr::matches(!!i), threshold = preprocess_PCA, prefix = paste("PCA", i, sep = "_"))
}
}
}
xy_recipe_prep <- recipes::prep(xy_recipe)
xy_recipe_prep
}
# To load the prepared training data into a variable juice() is used.
# It extracts the data from the xy_recipe object.
# Number of predictors (if more than one should use standard linear regression)
# Create and fit model. help(rand_forest)
if (is.null(extremely_randomised_splitrule)) {
mod_spec <-
parsnip::rand_forest(mode = mode_rf, trees = trees, mtry = mtry, min_n = min_n) %>%
parsnip::set_engine("randomForest")
# Create Workflow (to know variable roles from recipes)
wf <- workflows::workflow() %>%
workflows::add_model(mod_spec) %>%
workflows::add_recipe(xy_recipe)
# Fit model
mod <- parsnip::fit(wf, data = data_train)
} else if (is.character(extremely_randomised_splitrule)) {
mod_spec <-
parsnip::rand_forest(mode = mode_rf) %>%
parsnip::set_engine("ranger", splitrule = extremely_randomised_splitrule) %>%
parsnip::set_mode("classification")
# Create Workflow (to know variable roles from recipes)
wf <- workflows::workflow() %>%
workflows::add_model(mod_spec) %>%
workflows::add_recipe(xy_recipe)
# Fit model
mod <- parsnip::fit(wf, data = data_train)
}
# Prepare the test data according to the recipe
xy_testing <- rsample::assessment(object)
# look at xy_testing: glimpse(xy_testing)
# Apply model on new data
if (length(levels(data_train$y)) == 2){
holdout_pred_class <-
stats::predict(mod, xy_testing %>%
dplyr::select(-y), type = c("class")) %>%
dplyr::bind_cols(rsample::assessment(object) %>% dplyr::select(y, id_nr))
holdout_pred <-
stats::predict(mod, xy_testing %>%
dplyr::select(-y), type = c("prob")) %>%
dplyr::bind_cols(rsample::assessment(object) %>% dplyr::select(y, id_nr))
holdout_pred$.pred_class <- holdout_pred_class$.pred_class
class <- colnames(holdout_pred[2])
eval_measure_val <- select_eval_measure_val(
eval_measure = eval_measure,
holdout_pred = holdout_pred,
truth = y,
estimate = .pred_class,
class = class
)
# Sort output of RMSE, predictions and truth (observed y)
output <- list(
list(eval_measure_val),
list(holdout_pred$.pred_class),
list(holdout_pred$y),
list(holdout_pred[1]),
list(holdout_pred[2]),
list(preprocess_PCA),
list(holdout_pred$id_nr)
) # New
names(output) <- c(
"eval_measure_val",
"estimate",
"truth",
".pred_1",
".pred_2",
"preprocess_PCA",
"id_nr"
) # New
} else if (length(levels(data_train$y)) > 2) {
holdout_pred_class <-
stats::predict(mod, xy_testing, type = c("class")) %>%
dplyr::bind_cols(rsample::assessment(object) %>%
dplyr::select(y, id_nr))
holdout_pred <-
stats::predict(mod, xy_testing, type = c("prob")) %>%
dplyr::bind_cols(rsample::assessment(object) %>%
dplyr::select(y, id_nr))
holdout_pred$.pred_class <- holdout_pred_class$.pred_class
# Get RMSE; eval_measure = "rmse"
eval_measure_val <- select_eval_measure_val(eval_measure,
holdout_pred = holdout_pred,
truth = y, estimate = .pred_class
) #$.estimate
# Sort output of RMSE, predictions and truth (observed y)
output <- list(
list(eval_measure_val),
list(holdout_pred$.pred_class),
list(holdout_pred$y)
)
for (i in 1:length(unique(levels(holdout_pred$y))))
{
output[[3 + i]] <- list(holdout_pred[i])
}
output[[length(output) + 1]] <- list(preprocess_PCA)
output[[length(output) + 1]] <- list(holdout_pred$id_nr)
pred_names <- list()
for (i in 1:length(unique(levels(holdout_pred$y))))
{
pred_names[i] <- paste(".pred_", i, sep = "")
}
names(output) <- c(
"eval_measure_val",
"estimate",
"truth",
pred_names,
"preprocess_PCA",
"id_nr"
)
}
output
}
# In some situations, we want to parameterize the function over the tuning parameter:
#' Function to fit a model and compute RMSE.
#'
#' @param object An rsplit object (from results_nested_resampling tibble)
#' object = results_nested_resampling$splits[[1]]
#' @param extremely_randomised_splitrule default: "extratrees", see also: "gini" or "hellinger"
#' @param mtry hyperparameter for random forest.
#' @param min_n hyperparameter for random forest.
#' @param trees number of trees
#' @param eval_measure Measure used to evaluate and select models default: "roc_auc", see also
#' "accuracy", "bal_accuracy", "sens", "spec", "precision", "kappa", "f_measure".
#' @return Accuracy
#' @noRd
fit_model_accuracy_wrapper_rf <- function(mtry,
min_n,
object,
mode_rf,
trees,
preprocess_step_center,
preprocess_scale_center,
preprocess_PCA,
variable_name_index_pca,
eval_measure,
extremely_randomised_splitrule) {
fit_model_accuracy_rf(
object,
mode_rf,
mtry,
min_n,
trees,
preprocess_step_center,
preprocess_scale_center,
preprocess_PCA,
variable_name_index_pca,
eval_measure,
extremely_randomised_splitrule
)
}
#' For the nested resampling, a model needs to be fit for each tuning parameter and each INNER split.
#'
#' @param object an rsplit object from the INNER samples
#' object=results_nested_resampling$inner_resamples[[1]]$splits[[1]]
#' @param extremely_randomised_splitrule default: "extratrees", see also: "gini" or "hellinger"
#' @param mtry hyperparameter for random forest.
#' @param min_n hyperparameter for random forest.
#' @param trees number of trees
#' @return Accuracy
#' @noRd
tune_over_cost_rf <- function(object,
mode_rf,
mtry,
min_n,
trees,
preprocess_step_center,
preprocess_scale_center,
preprocess_PCA,
variable_name_index_pca,
eval_measure,
extremely_randomised_splitrule) {
T1 <- Sys.time()
if (!is.na(preprocess_PCA[1])) {
# Number of components or percent of variance to attain; min_halving;
if (preprocess_PCA[1] == "min_halving") {
num_features <- length(rsample::analysis(object)) - 1
num_users <- nrow(rsample::analysis(object))
preprocess_PCA_value <- round(max(min(num_features / 2, num_users / 1.5), min(50, num_features)))
preprocess_PCA_value
} else if (preprocess_PCA[1] >= 1) {
preprocess_PCA_value <- preprocess_PCA
} else if (preprocess_PCA[1] < 1) {
preprocess_PCA_value <- preprocess_PCA
} else {
preprocess_PCA_value <- NA
}
}
if (is.na(preprocess_PCA[1])) {
preprocess_PCA_value <- NA
}
grid_inner <- base::expand.grid(
mtry = mtry,
min_n = min_n,
trees = trees,
preprocess_PCA = preprocess_PCA_value
)
# Test models with the different hyperparameters for the inner samples
tune_results <- purrr::pmap(
list(
grid_inner$mtry,
grid_inner$min_n,
grid_inner$trees,
grid_inner$preprocess_PCA
),
fit_model_accuracy_wrapper_rf,
object = object,
mode_rf = mode_rf,
variable_name_index_pca = variable_name_index_pca,
eval_measure = eval_measure,
preprocess_step_center = preprocess_step_center,
preprocess_scale_center = preprocess_scale_center,
extremely_randomised_splitrule
)
# Sort the output to separate the accuracy, predictions and truth
tune_outputlist <- tune_results %>%
dplyr::bind_rows() %>%
split.default(names(.)) %>%
purrr::map(na.omit)
# Extract the Accuracy
# tune_eval_result <- unlist(tune_outputlist$eval_measure_val$eval_measure_val)
tune_accuracy <- (dplyr::bind_rows(tune_outputlist$eval_measure_val$eval_measure_val))$.estimate
# Add accuracy to the grid
grid_inner_accuracy <- grid_inner %>%
dplyr::mutate(eval_results = tune_accuracy)
# Progression output
best_eval <- bestParameters(
data = grid_inner_accuracy,
eval_measure = eval_measure
)
T2 <- Sys.time()
time <- T2 - T1
variable_time <- sprintf(
"(duration: %s %s).",
round(time, digits = 3),
units(time)
)
description_text <- paste(
"Fold:", eval_measure,
round(best_eval$eval_result, digits = 3),
variable_time,
"\n"
)
cat(colourise(description_text, "green"))
return(grid_inner_accuracy)
}
#' Since this will be called across the set of OUTER cross-validation splits, another wrapper is required:
#'
#' @param object An rsplit object from the INNER samples
#' object = results_nested_resampling$inner_resamples[[1]]
#' @param extremely_randomised_splitrule default: "extratrees", see also: "gini" or "hellinger"
#' @param mtry hyperparameter for random forest.
#' @param min_n hyperparameter for random forest.
#' @param trees number of trees
#' @return Accuracy
#' @noRd
summarize_tune_results_rf <- function(object,
mode_rf,
mtry,
min_n,
trees,
preprocess_step_center = preprocess_step_center,
preprocess_scale_center = preprocess_scale_center,
preprocess_PCA,
variable_name_index_pca,
eval_measure,
extremely_randomised_splitrule) {
# Return row-bound tibble containing the INNER results
results <- purrr::map_df(
.x = object$splits,
.f = tune_over_cost_rf,
mode_rf = mode_rf,
mtry = mtry,
min_n = min_n,
trees = trees,
preprocess_step_center = preprocess_step_center,
preprocess_scale_center = preprocess_scale_center,
preprocess_PCA = preprocess_PCA,
variable_name_index_pca = variable_name_index_pca,
eval_measure = eval_measure,
extremely_randomised_splitrule = extremely_randomised_splitrule
)
return(results)
}
#' Train word embeddings to a categorical variable using random forest.
#'
#' @param x Word embeddings from textEmbed.
#' @param y Categorical variable to predict.
#' @param x_append (optional) Variables to be appended after the word embeddings (x);
#' if wanting to preappend them before the word embeddings use the option
#' first = TRUE. If not wanting to train with word embeddings, set x_append = NULL (default = null).
#' @param append_first (boolean) Option to add variables before or after all word embeddings (default = FALSE).
#' @param cv_method (character) Cross-validation method to use within a pipeline of nested outer and
#' inner loops of folds (see nested_cv in rsample). Default is using cv_folds in the
#' outside folds and "validation_split" using rsample::validation_split in the inner loop to
#' achieve a development and assessment set (note that for validation_split the inside_folds
#' should be a proportion, e.g., inside_folds = 3/4); whereas "cv_folds" uses rsample::vfold_cv
#' to achieve n-folds in both the outer and inner loops.
#' @param outside_folds (numeric) Number of folds for the outer folds (default = 10).
#' @param inside_folds (numeric) Number of folds for the inner folds (default = 3/4).
#' @param strata (string or tibble; default "y") Variable to stratify according; if a string the
#' variable needs to be in the training set - if you want to stratify according to another variable
#' you can include it as a tibble (please note you can only add 1 variable to stratify according).
#' Can set it to NULL.
#' @param outside_strata (boolean) Whether to stratify the outside folds.
#' @param outside_breaks (numeric) The number of bins wanted to stratify a numeric stratification variable
#' in the outer cross-validation loop (default = 4).
#' @param inside_strata (boolean) Whether to stratify the outside folds.
#' @param inside_breaks The number of bins wanted to stratify a numeric stratification variable
#' in the inner cross-validation loop (default = 4).
#' @param mode_rf Default is "classification" ("regression" is not supported yet).
#' @param preprocess_step_center (boolean) Normalizes dimensions to have a mean of zero; default is set to FALSE
#' For more info see (step_center in recipes).
#' @param preprocess_scale_center (boolean) Normalizes dimensions to have a standard deviation of one;
#' default is set to FALSE. For more info see (step_scale in recipes).
#' @param preprocess_PCA Pre-processing threshold for PCA. Can select amount of variance to
#' retain (e.g., .90 or as a grid c(0.80, 0.90)); or
#' number of components to select (e.g., 10). (To skip this step, set preprocess_PCA to NA) Default is "min_halving",
#' which is a function that selects the number of PCA components based on number of participants and feature
#' (word embedding dimensions) in the data. The formula is:
#' preprocess_PCA = round(max(min(number_features/2), number_participants/2), min(50, number_features))).
#' @param extremely_randomised_splitrule Default is "extratrees", which thus implement a random forest;
#' can also select: NULL, "gini" or "hellinger"; if these are selected your mtry settings will
#' be overridden (see Geurts et al. (2006) Extremely randomized trees for details; and see the ranger r-package
#' for details on implementations).
#' @param mtry Hyper parameter that may be tuned; default: c(1, 20, 40),
#' @param min_n Hyper parameter that may be tuned; default: c(1, 20, 40)
#' @param trees Number of trees to use (default 1000).
#' @param eval_measure (character) Measure to evaluate the models in order to select the best
#' hyperparameters default "roc_auc"; see also "accuracy", "bal_accuracy", "sens", "spec", "precision",
#' "kappa", "f_measure".
#' @param model_description (character) Text to describe your model (optional; good when sharing the model with others).
#' @param multi_cores If TRUE it enables the use of multiple cores if the computer system allows for it (i.e.,
#' only on unix, not windows). Hence it makes the analyses considerably faster to run. Default is
#' "multi_cores_sys_default", where it automatically uses TRUE for Mac and Linux and FALSE for Windows.
#' Note that having it to TRUE does not enable reproducable results at the moment (i.e., cannot set seed).
#' @param save_output (character) Option not to save all output; default "all". See also "only_results" and
#' "only_results_predictions".
#' @param simulate.p.value (Boolean) From fisher.test: a logical indicating whether to compute p-values
#' by Monte Carlo simulation, in larger than 2 × 2 tables.
#' @param seed (numeric) Set different seed (default = 2020).
#' @param ... For example settings in yardstick::accuracy to set event_level (e.g., event_level = "second").
#' @return A list with roc_curve_data, roc_curve_plot, truth and predictions, preprocessing_recipe,
#' final_model, model_description chisq and fishers test as well as evaluation measures, e.g., including accuracy,
#' f_meas and roc_auc (for details on these measures see the yardstick r-package documentation).
#' @examples
#' # Examines how well the embeddings from column "harmonywords" in
#' # Language_based_assessment_data_8 can binarily classify gender.
#'
#' \dontrun{
#' trained_model <- textTrainRandomForest(
#' x = word_embeddings_4$texts$harmonywords,
#' y = as.factor(Language_based_assessment_data_8$gender),
#' trees = c(1000, 1500),
#' mtry = c(1), # this is short because of testing
#' min_n = c(1), # this is short because of testing
#' multi_cores = FALSE # This is FALSE due to CRAN testing and Windows machines.
#' )
#'
#'
#' # Examine results (t-value, degree of freedom (df), p-value,
#' # alternative-hypothesis, confidence interval, correlation coefficient).
#'
#' trained_model$results
#' }
#' @seealso See \code{\link{textEmbedLayerAggregation}}, \code{\link{textTrainLists}} and
#' \code{\link{textTrainRegression}}.
#' @importFrom stats cor.test na.omit chisq.test fisher.test complete.cases
#' @importFrom dplyr select bind_cols starts_with filter arrange rename
#' @importFrom tibble as_tibble
#' @importFrom recipes recipe step_naomit step_center step_scale step_pca
#' @importFrom rsample vfold_cv
#' @importFrom parsnip linear_reg set_engine rand_forest
#' @importFrom tune control_grid tune_grid select_best collect_predictions control_resamples
#' @importFrom workflows workflow add_model add_recipe
#' @importFrom magrittr %>%
#' @importFrom future plan multisession
#' @importFrom furrr future_map
#' @importFrom yardstick accuracy bal_accuracy sens spec precision kap f_meas
#' @export
textTrainRandomForest <- function(x,
y,
x_append = NULL,
append_first = FALSE,
cv_method = "validation_split",
outside_folds = 10,
inside_folds = 3 / 4,
strata = "y",
outside_strata = TRUE,
outside_breaks = 4,
inside_strata = TRUE,
inside_breaks = 4,
mode_rf = "classification",
preprocess_step_center = FALSE,
preprocess_scale_center = FALSE,
preprocess_PCA = NA,
extremely_randomised_splitrule = "extratrees",
mtry = c(1, 10, 20, 40),
min_n = c(1, 10, 20, 40),
trees = c(1000),
eval_measure = "bal_accuracy",
model_description = "Consider writing a description of your model here",
multi_cores = "multi_cores_sys_default",
save_output = "all",
simulate.p.value = FALSE,
seed = 2020,
...) {
T1_textTrainRandomForest <- Sys.time()
set.seed(seed)
# Sorting out y
if (tibble::is_tibble(y) || is.data.frame(y)) {
y_name <- colnames(y)
y <- tibble::as_tibble_col(y[[1]], column_name = "y")
} else {
y_name <- deparse(substitute(y))
y <- tibble::as_tibble_col(y, column_name = "y")
}
# The fit_model_rmse function need to number of word embeddings -- instead of
# sending a separate parameter number of embeddings are give as a comment in "model"
if (tibble::is_tibble(x)) {
comment(eval_measure) <- "1"
} else {
comment(eval_measure) <- paste(length(x))
}
# sorting out x's
variables_and_names <- sorting_xs_and_x_append(
x = x,
x_append = x_append,
append_first = append_first, ...
)
x1 <- variables_and_names$x1
x_name <- variables_and_names$x_name
embedding_description <- variables_and_names$embedding_description
x_append_names <- variables_and_names$x_append_names
variable_name_index_pca <- variables_and_names$variable_name_index_pca
rm(variables_and_names)
if (!mode_rf == "regression") {
y$y <- as.factor(y[[1]])
}
xy <- dplyr::bind_cols(x1, y)
xy <- tibble::as_tibble(xy)
xy$id_nr <- c(seq_len(nrow(xy)))
id_nr <- tibble::as_tibble_col(c(seq_len(nrow(xy))), column_name = "id_nr")
xy <- tibble::as_tibble(xy[stats::complete.cases(xy), ])
# Adding strata variable to the data set -- and then calling it "outside_strata"
# Which is then called in strata variable, and also made into not a predictor downstream
outside_strata_y <- NULL
inside_strata_y <- NULL
strata_name <- NULL
if (!is.null(strata)) {
if (tibble::is_tibble(strata)) {
strata_name <- colnames(strata)
colnames(strata) <- "strata"
xy <- dplyr::bind_cols(xy, strata)
if (inside_strata) {
outside_strata_y <- "strata"
}
if (outside_strata) {
inside_strata_y <- "strata"
}
}
if (strata[[1]][[1]] == "y") {
strata_name <- "y"
if (inside_strata) {
outside_strata_y <- "y"
}
if (outside_strata) {
inside_strata_y <- "y"
}
}
}
# Cross-Validation help(nested_cv) help(vfold_cv) help(validation_split)
if (cv_method == "cv_folds") {
results_nested_resampling <- rlang::expr(rsample::nested_cv(xy,
outside = rsample::vfold_cv(
v = !!outside_folds,
repeats = 1,
strata = !!outside_strata_y,
breaks = !!outside_breaks
), #
inside = rsample::vfold_cv(
v = !!inside_folds,
repeats = 1,
strata = !!inside_strata_y,
breaks = !!inside_breaks
)
))
}
if (cv_method == "validation_split") {
results_nested_resampling <- rlang::expr(rsample::nested_cv(xy,
outside = rsample::vfold_cv(
v = !!outside_folds,
repeats = 1,
strata = !!outside_strata_y,
breaks = !!outside_breaks
), #
inside = rsample::validation_split(
prop = !!inside_folds,
strata = !!inside_strata_y,
breaks = !!inside_breaks
)
))
}
results_nested_resampling <- rlang::eval_tidy(results_nested_resampling)
# Deciding whether to use multicorese depending on system and settings.
if (multi_cores == "multi_cores_sys_default") {
if (.Platform$OS.type == "unix") {
multi_cores_use <- TRUE
} else if (.Platform$OS.type == "windows") {
multi_cores_use <- FALSE
}
} else if (multi_cores == TRUE) {
multi_cores_use <- TRUE
} else if (multi_cores == FALSE) {
multi_cores_use <- FALSE
}
# Tuning inner resamples
if (multi_cores_use == FALSE) {
tuning_results <- purrr::map(
.x = results_nested_resampling$inner_resamples,
.f = summarize_tune_results_rf,
mode_rf = mode_rf,
mtry = mtry,
min_n = min_n,
trees = trees,
preprocess_step_center = preprocess_step_center,
preprocess_scale_center = preprocess_scale_center,
preprocess_PCA = preprocess_PCA,
variable_name_index_pca = variable_name_index_pca,
eval_measure = eval_measure,
extremely_randomised_splitrule = extremely_randomised_splitrule
)
} else if (multi_cores_use == TRUE) {
# The multisession plan uses the local cores to process the inner resampling loop.
future::plan(future::multisession)
# The object tuning_results is a list of data frames for each of the OUTER resamples.
tuning_results <- furrr::future_map(
.x = results_nested_resampling$inner_resamples,
.f = summarize_tune_results_rf,
mode_rf = mode_rf,
mtry = mtry,
min_n = min_n,
trees = trees,
preprocess_step_center = preprocess_step_center,
preprocess_scale_center = preprocess_scale_center,
preprocess_PCA = preprocess_PCA,
variable_name_index_pca = variable_name_index_pca,
eval_measure = eval_measure,
extremely_randomised_splitrule = extremely_randomised_splitrule
)
}
# Function to get the lowest eval_measure_val library(tidyverse)
# Determine the best parameter estimate from each INNER sample to be used
# for each of the outer resampling iterations:
hyper_parameter_vals <-
tuning_results %>%
purrr::map_df(bestParameters, eval_measure)
# Bind best results
results_split_parameter <-
dplyr::bind_cols(results_nested_resampling, hyper_parameter_vals)
# Compute the outer resampling results for each of the
# splits using the corresponding tuning parameter value from results_split_parameter.
results_outer <- purrr::pmap(
list(
object = results_nested_resampling$splits,
mode_rf = mode_rf,
results_split_parameter$mtry,
results_split_parameter$min_n,
trees = results_split_parameter$trees,
preprocess_PCA = results_split_parameter$preprocess_PCA,
variable_name_index_pca = list(variable_name_index_pca),
eval_measure = list(eval_measure)
),
fit_model_accuracy_rf
)
# Separate RMSE, predictions and observed y
outputlist_results_outer <- results_outer %>%
dplyr::bind_rows() %>%
split.default(names(.)) %>%
purrr::map(na.omit)
# Get predictions and evaluation results help(full_join)
if(length(levels(y$y)) == 2){
results_collected <- classification_results(
outputlist_results_outer = outputlist_results_outer,
id_nr,
simulate.p.value = simulate.p.value, ...
)
}
if(length(levels(y$y)) > 2){
results_collected <- classification_results_multi(
outputlist_results_outer = outputlist_results_outer,
id_nr,
simulate.p.value = simulate.p.value, ...
)
}
names(results_collected) <- c("predy_y", "roc_curve_data", "roc_curve_plot", "fisher", "chisq", "results_collected")
##### Construct final model to be saved and applied on other data ########
############################################################################
if ("strata" %in% colnames(xy)) {
xy_all <- xy %>%
dplyr::select(-strata)
} else {
xy_all <- xy
}
######### One word embedding as input
n_embbeddings <- as.numeric(comment(eval_measure))
if (n_embbeddings == 1) {
final_recipe <- # xy %>%
recipes::recipe(y ~ ., xy_all[0, ]) %>%
recipes::update_role(id_nr, new_role = "id variable") %>%