-
Notifications
You must be signed in to change notification settings - Fork 635
/
recipes.R
1234 lines (1059 loc) · 48.6 KB
/
recipes.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
## Overall method for recipes
#' @export
train.recipe <- function(recipe,
data,
method = "rf",
...,
metric = ifelse(is.factor(y), "Accuracy", "RMSE"),
maximize = ifelse(metric %in% c("RMSE", "logLoss"), FALSE, TRUE),
trControl = trainControl(),
tuneGrid = NULL,
tuneLength = ifelse(trControl$method == "none", 1, 3)) {
preproc_dots(...)
startTime <- proc.time()
if(trControl$verboseIter) {
cat("Preparing recipe\n")
flush.console()
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# prep and bake recipe on entire training set
trained_rec <- prepare(recipe, training = data, fresh = TRUE,
retain = TRUE,
verbose = FALSE, stringsAsFactors = TRUE)
x <- juice(trained_rec, all_predictors())
y <- juice(trained_rec, all_outcomes())
if(ncol(y) > 1)
stop("`train` doesn't support multivariate outcomes")
y <- getElement(y, names(y))
is_weight <- summary(trained_rec)$role == "case weight"
if(any(is_weight)) {
if(sum(is_weight) > 1)
stop("Ony one column can be used as a case weight.")
weights <- juice(trained_rec, has_role("case weight"))
weights <- getElement(weights, names(weights))
} else weights <- NULL
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(is.list(method)) {
minNames <- c("library", "type", "parameters", "grid",
"fit", "predict", "prob")
nameCheck <- minNames %in% names(method)
if(!all(nameCheck)) stop(paste("some required components are missing:",
paste(minNames[!nameCheck], collapse = ", ")),
call. = FALSE)
models <- method
method <- "custom"
} else {
models <- getModelInfo(method, regex = FALSE)[[1]]
if (length(models) == 0)
stop(paste("Model", method, "is not in caret's built-in library"), call. = FALSE)
}
checkInstall(models$library)
for(i in seq(along = models$library))
do.call("require", list(package = models$library[i]))
if(any(names(models) == "check") && is.function(models$check)) {
software_check <- models$check(models$library)
}
paramNames <- as.character(models$parameters$parameter)
funcCall <- match.call(expand.dots = TRUE)
modelType <- get_model_type(y)
if(!(modelType %in% models$type))
stop(paste("wrong model type for", tolower(modelType)), call. = FALSE)
## RECIPE the rec might produce character `x` so convert if these
## models are used?
if(grepl("^svm", method) & grepl("String$", method)) {
if(is.vector(x) && is.character(x)) {
stop("'x' should be a character matrix with a single column for string kernel methods",
call. = FALSE)
}
if(is.matrix(x) && is.numeric(x)) {
stop("'x' should be a character matrix with a single column for string kernel methods",
call. = FALSE)
}
if(is.data.frame(x)) {
stop("'x' should be a character matrix with a single column for string kernel methods",
call. = FALSE)
}
}
if(modelType == "Regression" & length(unique(y)) == 2)
warning(paste("You are trying to do regression and your outcome only has",
"two possible values Are you trying to do classification?",
"If so, use a 2 level factor as your outcome column."))
if(modelType != "Classification" & !is.null(trControl$sampling))
stop("sampling methods are only implemented for classification problems",
call. = FALSE)
if(!is.null(trControl$sampling)) {
trControl$sampling <- parse_sampling(trControl$sampling)
}
check_dims(x = x, y = y)
n <- if(class(y)[1] == "Surv") nrow(y) else length(y)
## Some models that use RWeka start multiple threads and this conflicts with multicore:
if(any(search() == "package:doMC") && getDoParRegistered() && "RWeka" %in% models$library)
warning("Models using Weka will not work with parallel processing with multicore/doMC")
flush.console()
if(modelType == "Classification") {
## We should get and save the class labels to ensure that predictions are coerced
## to factors that have the same levels as the original data. This is especially
## important with multiclass systems where one or more classes have low sample sizes
## relative to the others
classLevels <- levels(y)
attributes(classLevels) <- list(ordered = is.ordered(y))
xtab <- table(y)
if(any(xtab == 0)) {
xtab_msg <- paste("'", names(xtab)[xtab == 0], "'", collapse = ", ", sep = "")
stop(paste("One or more factor levels in the outcome has no data:", xtab_msg),
call. = FALSE)
}
if(trControl$classProbs && any(classLevels != make.names(classLevels))) {
stop(paste("At least one of the class levels is not a valid R variable name;",
"This will cause errors when class probabilities are generated because",
"the variables names will be converted to ",
paste(make.names(classLevels), collapse = ", "),
". Please use factor levels that can be used as valid R variable names",
" (see ?make.names for help)."), call. = FALSE)
}
if(metric %in% c("RMSE", "Rsquared"))
stop(paste("Metric", metric, "not applicable for classification models"),
call. = FALSE)
if(!trControl$classProbs && metric == "ROC")
stop(paste("Class probabilities are needed to score models using the",
"area under the ROC curve. Set `classProbs = TRUE`",
"in the trainControl() function."), call. = FALSE)
if(trControl$classProbs) {
if(!is.function(models$prob)) {
warning("Class probabilities were requested for a model that does not implement them")
trControl$classProbs <- FALSE
}
}
} else {
if(metric %in% c("Accuracy", "Kappa"))
stop(paste("Metric", metric, "not applicable for regression models"),
call. = FALSE)
classLevels <- NA
if(trControl$classProbs) {
warning("cannnot compute class probabilities for regression")
trControl$classProbs <- FALSE
}
}
if(trControl$method == "oob" & is.null(models$oob))
stop("Out of bag estimates are not implemented for this model",
call. = FALSE)
## If they don't exist, make the data partitions for the resampling iterations.
if(is.null(trControl$index)) {
if(trControl$method == "custom")
stop("'custom' resampling is appropriate when the `trControl` argument `index` is used",
call. = FALSE)
trControl$index <- switch(tolower(trControl$method),
oob = NULL,
none = list(seq(along = y)),
apparent = list(all = seq(along = y)),
alt_cv =, cv = createFolds(y, trControl$number, returnTrain = TRUE),
repeatedcv =, adaptive_cv = createMultiFolds(y, trControl$number, trControl$repeats),
loocv = createFolds(y, n, returnTrain = TRUE),
boot =, boot632 =, optimism_boot =, boot_all =,
adaptive_boot = createResample(y, trControl$number),
test = createDataPartition(y, 1, trControl$p),
adaptive_lgocv =, lgocv = createDataPartition(y, trControl$number, trControl$p),
timeslice = createTimeSlices(seq(along = y),
initialWindow = trControl$initialWindow,
horizon = trControl$horizon,
fixedWindow = trControl$fixedWindow,
skip = trControl$skip)$train,
subsemble = subsemble_index(y, V = trControl$number, J = trControl$repeats))
} else {
index_types <- unlist(lapply(trControl$index, is.integer))
if(!isTRUE(all(index_types)))
stop("`index` should be lists of integers.", call. = FALSE)
if(!is.null(trControl$indexOut)) {
index_types <- unlist(lapply(trControl$indexOut, is.integer))
if(!isTRUE(all(index_types)))
stop("`indexOut` should be lists of integers.", call. = FALSE)
}
}
if(trControl$method == "apparent") trControl$indexOut <- list(all = seq(along = y))
if(trControl$method == "subsemble") {
if(!trControl$savePredictions) trControl$savePredictions <- TRUE
trControl$indexOut <- trControl$index$holdout
trControl$index <- trControl$index$model
}
if(is.logical(trControl$savePredictions)) {
trControl$savePredictions <- if(trControl$savePredictions) "all" else "none"
} else {
if(!(trControl$savePredictions %in% c("all", "final", "none")))
stop('`savePredictions` should be either logical or "all", "final" or "none"', call. = FALSE)
}
## Create holdout indices
if(is.null(trControl$indexOut) && trControl$method != "oob"){
if(tolower(trControl$method) != "timeslice") {
y_index <- if(class(y)[1] == "Surv") 1:nrow(y) else seq(along = y)
trControl$indexOut <- lapply(trControl$index, function(training) setdiff(y_index, training))
if(trControl$method %in% c("optimism_boot", "boot_all")) {
trControl$indexExtra <- lapply(trControl$index, function(training) {
list(origIndex = y_index, bootIndex = training)
})
}
names(trControl$indexOut) <- prettySeq(trControl$indexOut)
} else {
trControl$indexOut <- createTimeSlices(seq(along = y),
initialWindow = trControl$initialWindow,
horizon = trControl$horizon,
fixedWindow = trControl$fixedWindow,
skip = trControl$skip)$test
}
}
if(trControl$method != "oob" & is.null(trControl$index))
names(trControl$index) <- prettySeq(trControl$index)
if(trControl$method != "oob" & is.null(names(trControl$index)))
names(trControl$index) <- prettySeq(trControl$index)
if(trControl$method != "oob" & is.null(names(trControl$indexOut)))
names(trControl$indexOut) <- prettySeq(trControl$indexOut)
if(is.null(tuneGrid)) {
tuneGrid <- models$grid(x = x, y = y, len = tuneLength, search = trControl$search)
if (trControl$search != "grid" && tuneLength < nrow(tuneGrid))
tuneGrid <- tuneGrid[1:tuneLength,,drop = FALSE]
}
## Check to make sure that there are tuning parameters in some cases
if(grepl("adaptive", trControl$method) & nrow(tuneGrid) == 1) {
stop(paste("For adaptive resampling, there needs to be more than one",
"tuning parameter for evaluation"), call. = FALSE)
}
dotNames <- hasDots(tuneGrid, models)
if(dotNames) colnames(tuneGrid) <- gsub("^\\.", "", colnames(tuneGrid))
## Check tuning parameter names
tuneNames <- as.character(models$parameters$parameter)
goodNames <- all.equal(sort(tuneNames), sort(names(tuneGrid)))
if(!is.logical(goodNames) || !goodNames) {
stop(paste("The tuning parameter grid should have columns",
paste(tuneNames, collapse = ", ", sep = "")), call. = FALSE)
}
if(trControl$method == "none" && nrow(tuneGrid) != 1)
stop("Only one model should be specified in tuneGrid with no resampling", call. = FALSE)
## In case prediction bounds are used, compute the limits. For now,
## store these in the control object since that gets passed everywhere
trControl$yLimits <- if(is.numeric(y)) get_range(y) else NULL
if(trControl$method != "none") {
if(is.function(models$loop) && nrow(tuneGrid) > 1){
trainInfo <- models$loop(tuneGrid)
if(!all(c("loop", "submodels") %in% names(trainInfo)))
stop("The 'loop' function should produce a list with elements 'loop' and 'submodels'", call. = FALSE)
lengths <- unlist(lapply(trainInfo$submodels, nrow))
if(all(lengths == 0)) trainInfo$submodels <- NULL
} else trainInfo <- list(loop = tuneGrid)
num_rs <- if(trControl$method != "oob") length(trControl$index) else 1L
if(trControl$method %in% c("boot632", "optimism_boot", "boot_all")) num_rs <- num_rs + 1L
## Set or check the seeds when needed
if(is.null(trControl$seeds) || all(is.na(trControl$seeds))) {
seeds <- sample.int(n = 1000000L, size = num_rs * nrow(trainInfo$loop) + 1L)
seeds <- lapply(seq(from = 1L, to = length(seeds), by = nrow(trainInfo$loop)),
function(x) { seeds[x:(x+nrow(trainInfo$loop)-1L)] })
seeds[[num_rs + 1L]] <- seeds[[num_rs + 1L]][1L]
trControl$seeds <- seeds
} else {
if(!(length(trControl$seeds) == 1 && is.na(trControl$seeds))) {
## check versus number of tasks
numSeeds <- unlist(lapply(trControl$seeds, length))
badSeed <- (length(trControl$seeds) < num_rs + 1L) ||
(any(numSeeds[-length(numSeeds)] < nrow(trainInfo$loop))) ||
(numSeeds[length(numSeeds)] < 1L)
if(badSeed) stop(paste("Bad seeds: the seed object should be a list of length",
num_rs + 1, "with",
num_rs, "integer vectors of size",
nrow(trainInfo$loop), "and the last list element having at least a",
"single integer"), call. = FALSE)
if(any(is.na(unlist(trControl$seeds)))) stop("At least one seed is missing (NA)", call. = FALSE)
}
}
if(trControl$method == "oob") {
## delay this test until later
perfNames <- metric
} else {
## run some data thru the summary function and see what we get
testSummary <- evalSummaryFunction(y,
wts = weights, ctrl = trControl,
lev = classLevels, metric = metric,
method = method)
perfNames <- names(testSummary)
}
if(!(metric %in% perfNames)){
oldMetric <- metric
metric <- perfNames[1]
warning(paste("The metric \"",
oldMetric,
"\" was not in ",
"the result set. ",
metric,
" will be used instead.",
sep = ""))
}
if(trControl$method == "oob"){
tmp <- oob_train_rec(rec = recipe, dat = data,
info = trainInfo, method = models,
ctrl = trControl, lev = classLevels, ...)
performance <- tmp
perfNames <- colnames(performance)
perfNames <- perfNames[!(perfNames %in% as.character(models$parameters$parameter))]
if(!(metric %in% perfNames)){
oldMetric <- metric
metric <- perfNames[1]
warning(paste("The metric \"",
oldMetric,
"\" was not in ",
"the result set. ",
metric,
" will be used instead.",
sep = ""))
}
} else {
if(trControl$method == "LOOCV"){
tmp <- loo_train_rec(rec = recipe, dat = data,
info = trainInfo, method = models,
ctrl = trControl, lev = classLevels, ...)
performance <- tmp$performance
} else {
if(!grepl("adapt", trControl$method)){
tmp <- train_rec(rec = recipe, dat = data,
info = trainInfo, method = models,
ctrl = trControl, lev = classLevels, ...)
performance <- tmp$performance
resampleResults <- tmp$resample
} else {
tmp <- train_adapt_rec(rec = recipe, dat = data,
info = trainInfo,
method = models,
ctrl = trControl,
lev = classLevels,
metric = metric,
maximize = maximize,
...)
performance <- tmp$performance
resampleResults <- tmp$resample
}
}
}
## Remove extra indices
trControl$indexExtra <- NULL
if(!(trControl$method %in% c("LOOCV", "oob"))) {
if(modelType == "Classification" && length(grep("^\\cell", colnames(resampleResults))) > 0) {
resampledCM <- resampleResults[, !(names(resampleResults) %in% perfNames)]
resampleResults <- resampleResults[, -grep("^\\cell", colnames(resampleResults))]
#colnames(resampledCM) <- gsub("^\\.", "", colnames(resampledCM))
} else resampledCM <- NULL
} else resampledCM <- NULL
if(trControl$verboseIter) {
cat("Aggregating results\n")
flush.console()
}
perfCols <- names(performance)
perfCols <- perfCols[!(perfCols %in% paramNames)]
if(all(is.na(performance[, metric]))) {
cat(paste("Something is wrong; all the", metric, "metric values are missing:\n"))
print(summary(performance[, perfCols[!grepl("SD$", perfCols)], drop = FALSE]))
stop("Stopping", call. = FALSE)
}
## Sort the tuning parameters from least complex to most complex
if(!is.null(models$sort)) performance <- models$sort(performance)
if(any(is.na(performance[, metric])))
warning("missing values found in aggregated results")
if(trControl$verboseIter && nrow(performance) > 1) {
cat("Selecting tuning parameters\n")
flush.console()
}
## select the optimal set
selectClass <- class(trControl$selectionFunction)[1]
## Select the "optimal" tuning parameter.
if(grepl("adapt", trControl$method)) {
perf_check <- subset(performance, .B == max(performance$.B))
} else perf_check <- performance
## Make adaptive only look at parameters with B = max(B)
if(selectClass == "function") {
bestIter <- trControl$selectionFunction(x = perf_check,
metric = metric,
maximize = maximize)
}
else {
if(trControl$selectionFunction == "oneSE") {
bestIter <- oneSE(perf_check,
metric,
length(trControl$index),
maximize)
} else {
bestIter <- do.call(trControl$selectionFunction,
list(x = perf_check,
metric = metric,
maximize = maximize))
}
}
if(is.na(bestIter) || length(bestIter) != 1) stop("final tuning parameters could not be determined", call. = FALSE)
if(grepl("adapt", trControl$method)) {
best_perf <- perf_check[bestIter,as.character(models$parameters$parameter),drop = FALSE]
performance$order <- 1:nrow(performance)
bestIter <- merge(performance, best_perf)$order
performance$order <- NULL
}
## Based on the optimality criterion, select the tuning parameter(s)
bestTune <- performance[bestIter, paramNames, drop = FALSE]
} else {
bestTune <- tuneGrid
performance <- evalSummaryFunction(y, wts = weights, ctrl = trControl,
lev = classLevels, metric = metric,
method = method)
perfNames <- names(performance)
performance <- as.data.frame(t(performance))
performance <- cbind(performance, tuneGrid)
performance <- performance[-1,,drop = FALSE]
tmp <- resampledCM <- NULL
} # end(trControl$method != "none")
## Save some or all of the resampling summary metrics
if(!(trControl$method %in% c("LOOCV", "oob", "none"))) {
byResample <- switch(trControl$returnResamp,
none = NULL,
all = {
out <- resampleResults
colnames(out) <- gsub("^\\.", "", colnames(out))
out
},
final = {
out <- merge(bestTune, resampleResults)
out <- out[,!(names(out) %in% names(tuneGrid)), drop = FALSE]
out
})
} else {
byResample <- NULL
}
# names(bestTune) <- paste(".", names(bestTune), sep = "")
## Reorder rows of performance
orderList <- list()
for(i in seq(along = paramNames)) orderList[[i]] <- performance[,paramNames[i]]
performance <- performance[do.call("order", orderList),]
if(trControl$verboseIter) {
bestText <- paste(paste(names(bestTune), "=",
format(bestTune, digits = 3)),
collapse = ", ")
if(nrow(performance) == 1) bestText <- "final model"
cat("Fitting", bestText, "on full training set\n")
flush.console()
}
## Make the final model based on the tuning results
indexFinal <- if(is.null(trControl$indexFinal))
seq(along = y) else trControl$indexFinal
if(!(length(trControl$seeds) == 1 && is.na(trControl$seeds)))
set.seed(trControl$seeds[[length(trControl$seeds)]][1])
finalTime <- system.time(
finalModel <- rec_model(recipe,
subset_x(data, indexFinal),
method = models,
tuneValue = bestTune,
obsLevels = classLevels,
last = TRUE,
classProbs = trControl$classProbs,
sampling = trControl$sampling,
...)
)
if(trControl$trim && !is.null(models$trim)) {
if(trControl$verboseIter) old_size <- object.size(finalModel$fit)
finalModel$fit <- models$trim(finalModel$fit)
if(trControl$verboseIter) {
new_size <- object.size(finalModel$fit)
reduction <- format(old_size - new_size, units = "Mb")
if(reduction == "0 Mb") reduction <- "< 0 Mb"
p_reduction <- (unclass(old_size) - unclass(new_size))/unclass(old_size)*100
p_reduction <- if(p_reduction < 1) "< 1%" else paste0(round(p_reduction, 0), "%")
cat("Final model footprint reduced by", reduction, "or", p_reduction, "\n")
}
}
finalModel <- finalModel$fit
## Remove this and check for other places it is reference
## replaced by tuneValue
if(method == "pls") finalModel$bestIter <- bestTune
## To use predict.train and automatically use the optimal lambda,
## we need to save it
if(method == "glmnet") finalModel$lambdaOpt <- bestTune$lambda
if(trControl$returnData) {
outData <- data
} else outData <- NULL
if(trControl$savePredictions == "final")
tmp$predictions <- merge(bestTune, tmp$predictions)
endTime <- proc.time()
times <- list(everything = endTime - startTime,
final = finalTime)
out <- structure(list(method = method,
modelInfo = models,
modelType = modelType,
recipe = trained_rec,
results = performance,
pred = tmp$predictions,
bestTune = bestTune,
call = funcCall,
dots = list(...),
metric = metric,
control = trControl,
finalModel = finalModel,
trainingData = outData,
resample = byResample,
resampledCM = resampledCM,
perfNames = perfNames,
maximize = maximize,
yLimits = trControl$yLimits,
times = times,
levels = classLevels),
class = c("train.recipe", "train"))
trControl$yLimits <- NULL
if(trControl$timingSamps > 0) {
pData <- x[sample(1:nrow(x), trControl$timingSamps, replace = TRUE),,drop = FALSE]
out$times$prediction <- system.time(predict(out, pData))
} else out$times$prediction <- rep(NA, 3)
out
}
#' @export
predict.train.recipe <- function(object,
newdata = stop("Please provide `newdata`"),
type = "raw",
...) {
if (type == "raw") {
predicted <- rec_pred(method = object$modelInfo,
object = list(fit = object$finalModel,
recipe = object$recipe),
newdata = newdata)
names(predicted) <- NULL
if (!is.null(object$levels) && !is.na(object$levels)) {
predicted <- if (attr(object$levels, "ordered"))
ordered(as.character(predicted), levels = object$levels)
else
factor(as.character(predicted), levels = object$levels)
}
} else {
predicted <- rec_prob(method = object$modelInfo,
object = list(fit = object$finalModel,
recipe = object$recipe),
newdata = newdata)
predicted <- predicted[, object$levels]
}
predicted
}
## drop dimensions from a `tibble`
get_vector <- function(object) {
if(!inherits(object, "tbl_df") & !is.data.frame(object))
return(object)
if(ncol(object) > 1)
stop("Only one column should be available")
getElement(object, names(object)[1])
}
## return a vector of names
role_cols <- function(object, role) {
vars <- object$term_info
vars$variable[vars$role %in% role]
}
## Check to make sure that old syntax is not used
preproc_dots <- function(...) {
dots <- list(...)
is_pp <- grepl("^preProc", names(dots))
if(any(is_pp))
warning("When using a recipe with `train`, ",
paste0("`", names(dots)[is_pp], "`", collapse = ", "),
" will be ignored.",
call. = FALSE)
invisible(NULL)
}
model_failed <- function(x) {
if(inherits(x, "try-error"))
return(TRUE)
if(any(names(x) == "fit"))
if(inherits(x$fit, "try-error"))
return(TRUE)
if(any(names(x) == "recipe"))
if(inherits(x$recipe, "try-error"))
return(TRUE)
FALSE
}
pred_failed <- function(x)
inherits(x, "try-error")
## Convert the recipe to holdout data. rename this to something like
## get_perf_data
#' @importFrom recipes bake all_predictors all_outcomes has_role
holdout_rec <- function(object, dat, index) {
##
ho_data <- bake(object$recipe,
newdata = subset_x(dat, index),
all_outcomes())
names(ho_data) <- "obs"
## ~~~~~~ move these two to other functions:
wt_cols <- role_cols(object$recipe, "case weight")
if(length(wt_cols) > 0) {
wts <- bake(object$recipe,
newdata = subset_x(dat, index),
has_role("case weight"))
ho_data$weights <- get_vector(wts)
rm(wts)
}
perf_cols <- role_cols(object$recipe, "performance var")
if(length(perf_cols) > 0) {
perf_data <- bake(object$recipe,
newdata = subset_x(dat, index),
has_role("performance var"))
ho_data <- cbind(ho_data, perf_data)
}
## ~~~~~~
ho_data$rowIndex <- (1:nrow(dat))[index]
ho_data <- as.data.frame(ho_data)
}
#' @importFrom recipes bake prepare juice has_role
rec_model <- function(rec, dat, method, tuneValue, obsLevels,
last = FALSE, sampling = NULL, classProbs, ...) {
if(!is.null(sampling) && sampling$first) {
## get original column names for downsamping then reassemble
## the training set prior to making the recipe
var_info <- summary(rec)
y_cols <- role_cols(rec, "outcome")
y <- dat[, y_cols]
if(length(y_cols) > 1)
stop("`train` doesn't support multivariate outcomes")
if(is.data.frame(y)) y <- getElement(y, names(y))
other_cols <- var_info[var_info$role %in% c("predictor", "case weight", "performance var"),]
other_cols <- other_cols$variable
other_dat <- dat[, other_cols] ## test this with data frames and tibbles
tmp <- sampling$func(other_dat, y)
orig_dat <- dat
dat <- tmp$x
dat[, y_cols] <- tmp$y
rm(tmp, y, other_cols, other_dat, orig_dat)
}
trained_rec <- prepare(rec, training = dat, fresh = TRUE,
verbose = FALSE, stringsAsFactors = TRUE,
retain = TRUE)
x <- juice(trained_rec, all_predictors())
y <- juice(trained_rec, all_outcomes())
y <- get_vector(y)
is_weight <- summary(trained_rec)$role == "case weight"
if(any(is_weight)) {
if(sum(is_weight) > 1)
stop("Ony one column can be used as a case weight.")
weights <- bake(trained_rec, newdata = dat, has_role("case weight"))
weights <- get_vector(weights)
} else weights <- NULL
if(!is.null(sampling) && !sampling$first) {
tmp <- sampling$func(x, y)
x <- tmp$x
y <- tmp$y
rm(tmp)
}
modelFit <- try(method$fit(x = x,
y = y, wts = weights,
param = tuneValue, lev = obsLevels,
last = last,
classProbs = classProbs, ...),
silent = TRUE)
## for models using S4 classes, you can't easily append data, so
## exclude these and we'll use other methods to get this information
if(is.null(method$label)) method$label <- ""
if(!isS4(modelFit) & !model_failed(modelFit)) {
modelFit$xNames <- colnames(x)
modelFit$problemType <- if(is.factor(y)) "Classification" else "Regression"
modelFit$tuneValue <- tuneValue
modelFit$obsLevels <- obsLevels
modelFit$param <- list(...)
}
list(fit = modelFit, recipe = trained_rec)
}
#' @importFrom recipes bake all_predictors
rec_pred <- function (method, object, newdata, param = NULL) {
x <- bake(object$recipe, newdata = newdata, all_predictors())
out <- method$predict(modelFit = object$fit, newdata = x,
submodels = param)
if(is.matrix(out) | is.data.frame(out))
out <- out[,1]
out
}
#' @importFrom recipes bake all_predictors
rec_prob <- function (method, object, newdata = NULL, param = NULL) {
x <- bake(object$recipe, newdata = newdata, all_predictors())
obsLevels <- levels(object$fit)
classProb <- method$prob(modelFit = object$fit, newdata = x,
submodels = param)
if (!is.data.frame(classProb) & is.null(param)) {
classProb <- as.data.frame(classProb)
if (!is.null(obsLevels))
classprob <- classProb[, obsLevels]
}
classProb
}
## analogous workflows to the originals
loo_train_rec <- function(rec, dat, info, method,
ctrl, lev, testing = FALSE, ...) {
loadNamespace("caret")
loadNamespace("recipes")
printed <- format(info$loop)
colnames(printed) <- gsub("^\\.", "", colnames(printed))
`%op%` <- getOper(ctrl$allowParallel && getDoParWorkers() > 1)
is_regression <- is.null(lev)
pkgs <- c("methods", "caret", "recipes")
if(!is.null(method$library))
pkgs <- c(pkgs, method$library)
result <- foreach(iter = seq(along = ctrl$index),
.combine = "rbind",
.verbose = FALSE,
.packages = pkgs,
.errorhandling = "stop") %:%
foreach(parm = 1:nrow(info$loop),
.combine = "rbind",
.verbose = FALSE,
.packages = pkgs,
.errorhandling = "stop") %op% {
if(!(length(ctrl$seeds) == 1 && is.na(ctrl$seeds)))
set.seed(ctrl$seeds[[iter]][parm])
if(testing) cat("after loops\n")
loadNamespace("caret")
if(ctrl$verboseIter)
progress(printed[parm,,drop = FALSE],
names(ctrl$index), iter, TRUE)
if(is.null(info$submodels[[parm]])
|| nrow(info$submodels[[parm]]) > 0) {
submod <- info$submodels[[parm]]
} else submod <- NULL
mod_rec <-
try(
rec_model(rec, dat[ ctrl$index[[iter]], ],
method = method,
tuneValue = info$loop[parm,,drop = FALSE],
obsLevels = lev,
classProbs = ctrl$classProbs,
sampling = ctrl$sampling,
...),
silent = TRUE)
holdoutIndex <- ctrl$indexOut[[iter]]
if(!model_failed(mod_rec)) {
predicted <- try(
rec_pred(method = method,
object = mod_rec,
newdata = subset_x(dat, holdoutIndex),
param = submod),
silent = TRUE)
if(pred_failed(predicted)) {
fail_warning(settings = printed[parm,,drop = FALSE],
msg = predicted,
where = "predictions",
iter = names(ctrl$index)[iter],
verb = ctrl$verboseIter)
predicted <- fill_failed_pred(index = holdoutIndex, lev = lev, submod)
}
} else {
fail_warning(settings = printed[parm,,drop = FALSE],
msg = mod_rec,
iter = names(ctrl$index)[iter],
verb = ctrl$verboseIter)
predicted <- fill_failed_pred(index = holdoutIndex, lev = lev, submod)
}
if(testing) print(head(predicted))
if(ctrl$classProbs) {
if(!model_failed(mod_rec)) {
probValues <- rec_prob(method = method,
object = mod_rec,
newdata = subset_x(dat, holdoutIndex),
param = submod)
} else {
probValues <- fill_failed_prob(holdoutIndex, lev, submod)
}
if(testing) print(head(probValues))
}
predicted <- trim_values(predicted, ctrl, is_regression)
##################################
## We'll attach data points/columns to the object used
## to assess holdout performance
ho_data <- holdout_rec(mod_rec, dat, holdoutIndex)
if(!is.null(info$submodels)) {
## collate the predictions across all the sub-models
predicted <- lapply(predicted,
function(x, lv, dat) {
x <- outcome_conversion(x, lv = lev)
dat$pred <- x
dat
},
lv = lev,
dat = ho_data)
if(testing) print(head(predicted))
## same for the class probabilities
if(ctrl$classProbs) {
for(k in seq(along = predicted)) predicted[[k]] <-
cbind(predicted[[k]], probValues[[k]])
}
predicted <- do.call("rbind", predicted)
allParam <- expandParameters(info$loop[parm,,drop = FALSE], submod)
rownames(predicted) <- NULL
predicted <- cbind(predicted, allParam)
## if saveDetails then save and export 'predicted'
} else {
pred_val <- outcome_conversion(predicted, lv = lev)
predicted <- ho_data
predicted$pred <- pred_val
if(ctrl$classProbs) predicted <- cbind(predicted, probValues)
predicted <- cbind(predicted, info$loop[parm,,drop = FALSE])
}
if(ctrl$verboseIter)
progress(printed[parm,,drop = FALSE],
names(ctrl$index), iter, FALSE)
predicted
}
names(result) <- gsub("^\\.", "", names(result))
out <- ddply(result,
as.character(method$parameter$parameter),
ctrl$summaryFunction,
lev = lev,
model = method)
list(performance = out, predictions = result)
}
oob_train_rec <- function(rec, dat, info, method,
ctrl, lev, testing = FALSE, ...) {
loadNamespace("caret")
loadNamespace("recipes")
printed <- format(info$loop)
colnames(printed) <- gsub("^\\.", "", colnames(printed))
`%op%` <- getOper(ctrl$allowParallel && getDoParWorkers() > 1)
pkgs <- c("methods", "caret", "recipes")
if(!is.null(method$library)) pkgs <- c(pkgs, method$library)
result <- foreach(
parm = 1:nrow(info$loop),
.packages = pkgs,
.combine = "rbind") %op% {
loadNamespace("caret")
if(ctrl$verboseIter)
progress(printed[parm,,drop = FALSE], "", 1, TRUE)
if(!(length(ctrl$seeds) == 1 && is.na(ctrl$seeds)))
set.seed(ctrl$seeds[[1L]][parm])
mod <- rec_model(rec, dat,
method = method,
tuneValue = info$loop[parm,,drop = FALSE],
obsLevels = lev,
classProbs = ctrl$classProbs,
sampling = ctrl$sampling,
...)
out <- method$oob(mod$fit)
if(ctrl$verboseIter)
progress(printed[parm,,drop = FALSE], "", 1, FALSE)
cbind(as.data.frame(t(out)), info$loop[parm,,drop = FALSE])
}
names(result) <- gsub("^\\.", "", names(result))
result
}
train_rec <- function(rec, dat, info, method, ctrl, lev, testing = FALSE, ...) {
loadNamespace("caret")
loadNamespace("recipes")
printed <- format(info$loop, digits = 4)
colnames(printed) <- gsub("^\\.", "", colnames(printed))
## For 632 estimator, add an element to the index of zeros to trick it into
## fitting and predicting the full data set.
resampleIndex <- ctrl$index
if(ctrl$method %in% c("boot632", "optimism_boot", "boot_all")) {
resampleIndex <- c(list("AllData" = rep(0, nrow(dat))), resampleIndex)
ctrl$indexOut <- c(list("AllData" = rep(0, nrow(dat))), ctrl$indexOut)
if(!is.null(ctrl$indexExtra))
ctrl$indexExtra <- c(list("AllData" = NULL), ctrl$indexExtra)
}
`%op%` <- getOper(ctrl$allowParallel && getDoParWorkers() > 1)
keep_pred <- isTRUE(ctrl$savePredictions) || ctrl$savePredictions %in% c("all", "final")
pkgs <- c("methods", "caret", "recipes")
if(!is.null(method$library)) pkgs <- c(pkgs, method$library)
is_regression <- is.null(lev)
export <- c("optimism_boot")
result <- foreach(iter = seq(along = resampleIndex), .combine = "c", .packages = pkgs, .export = export) %:%
foreach(parm = 1L:nrow(info$loop), .combine = "c", .packages = pkgs, .export = export) %op% {
if(!(length(ctrl$seeds) == 1L && is.na(ctrl$seeds)))
set.seed(ctrl$seeds[[iter]][parm])
loadNamespace("caret")
loadNamespace("recipes")
if(ctrl$verboseIter)
progress(printed[parm,,drop = FALSE],