-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ensemble_Methods.R
2036 lines (1724 loc) · 101 KB
/
Ensemble_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
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
################### Ensemble methods functions ###################
# 1) training function on df_train
# 2) predicting event probability for fixed_time from a method, either for df_train or for df_test
# 3) validating performance from the method on df_test
#' Methods:
#' Cox
#' Cox MFP
#' SRF, cross validate to tune
#' Ensemble 1a = Cox into SRF, cross-validate SRF to tune
#' Ensemble 1b = SRF into Cox, cross-validate SRF to tune
#' Ensemble 2a = VIMP from SRF, then single RPART TREE, then Cox in each leaf
#' Ensemble 2b = same with SRF tree
#' Ensemble 3 = VIMP from SRF, then single RPART TREE, then modified Cox with leaves identificators
################## libraries ##################
library(psych)
library(glmnet)
library(survival)
library(survminer)
library(ggplot2)
library(randomForest)
library(timeROC) # for time-dep AUC
library(zoo) # for timeROC
library(MALDIquant) # for match.closest
library(randomForestSRC)
library(data.tree)
library(rpart)
library(treeClust)
library(rpart.plot) # to plot rpart trees
library(classifierplots)
library(mfp) #for fractional polynomials
#library(pec) # for predictSurvProb replaced
library(dplyr)
library("doParallel")
library("doFuture") #for parallel calculations
num.cores <- detectCores()-1
cluztrr <- makeCluster(num.cores)
registerDoParallel(cl = cluztrr)
registerDoFuture()
plan(cluster, workers = cluztrr)
################# validation statistics #####################
bs_surv = function(y_predicted_newdata,
#these are probability of events! not survival prob!
df_brier_train,
df_newdata,
time_points,
weighted = TRUE){
#'This function calculates time-dependent BrierScore for df_newdata,
#' overall same as
#' https://scikit-survival.readthedocs.io/en/stable/api/generated/sksurv.metrics.brier_score.html#sksurv.metrics.brier_score
#' https://github.com/sebp/scikit-survival/blob/v0.19.0.post1/sksurv/metrics.py#L524-L644
#' it uses IPCW (inverse probability of censoring weights), computed with K-M curve
#' where events are censored events from train data. i.e. df_brier_train
#' returns vector of BS for each time in time_points
# K-M prob of censoring for each observation till its individual time
df_newdata$p_km_obs = survival_prob_km(df_brier_train, df_newdata$time, estimate_censoring = TRUE)
df_newdata$p_km_obs = pmax(pmin(df_newdata$p_km_obs, 0.9999),0.0001)
# !! impute with mean observations if can't estimate !!
#!!!
df_newdata[is.na(df_newdata$p_km_obs), "p_km_obs"] = mean(df_newdata$p_km_obs, na.rm=1)
#one number P(t_i is censored) = P(T > t_i) = S(t_i):
p_km_t = survival_prob_km(df_brier_train, time_points, estimate_censoring = TRUE)
p_km_t= pmax(pmin(p_km_t, 0.9999), 0.0001); p_km_t
bs = c()
for (j in 1:length(time_points)) {
# assign t_point and predicted probabilities
if (length(time_points) ==1) { #only 1 time
t_point = time_points
ppp = pmax(pmin(y_predicted_newdata, 0.99999), 0.00001)
} else { #many times
t_point = time_points[j]
ppp = pmax(pmin(y_predicted_newdata[,j], 0.99999), 0.00001)
}
#cases and controls by time t_point
id_case = ((df_newdata$time <= t_point) & (df_newdata$event==1))
id_control= (df_newdata$time > t_point)
# compute BS with weights which are 1/G(t) for controls and 1/G(obs_i) for cases
# if weights == false, use w=1 for all
if (weighted==TRUE){
#brier score is average of weighted squared errors
bs[j] = (sum(as.numeric(id_case)*(1-ppp)^2*1/df_newdata$p_km_obs, na.rm=1)+
sum(id_control*(0-ppp)^2*1/p_km_t[j], na.rm=1))/dim(df_newdata)[1]
}else{
#unweighted BS
bs[j] = sum(id_case*(1-ppp)^2 + id_control*(0-ppp)^2, na.rm=1)/dim(df_newdata)[1]
}
}
names(bs) = round(time_points,6)
return(bs)
}
survival_prob_km = function(df_km_train, times, estimate_censoring = FALSE){
#This function calculates survival probability by K-M estimate returns vector (times)
# if estimate_censoring is TRUE, censoring distr is estimated
# we also extrapolate it in survival function space using poly(n=3)
if (estimate_censoring == FALSE) {
km = survfit(Surv(time, event)~1, data = df_km_train)
kmf <- approxfun(km$time, km$surv, method = "constant")
kmdf = data.frame(cbind("time"=km$time, "surv" = km$surv))
extrap = lm(surv ~ poly(time,3, raw = TRUE), data = kmdf)
km_extrap = function(x){(cbind(1, x, x**2, x**3) %*% extrap$coefficients[1:4])}
}else{
df_km_train$censor_as_event = 1 - df_km_train$event
km = survfit(Surv(time, censor_as_event)~1, data = df_km_train)
kmdf = data.frame(cbind("time"=km$time, "surv" = km$surv))
kmf <- approxfun(km$time, km$surv, method = "constant")
extrap = lm(surv ~ poly(time,3, raw = TRUE), data = kmdf)
km_extrap = function(x){(cbind(1, x, x**2, x**3) %*% extrap$coefficients[1:4])}
}
return(km_extrap(times))
}
method_any_cv = function(df, predict.factors, train_function, predict_function, valuation_times,
cv_number = 2, seed_for_cv = 2024, parallel=FALSE,
model_args = list(), predict_args = list()){ #all arguments apart from data
pb <- txtProgressBar(0, cv_number+2, style=3)
time_0 = Sys.time()
set.seed(seed_for_cv)
#define the method to evaluate
#mmm = call(train_function, model_args)
predict.factors = eligible_params(predict.factors, df)
if (length(predict.factors)==0){print ("No eligible predictors."); return (NULL)}
cv_folds = caret::createFolds(df$event, k=cv_number, list = FALSE) #use caret to split into k-folds = cv_steps
modelstats_train = list(); modelstats_test = list();modelstats=list()
model_list = list()
setTxtProgressBar(pb, 1)
if (!parallel){
for (cv_iteration in 1:cv_number){
df_train_cv = df[cv_folds != cv_iteration, ]; dim(df_train_cv)
df_test_cv = df[cv_folds == cv_iteration, ]; dim(df_test_cv)
model_i = do.call(train_function, append(list(df_train_cv, predict.factors), model_args))
model_list[[cv_iteration]] = model_i
y_predict_test = do.call(predict_function, append(list(model_i, df_test_cv, valuation_times), predict_args))
y_predict_train = do.call(predict_function, append(list(model_i, df_train_cv, valuation_times), predict_args))
modelstats[[cv_iteration]] =rbind(
"test" = method_any_validate(y_predict_test,valuation_times, df_train_cv, df_test_cv, weighted = 1),
"train"=method_any_validate(y_predict_train, valuation_times, df_train_cv, df_train_cv, weighted = 1))
setTxtProgressBar(pb, cv_iteration+1)
}
}else{
parallel_stats = foreach::foreach(cv_iteration= 1:cv_number, .packages = c("survival", "timeROC"), .errorhandling = "pass"
)%dopar%{
df_train_cv = df[cv_folds != cv_iteration, ]
df_test_cv = df[cv_folds == cv_iteration, ]
model_i = do.call(train_function, append(list(df_train_cv, predict.factors), model_args))
y_predict_test = do.call(predict_function, append(list(model_i, df_test_cv, valuation_times), predict_args))
y_predict_train = do.call(predict_function, append(list(model_i, df_train_cv, valuation_times), predict_args))
setTxtProgressBar(pb, cv_iteration+1)
list("model" = model_i,
"stat" = rbind("test" = method_any_validate(y_predict_test,valuation_times, df_train_cv, df_test_cv, weighted = 1),
"train" = method_any_validate(y_predict_train, valuation_times, df_train_cv, df_train_cv, weighted = 1)))
}
model_list = foreach::foreach(cv_iteration = 1:cv_number)%do%{parallel_stats[[cv_iteration]]$model}
modelstats = foreach::foreach(cv_iteration = 1:cv_number)%do%{parallel_stats[[cv_iteration]]$stat}
}
#print(modelstats)
#create data frame with results:
df_modelstats_test = t(matrix(unlist(lapply(X= modelstats, FUN = function(x) x[1,])),
nrow = 7, ncol = length(modelstats)))
df_modelstats_train = t(matrix(unlist(lapply(X= modelstats, FUN = function(x) x[length(valuation_times)+1,])),
nrow = 7, ncol = length(modelstats)))
if(length(valuation_times)>1){
for (t in 2:length(valuation_times)){
df_modelstats_test = rbind(df_modelstats_test, t(matrix(unlist(lapply(X= modelstats,
FUN = function(x) x[t,])),nrow = 7, ncol = length(modelstats))))
df_modelstats_train =
rbind(df_modelstats_train, t(matrix(unlist(lapply(X= modelstats,
FUN = function(x) x[length(valuation_times)+t,])),nrow = 7, ncol = length(modelstats))))
}
}
df_modelstats_test = as.data.frame(df_modelstats_test); names(df_modelstats_test)= names(modelstats[[1]])
df_modelstats_train = as.data.frame(df_modelstats_train); names(df_modelstats_train)= names(modelstats[[1]])
df_modelstats_test$test = 1; df_modelstats_train$test = 0
df_modelstats_test$cv_n = c(1:cv_number); df_modelstats_train$cv_n = c(1:cv_number)
setTxtProgressBar(pb, cv_number+2)
close(pb)
#comprise output object
output = list()
output$test = df_modelstats_test
output$train = df_modelstats_train
output$testaverage = sapply(df_modelstats_test[,1:8],mean,na.rm=1)
output$trainaverage = sapply(df_modelstats_train[,1:8],mean,na.rm=1)
output$model_list = model_list
time_1 = Sys.time()
output$time = time_1 - time_0
return(output)
}
method_any_validate = function(y_predict, times_to_predict, df_train,
df_test, weighted = TRUE, alpha = "logit"){
#This function computes auc, brier score, c-index,
# calibration slope and alpha for df_test
#for apparent statistics use test = train
auc_score = c()
brier_score = c()
brier_score_scaled = c()
c_score = c()
calibration_slope = c()
calibration_alpha = c()
for (i in 1:length(times_to_predict)){
t_i = times_to_predict[i]
if (length(times_to_predict)>1) { y_hat = y_predict[,i] } else {y_hat = unlist(y_predict)}
#' time dependent AUC
if(class(try(timeROC::timeROC(T = df_test$time,delta=df_test$event,
marker= y_hat, times = t_i*0.9999999,cause=1,
weighting = "marginal"), silent = TRUE))=="try-error"){
auc_score[i] = NaN}else{
auc_score[i] = timeROC::timeROC(T = df_test$time,delta=df_test$event,
marker= y_hat, times = t_i*0.9999999,cause=1,
weighting = "marginal")$AUC[2]}
#' compute time-dependent Brier score:
if (class(try(bs_surv(y_hat, df_train, df_test, t_i, weighted = weighted), silent=TRUE))=="try-error"){
brier_score[i] = NaN}else{
brier_score[i]= bs_surv(y_hat, df_train, df_test, t_i, weighted = weighted)
brier_score_base_i = bs_surv(rep(mean((df_test$event)*(df_test$time<=t_i)),dim(df_test)[1]),
df_train, df_test, t_i, weighted = weighted)
brier_score_scaled[i]= 1 - brier_score[i] / brier_score_base_i
}
#' compute concordance - time-dependent in a sense that a predictor is event probability at t_i:
#' for Cox model it is the same for each time, as event prob
#' will be in the same order as LPs at any time point
if (class(try(concordancefit(Surv(df_test$time, df_test$event), -1*y_hat), silent=TRUE))=="try-error"){
c_score[i]= NaN
}else{
if (is.null(concordancefit(Surv(df_test$time, df_test$event), -1*y_hat)$concordance)) {c_score[i]= NaN
}else{c_score[i]=concordancefit(Surv(df_test$time, df_test$event), -1*y_hat)$concordance}
}
# can add later confusion matrix, but also need to find Youden point instead of 0.5
# confmatrix = timeROC::SeSpPPVNPV(0.5, T = df_test$time,delta=df_test$event,
# marker= y_hat, times = t_i*0.999,cause=1, weighting = "marginal")
# c(as.double(confmatrix$TP[2]), as.double(confmatrix$FP[2]),
# as.double(confmatrix$PPV[2]), as.double(confmatrix$NPV[2]))
# compute calibration slope and alpha:
# 1/0 by t_i:
df_test$event_ti = ifelse(df_test$time <= t_i & df_test$event ==1, 1, 0)
# cut 0 and 1 predicted probabilities for the logit to work:
df_test$predict_ti = pmax(pmin(y_hat, 0.99999), 0.00001)
#Take out censored observations before t_i,ie leaving those which state we know:
df_test_in_scope = df_test[(df_test$time >= t_i) | (df_test$time <t_i & df_test$event ==1), ]
#Calibration slope and alpha.
y_hat_hat = log(df_test_in_scope$predict_ti / (1-df_test_in_scope$predict_ti))
y_actual_i = df_test_in_scope$event_ti
if(class(
try( glm(y_actual_i ~ y_hat_hat,family = binomial(link = "logit")), silent = TRUE)
)[1] =="try-error" ){
calibration_slope[i] = NaN
calibration_alpha[i] = NaN
}else{
calibration_slope[i] = glm(y_actual_i ~ y_hat_hat,
family = binomial(link = "logit"))$coefficients[2]
if (alpha == "logit"){ #take alpha from alpha: logit(y)~ logit(y_hat) + alpha
calibration_alpha[i] = glm(y_actual_i ~ offset(y_hat_hat),
family = binomial(link = "logit"))$coefficients[1]
}else{ #take alpha as alpha= mean(y) - mean(y_hat)
calibration_alpha[i] = mean(y_actual_i) - mean(df_test_in_scope$predict_ti)
}
}#end "else"
} #end "for"
output = data.frame("T" = times_to_predict,
"AUCROC" = auc_score,
"BS" = brier_score,
"BS_scaled" = brier_score_scaled,
"C_score" = c_score,
"Calib_slope" = calibration_slope,
"Calib_alpha" = calibration_alpha
)
return (output)
}
eligible_params = function(params, df){
#'This function checks eligible predictors from params list for split
#' It deletes those which are
#'1) not in df and
#'2) taking only 1 value (constants)
#'! Later may delete collinear factors
#'
if (length(params)==0) return (NULL)
# take only columns which are in df
z = params %in% names(df)
if (sum(!z)==length(params)){
return (NULL) #no eligible params
}else{
params = params[z]# there are some potentially eligible
}
params_eligible = params
for (i in 1:length(params)){
if (length(unique(df[,params[i]])) < 2){
params_eligible = params_eligible[params_eligible!=params[i]]}
}
return (params_eligible)
}
populationstats = function(df_stats, time_f, namedf = "df"){
#df_stats = results_object[[3]]
statsdf = data.frame()
df_stats$event_fixed_time = ifelse(((df_stats$event ==1)&(df_stats$time < time_f)), 1, 0)
df_stats$time_fixed_time = ifelse(df_stats$time < time_f, df_stats$time, time_f )
statsdf["data_set_name","value"] = namedf
statsdf["sample_size_N","value"] = nrow(df_stats)
statsdf["time_fixed","value"] = time_f # results_object[[8]][1]
statsdf["time_max","value"] = round(max(df_stats$time),2)
statsdf["time_mean","value"] = round(mean(df_stats$time),2)
statsdf["time_mean_event","value"] = round(sum(df_stats[df_stats$event ==1, "time"])/sum(df_stats$event ==1),2)
statsdf["time_mean_censored","value"] = round(sum(df_stats[df_stats$event ==0, "time"])/sum(df_stats$event ==0),2)
statsdf["time_mean_T","value"] = round(mean(df_stats$time_fixed_time),2)
statsdf["time_mean_event_T","value"] = round(sum(df_stats[df_stats$event_fixed_time ==1, "time"])/sum(df_stats$event_fixed_time ==1),2)
statsdf["time_mean_censored_T","value"] = round(sum(df_stats[df_stats$event_fixed_time ==0, "time"])/sum(df_stats$event_fixed_time ==0),2)
statsdf["event_%","value"] = round(sum(df_stats$event==1) / nrow(df_stats) *100,2)
statsdf["event_before_T_%","value"] = round(sum(df_stats$event_fixed_time==1) / nrow(df_stats)*100,2)
statsdf["censored_before_T_%","value"] =
round(sum(df_stats$event_fixed_time==0 & df_stats$time_fixed_time < statsdf["time_fixed","value"]) / nrow(df_stats)*100,2)
return (statsdf)
}
############# Basic Cox Model functions (in the same format as other methods) ###############
method_cox_train = function(df_train, predict.factors, useCoxLasso = FALSE, fixed_time = NaN){
# wrapper for coxph() function returning a trained Cox model
if (useCoxLasso==FALSE){
cox.m = NULL
try({
cox.m = coxph(as.formula(
paste("Surv(df_train$time, df_train$event) ~",
paste(predict.factors, collapse="+"))),
data =df_train, x = TRUE)
#!!! We replace NA coefficients with 0 i.e. ignore predictors which Cox couldn't estimate
# such that predictions don't brake
cox.m$coefficients[is.na(cox.m$coefficients)] = 0},
silent = TRUE)
if(is.null(cox.m)){
print(paste("Warning: cox.m == NULL, N/Events=", dim(df_train)[1], sum(df_train$event==1)))
write.csv(df_train, "failing_cox_df_train.csv")}
return (cox.m)
}else{
return(method_coxlasso_train(df_train, predict.factors))
}
}
method_coxlasso_train <- function(df_train, predict.factors, fixed_time = NaN){
#' This function trains cox-lasso and re-trains
#' cox on non-zero predictors
#'
library(glmnet)
cox.m = NULL
try({
cv10 =glmnet::cv.glmnet(as.matrix(df_train[predict.factors]),
Surv(df_train$time, df_train$event),
family="cox", nfold=5, alpha = 1)
new.predictors = rownames(coef(cv10, s="lambda.min"))[as.matrix(coef(cv10, s="lambda.min"))!=0]
if (length(new.predictors)==0) {
print ("0 predictors in lasso!")
cox.m = survival::coxph(Surv(df_train$time, df_train$event) ~ 1,data = df_train, x = TRUE)
}else{
f = as.formula(paste("Surv(df_train$time, df_train$event) ~",paste(new.predictors, collapse="+")))
cox.m = survival::coxph(f,data = df_train, x = TRUE)
#!!!!! We replace NA coefficients with 0
# i.e. ignore predictors which the Cox model couldn't estimate)
cox.m$coefficients[is.na(cox.m$coefficients)] = 0
}
}, silent = TRUE)
return (cox.m)
}
method_cox_predict = function(model_cox, newdata, times){
#' returns event probability from trained cox model model_cox
#' for newdata at given times
#' could use pec package, but it makes data table out of newdata
#' predicted_event_prob = 1-pec::predictSurvProb(model_cox, newdata, times)
#define bh - baseline hazard as dataframe with "time" and "hazard"
# if baseline hazard can't be calibrated, # return mean(y) for all times
# we take baseline hazard from K-M estimate and lp from Cox !!!! :((
if(class(try(basehaz(model_cox, centered = 0), silent= TRUE)) =="try-error"){
bh = summary(survfit(model_cox$y~1),times)$cumhaz
predicted_event_prob = matrix(nrow = dim(newdata)[1],ncol = length(times))
for (i in seq(length(times))){
predicted_event_prob[,i] = 1 -
exp(-bh[i]*exp(predict(model_cox, newdata = newdata, type = "lp", reference = "zero"))) }
colnames(predicted_event_prob) = round(times,6)
return(predicted_event_prob)
} else {bh = basehaz(model_cox, centered = 0)}
#define bh as function to compute bh for any time
bh_approx = approxfun(bh[,"time"], bh[,"hazard"], method = "constant")
#define bh_extrap how to extrapolate outside of the times in the training data
if (class(try( lm(hazard ~ poly(time,3, raw = TRUE),
data = bh), silent = TRUE))!="try-error"){
extrap = lm(hazard ~ poly(time,3, raw = TRUE), data = bh)
bh_extrap = function(x){sum(c(1, x, x**2, x**3) * extrap$coefficients[1:4])}
} else {
min_bh = min(bh[,"hazard"], na.rm=1)
max_bh = max(bh[,"hazard"], na.rm=1)
l =dim(bh)[1]
bh[1, c("hazard", "time")] = c(0.0000001, min_bh);
bh[l+1, c("hazard", "time")] = c(bh[l,"time"]+100000, max_bh)
bh_extrap = approxfun(bh[,"time"], bh[,"hazard"], method = "constant")
}
#compute event probability for times:
#create placeholder
predicted_event_prob = matrix(nrow = dim(newdata)[1], ncol = length(times))
#go over each time in times
for (i in seq(length(times))){
if (is.na(bh_approx(times[i]))){ #if interpolation doesn't work, take extrapolated value
bh_time = bh_extrap(times[i]); if(is.na(bh_time)){bh_time = mean(bh[, "hazard"], na.rm=TRUE)}
}else{
bh_time = bh_approx(times[i])
}
#if baseline hazard is infinite, event probability is 1
if (bh_time == Inf){
predicted_event_prob[,i] = 1
#if baseline hazard is ==0, event prob is 0 for all with survival==1
# (somehow "survival" calculates even with bh==0)
}else if (bh_time == 0){
predicted_event_prob[,i] = 0
#if baseline hazard is a number, use the survival formula
}else{
predicted_event_prob[,i] = 1 -exp(-bh_time*exp(predict(model_cox,
newdata = newdata,type = "lp", reference = "zero")))
}
}
# name columns by the time for which it predicts event prob
colnames(predicted_event_prob) = round(times,6)
return (predicted_event_prob)
}
#system.time({method_cox_cv(d_xt, params, fixed_time = seq(1,10,length.out=50), parallel=FALSE)})
## user system elapsed
## 24.76 0.39 25.58
#system.time({method_cox_cv(d_xt, params, fixed_time = seq(1,10,length.out=50), parallel=TRUE)})
## user system elapsed
## 0.47 0.12 7.05
method_cox_cv = function(df, predict.factors, fixed_time = NaN,
cv_number = 5, seed_to_fix = 2024, useCoxLasso= FALSE, parallel=FALSE){
predict.factors = eligible_params(predict.factors, df)
# defining output for fixed_time
if (sum(is.nan(fixed_time))>0){fixed_time = round(quantile(df_train[df_train$event==1, "time"], 0.8),1)}
if (length(predict.factors)==0){print ("No eligible predictors."); return (NULL)}
time_0 = Sys.time()
set.seed(seed_to_fix)
cv_folds = caret::createFolds(df$event, k=cv_number, list = FALSE) #use caret to split into k-folds = cv_steps
modelstats_train = list(); modelstats_test = list();modelstats=list()
if (!parallel){
for (cv_iteration in 1:cv_number){
df_train_cv = df[cv_folds != cv_iteration, ]; dim(df_train_cv)
df_test_cv = df[cv_folds == cv_iteration, ]; dim(df_test_cv)
cox.model = method_cox_train(df_train_cv,
eligible_params(predict.factors, df_train_cv),
useCoxLasso= useCoxLasso)
y_predict_test = method_cox_predict(cox.model, df_test_cv, fixed_time)
y_predict_train = method_cox_predict(cox.model, df_train_cv, fixed_time)
modelstats[[cv_iteration]] =rbind(
"test" = method_any_validate(y_predict_test,fixed_time, df_train_cv, df_test_cv, weighted = 1),
"train"=method_any_validate(y_predict_train, fixed_time, df_train_cv, df_train_cv, weighted = 1))
}
}else{
modelstats = foreach::foreach(cv_iteration= 1:cv_number, .packages = c("survival", "timeROC"), .errorhandling = "pass"
)%dopar%{
df_train_cv = df[cv_folds != cv_iteration, ]
df_test_cv = df[cv_folds == cv_iteration, ]
cox.model = method_cox_train(df_train_cv, eligible_params(predict.factors, df_train_cv),
useCoxLasso= useCoxLasso)
y_predict_test = method_cox_predict(cox.model, df_test_cv, fixed_time)
y_predict_train = method_cox_predict(cox.model, df_train_cv, fixed_time)
rbind("test" = method_any_validate(y_predict_test,fixed_time, df_train_cv, df_test_cv, weighted = 1),
"train" = method_any_validate(y_predict_train, fixed_time, df_train_cv, df_train_cv, weighted = 1))
}
}
#create data frame with results:
df_modelstats_test = t(matrix(unlist(lapply(X= modelstats, FUN = function(x) x[1,])),
nrow = 7, ncol = length(modelstats)))
df_modelstats_train = t(matrix(unlist(lapply(X= modelstats, FUN = function(x) x[length(fixed_time)+1,])),
nrow = 7, ncol = length(modelstats)))
if(length(fixed_time)>1){
for (t in 2:length(fixed_time)){
df_modelstats_test = rbind(df_modelstats_test, t(matrix(unlist(lapply(X= modelstats,
FUN = function(x) x[t,])),nrow = 7, ncol = length(modelstats))))
df_modelstats_train =
rbind(df_modelstats_train, t(matrix(unlist(lapply(X= modelstats,
FUN = function(x) x[length(fixed_time)+t,])),nrow = 7, ncol = length(modelstats))))
}
}
df_modelstats_test = as.data.frame(df_modelstats_test); names(df_modelstats_test)= names(modelstats[[1]])
df_modelstats_train = as.data.frame(df_modelstats_train); names(df_modelstats_train)= names(modelstats[[1]])
df_modelstats_test$test = 1; df_modelstats_train$test = 0
df_modelstats_test$cv_n = c(1:cv_number); df_modelstats_train$cv_n = c(1:cv_number)
#comprise output object
output = list()
output$test = df_modelstats_test
output$train = df_modelstats_train
output$testaverage = sapply(df_modelstats_test[,1:8],mean,na.rm=1)
output$trainaverage = sapply(df_modelstats_train[,1:8],mean,na.rm=1)
time_1 = Sys.time()
output$time = time_1 - time_0
return(output)
}
############# Augmented CoxPH with Fractional Polynomials ###############
method_coxmfp_train = function(df_train, predict.factors, verbose = FALSE){
#Cox with fractional polynomials, returns final Cox model with the selected fp() risk factors
if (length(predict.factors)>20) {
print ("Too many factors (>20) to perform MFP, default to baseline Cox")
cox.mfp = coxph(as.formula(paste("Surv(df_train$time, df_train$event) ~",
paste(predict.factors, collapse="+"))),
data =df_train, x = TRUE)
}else{
# create predict.factors.mfp = paste("fp(", predict.factors, ")", sep="") for all to be fp'd
predict.factors.mfp = predict.factors
for (i in 1:length(predict.factors)) {
# only "fp" continuous factors, or those with more than 5 values i=1
if (length(table(select(df_train, predict.factors[i]))) >= 5){
predict.factors.mfp[i]= paste("fp(", predict.factors[i], ")", sep="")}
}
#calculate mfp
mfp.compute <- mfp(as.formula(paste("Surv(time, event) ~",paste(predict.factors.mfp, collapse="+"))),
data = df_train, family = "cox", verbose = FALSE, maxits = 15,select = 0.1)
if (verbose){ print ("Multiple polynomial formula for Cox model _"); print (mfp.compute$formula)}
cox.mfp = coxph(formula = mfp.compute$formula, data = df_train, x = TRUE)
}
return(cox.mfp)
}
method_coxmfp_predict = function(model_coxmfp, df_test, fixed_time){
#predicting event probability from CoxPh for all observations in df_test by fixed_time
predicted_event_prob = method_cox_predict(model_coxmfp, df_test, fixed_time)
return (predicted_event_prob)
}
method_coxmfp_cv = function(df, predict.factors, fixed_time = NaN, cv_number = 5,
seed_to_fix = 100, parallel= FALSE){
# cross-validating CoxPh model, returns performance measures for each CV and averaged metrics
time_0 = Sys.time()
set.seed(seed_to_fix)
# defining output for fixed_time
if (sum(is.nan(fixed_time))>0){fixed_time = round(quantile(df[df$event==1, "time"], 0.85),1)}
cv_folds = caret::createFolds(df$event, k=cv_number, list = FALSE) #use caret to split into k-folds = cv_steps
modelstats_train = list(); modelstats_test = list()
models_for_each_cv = list()
if (!parallel){
for (cv_iteration in 1:cv_number){
df_train_cv = df[cv_folds != cv_iteration, ]
df_test_cv = df[cv_folds == cv_iteration, ]
cox.model = method_coxmfp_train(df_train_cv, eligible_params(predict.factors,df_train_cv))
models_for_each_cv[[cv_iteration]] = cox.model
y_predict_test = method_coxmfp_predict(cox.model, df_test_cv, fixed_time)
y_predict_train = method_coxmfp_predict(cox.model, df_train_cv, fixed_time)
modelstats_test[[cv_iteration]] = method_any_validate(y_predict_test, fixed_time, df_train_cv, df_test_cv, weighted = 1)
modelstats_train[[cv_iteration]] = method_any_validate(y_predict_train, fixed_time, df_train_cv, df_train_cv, weighted = 1)
}
}
df_modelstats_test = data.frame(modelstats_test[[1]])
df_modelstats_train = data.frame(modelstats_train[[1]])
for (i in 2:cv_number){df_modelstats_test[i,]= modelstats_test[[i]]; df_modelstats_train[i,]= modelstats_train[[i]]}
df_modelstats_test$test = 1; df_modelstats_train$test = 0
output = list()
output$test = df_modelstats_test
output$train = df_modelstats_train
output$testaverage = sapply(df_modelstats_test,mean,na.rm=1)
output$trainaverage = sapply(df_modelstats_train,mean,na.rm=1)
output$tuned_cv_models = models_for_each_cv
time_1 = Sys.time()
output$time = time_1 - time_0
return(output)
}
############# SRF ################
srf_survival_prob_for_time = function(rfmodel, df_to_predict, fixed_time, oob = FALSE){
#finds survival prediction from srf model
if (oob) { predicted_matrix = predict(rfmodel, newdata = df_to_predict,
ensemble = "oob", outcome= "test")
} else { predicted_matrix = predict(rfmodel, newdata = df_to_predict)}
j_for_fixedtime = match(1, round(predicted_matrix$time.interest,1) == fixed_time, nomatch = -100);
if (j_for_fixedtime == -100){#print("no fixed time match was found, using closest")
j_for_fixedtime = which.min(abs(predicted_matrix$time.interest - fixed_time))}
if (oob) { y_predicted = predicted_matrix$survival.oob[ ,j_for_fixedtime]
}else{ y_predicted = predicted_matrix$survival[, j_for_fixedtime]}
return(y_predicted)
}
# s = method_srf_train(d_xt, params)
# p = method_srf_predict(s, d_xt, 5)
srf_tune = function(df_tune, cv_number =3,
predict.factors, fixed_time = NaN,
seed_to_fix = 100,mtry= c(3,4,5),
nodesize = c(10,20,50),nodedepth = c(100),
verbose = FALSE, oob = TRUE){
#function to tune survival random forest by mtry, nodesize and nodedepth grid
# if oob = TRUE, there is no CV !!! as OOB does the job already
#take out the factors which are not in df_tune or the ones which take 1 value
predict.factors = eligible_params(predict.factors,df_tune)
#set seed
set.seed(seed_to_fix)
# limit mtry with the number of predictors and nodesize by 1/6 of sample size
if (sum(nodesize > dim(df_tune)[1]/6) > 0) {
if(verbose) print ("Warning - some min nodesize is > 1/6 of the sample size (1/2 of CV fold)")}
nodesize = nodesize[nodesize <= dim(df_tune)[1]/6]
#if all numbers higher than number of factors, only check this factor
if (sum(mtry > length(predict.factors)) == length(mtry)) {
mtry = c(length(predict.factors))}
mtry = mtry[mtry <= length(predict.factors)]
#grid of values to tune
grid_of_values = expand.grid("mtry" = mtry,
"nodesize" = nodesize, "nodedepth" = nodedepth)
if(verbose) print(paste("Grid size is", dim(grid_of_values)[1]))
if (dim(grid_of_values)[1]==0) {
output = list()
return (output)}
# defining output for fixed_time
if (sum(is.nan(fixed_time)>0)| length(fixed_time)>1){ #not implemented for multiple time tuning yet
fixed_time = round(quantile(df_tune[df_tune$event==1, "time"], 0.85),1)}
df_tune$time_f = ifelse(df_tune$time <= fixed_time ,df_tune$time, fixed_time)
df_tune$event_f =ifelse(df_tune$event==1 & df_tune$time <= fixed_time ,1,0)
#going through combinations
modelstats = list( )
if (oob==FALSE) { #we do CV instead of using OOB predictions to tune SRF
set.seed(seed_to_fix)
cv_folds = caret::createFolds(df_tune$event, k=cv_number, list = FALSE) #use caret to split into k-folds = cv_number
for (i in 1:dim(grid_of_values)[1]){
if(verbose) print(grid_of_values[i,])
mtry_i = grid_of_values[i,"mtry"]
nodesize_i = grid_of_values[i,"nodesize"]
nodedepth_i = grid_of_values[i,"nodedepth"]
#train grid combination for each cv_iteration
modelstats_cv = list()
for (cv_iteration in 1:cv_number) {
print (paste("SRF tuning CV step", cv_iteration, "/out of", cv_number))
df_train_cv = df_tune[cv_folds != cv_iteration, ]
df_test_cv = df_tune[cv_folds == cv_iteration, ]
#train SRF
rf.dt = rfsrc(as.formula(paste("Surv(time, event) ~",
paste(predict.factors, collapse="+"))),
data = df_train_cv,
nodesize = nodesize_i, # this is AVERAGE size, so we want this to be quite high
ntree = 300,
mtry = mtry_i,
nodedepth = nodedepth_i,
nsplit = 50,
splitrule = "logrank", statistics= FALSE, membership=TRUE,
importance = "none", #to speed up by switching off VIMP calculations
seed = seed_to_fix
)
#compute predicted event probability for all people
y_predicted = 1-srf_survival_prob_for_time(rf.dt, df_test_cv, fixed_time, oob= FALSE)
validation_stats = method_any_validate(y_predicted, fixed_time, df_train_cv, df_test_cv, weighted = TRUE)
modelstats_cv[[cv_iteration]] = c("mtry" = mtry_i, "nodesize" = nodesize_i, "nodedepth" = nodedepth_i,
"time" = validation_stats$T,
"AUCROC" = validation_stats$AUCROC,
"BS"= validation_stats$BS,
"BS_scaled" = validation_stats$BS_scaled,
"C_score" = validation_stats$C_score,
"Calib_alpha" = validation_stats$Calib_alpha,
"Calib_slope" = validation_stats$Calib_slope)
}#end k-fold CV for one grid combination
#averaging over cv-steps, firs transform to data.frame to use mean()
modelstats_cv_df = data.frame(t(modelstats_cv[[1]]))
for (j in 2:cv_number) {modelstats_cv_df = rbind(modelstats_cv_df,t(modelstats_cv[[j]]))}
modelstats[[i]] = c(modelstats_cv[[1]]["mtry"], modelstats_cv[[1]]["nodesize"],
modelstats_cv[[1]]["nodedepth"],
"AUCROC" = mean(modelstats_cv_df$AUCROC, na.rm=1),
"BS" = mean(modelstats_cv_df$BS, na.rm=1),
"BS_scaled" = mean(modelstats_cv_df$BS_scaled, na.rm=1),
"C_score"= mean(modelstats_cv_df$C_score, na.rm=1),
"Calib_alpha" = mean(modelstats_cv_df$Calib_alpha, na.rm=1),
"Calib_slope" = mean(modelstats_cv_df$Calib_slope, na.rm=1),
"time"= fixed_time)
}#end for grid
} else { # end if(oob==false)
if(verbose) {print ("No internal CV for training SRF,
instead out-of-bag predictions used to assess performance")}
for (i in 1:dim(grid_of_values)[1]){
if(verbose) print(grid_of_values[i,])
mtry_i = grid_of_values[i,"mtry"]
nodesize_i = grid_of_values[i,"nodesize"]
nodedepth_i = grid_of_values[i,"nodedepth"]
rf.dt = rfsrc(as.formula(paste("Surv(time, event) ~",
paste(predict.factors, collapse="+"))),
data = df_tune,
nodesize = nodesize_i, # this is AVERAGE size, so we want this to be quite high
ntree = 300,
mtry = mtry_i,
nodedepth = nodedepth_i,
nsplit = 50,
splitrule = "logrank", statistics= FALSE, membership=TRUE,
importance = "none", #to speed up by switching off VIMP calculations
seed = seed_to_fix)
#compute predicted event probability for all people
y_predicted = 1-srf_survival_prob_for_time(rf.dt, df_tune, fixed_time, oob= TRUE)
validation_stats = method_any_validate(y_predicted, fixed_time, df_tune, df_tune, weighted = TRUE)
modelstats[[i]] = c("mtry" = mtry_i, "nodesize" = nodesize_i,
"nodedepth" = nodedepth_i,
"time" = validation_stats$T,
"AUCROC" = validation_stats$AUCROC,
"BS"= validation_stats$BS,
"BS_scaled" = validation_stats$BS_scaled,
"C_score" = validation_stats$C_score,
"Calib_alpha" = validation_stats$Calib_alpha,
"Calib_slope" = validation_stats$Calib_slope)
} #end for (i in grid)
}#end else
#reshaping into data frame
df_modelstats = data.frame("V1" = modelstats[[1]])
#check if there was more than 1 grid search
if (dim(grid_of_values)[1] >1) {for (i in 2:dim(grid_of_values)[1]){ df_modelstats[i]= modelstats[[i]]}}
df_modelstats = data.frame(t(df_modelstats))
if (verbose == TRUE) {
print(paste("AUC varies from", round(min(df_modelstats$AUCROC ),4), "to", round(max(df_modelstats$AUCROC ),4)))
print(paste("Brier score varies from", round(min(df_modelstats$BS ),4), "to", round(max(df_modelstats$BS ),4)))
print("Combination with highest AUC")
print(df_modelstats[which.max(df_modelstats$AUCROC), c("mtry", "nodesize", "nodedepth")])
print("Combination with lowest Brier Score")
print(df_modelstats[which.min(df_modelstats$BS), c("mtry", "nodesize", "nodedepth")])
print("Combination with lowest AUC")
df_modelstats[which.min(df_modelstats$AUCROC), c("mtry", "nodesize", "nodedepth")]
}
output = list()
output$modelstats = df_modelstats
output$bestbrier = df_modelstats[which.min(df_modelstats$BS), ]
output$bestauc = df_modelstats[which.max(df_modelstats$AUCROC), ]
output$bestcindex = df_modelstats[which.max(df_modelstats$C_score), ]
return(output)
}
method_srf_train = function(df_train, predict.factors,
fixed_time = NaN, inner_cv = 3,
seed_to_fix = 100, fast_version = TRUE, oob = TRUE, verbose = FALSE){
#take out predictors which are not in df_train or constant
predict.factors = eligible_params(predict.factors, df_train)
#for now only for best AUC but can be amended for brier score or cindex
#defining the tuning grid for SRF
p = length(predict.factors) #number of predictors
n = dim(df_train)[1]
#mtry grid
mtry_default = round(sqrt(p),0)
# defining output for fixed_time
if ( sum(is.nan(fixed_time))>0| (length(fixed_time)>1)){
fixed_time = round(quantile(df_train[df_train$event==1, "time"], 0.85),1)}
if (p<=10) {mtry = c(2,3,4,5)}else{if(p<=25){mtry = c(3,5,7,10,15)}else{
mtry = c(round(p/10,0),round(p/5,0), round(p/3,0), round(p/2,0),mtry_default)}}
#minimum nodesize grid
nodesize = seq(min(15, round(n/6-1,0)), max(min(n/10,50),30), 5)
#nodedepth grid
nodedepth = c(50) #we don't tune this so just a big number
if (verbose) {print (paste("mtry", mtry, "nodedepth", nodedepth, "nodesize", nodesize))}
if (fast_version == TRUE) {
#take recommended mtry and check the best depth and node size
tune1 = srf_tune(df_train, cv_number = inner_cv, eligible_params(predict.factors,df_train),
fixed_time = fixed_time, seed_to_fix = seed_to_fix,
mtry = mtry_default,nodesize = nodesize,
nodedepth = nodedepth, oob = oob)
nodesize_best = as.integer(tune1$bestauc["nodesize"])
nodedepth_best = as.integer(tune1$bestauc["nodedepth"])
#using the depth and size check the best mtry
tune2 = srf_tune(df_train, cv_number = inner_cv,eligible_params(predict.factors,df_train),
fixed_time = fixed_time, seed_to_fix = seed_to_fix,
mtry = mtry, nodesize = nodesize_best,
nodedepth = nodedepth_best , oob = oob)
mtry_best = tune2$bestauc["mtry"]
best_combo_stat = tune2$bestauc
modelstatsall = rbind(tune1$modelstats, tune2$modelstats)
}else{
tuneall = srf_tune(df_train, cv_number = inner_cv,
eligible_params(predict.factors,df_train),
fixed_time = fixed_time, seed_to_fix = seed_to_fix,
mtry = mtry,nodesize = nodesize,
nodedepth = nodedepth, oob = oob)
nodesize_best = tuneall$bestauc["nodesize"]
nodedepth_best = tuneall$bestauc["nodedepth"]
mtry_best = tuneall$bestauc["mtry"]
best_combo_stat = tuneall$bestauc
modelstatsall= tuneall$modelstats
}
final.rfs = rfsrc(as.formula(paste("Surv(time, event) ~",
paste(eligible_params(predict.factors,df_train), collapse="+"))),
data = df_train,
nodesize = nodesize_best, # this is AVERAGE size, so we want this to be quite high
ntree = 500,
mtry = as.integer(mtry_best),
nodedepth = as.integer(nodedepth_best),
nsplit = 50,
splitrule = "logrank", statistics= FALSE, membership=TRUE,
importance = "none", #to speed up by switching off VIMP calculations
seed = seed_to_fix
)
output = list()
output$beststats = best_combo_stat
output$allstats = modelstatsall
output$model = final.rfs
#calibrate SRF with the best parameters
return (output)
}
method_srf_predict = function(model_srf, df_test, fixed_time, oob= FALSE){
if (class(model_srf)=="list") {srf = model_srf$model}else{srf = model_srf}
if (length(fixed_time)==1) {return(1- srf_survival_prob_for_time(srf, df_test, fixed_time, oob= oob))}
predicted_event_prob = matrix(nrow = dim(df_test)[1], ncol = length(fixed_time))
for (t in 1:length(fixed_time)){
predicted_event_prob[,t] = 1- srf_survival_prob_for_time(srf, df_test, fixed_time[t], oob= oob)
}
colnames(predicted_event_prob) = round(fixed_time,3)
return (predicted_event_prob)
}
method_srf_cv = function(df, predict.factors, fixed_time = NaN,
cv_number = 3,
inner_cv = 3,
seed_to_fix = 100, parallel=FALSE){
time_0 = Sys.time()
set.seed(seed_to_fix)
# defining output for fixed_time
if (sum(is.nan(fixed_time))>0){fixed_time = round(quantile(df[df$event==1, "time"], 0.85),1)}
predict.factors = eligible_params(predict.factors,df)
if(length(predict.factors)==0){print ("No eliible params"); return (NULL)}
#use caret to split into k-folds = cv_steps
cv_folds = caret::createFolds(df$event, k=cv_number, list = FALSE)
modelstats_train = list(); modelstats_test = list()
srf_models_for_each_cv = list() #saving trained best SRF to re-use in ensemble 1A
print (paste("Cross-validating Survival Random Forest with", cv_number,
"outer loops, and ",inner_cv,"inner loops for model tuning"))
if (!parallel){
for (cv_iteration in 1:cv_number){
print (paste('External loop CV, step number = ', cv_iteration, '/ out of', cv_number))
df_train_cv = df[cv_folds != cv_iteration, ]
df_test_cv = df[cv_folds == cv_iteration, ]
srf.model.tuned = method_srf_train(df_train_cv, predict.factors,fixed_time = fixed_time,
inner_cv = inner_cv, seed_to_fix=seed_to_fix,
fast_version = TRUE, oob = TRUE)
y_predict_test = method_srf_predict(srf.model.tuned, df_test_cv,
fixed_time, oob = FALSE)
y_predict_train = method_srf_predict(srf.model.tuned, df_train_cv,
fixed_time, oob= FALSE)
modelstats_test[[cv_iteration]] = method_any_validate(y_predict_test,
fixed_time, df_train_cv, df_test_cv, weighted = 1)
modelstats_train[[cv_iteration]] = method_any_validate(y_predict_train,
fixed_time, df_train_cv, df_train_cv, weighted = 1)
srf_models_for_each_cv[[cv_iteration]]= srf.model.tuned$model
}
}else{ #parallel cv
}
df_modelstats_test = data.frame(modelstats_test[[1]])
df_modelstats_train = data.frame(modelstats_train[[1]])
for (i in 2:cv_number){df_modelstats_test[i,]= modelstats_test[[i]]
df_modelstats_train[i,]= modelstats_train[[i]]}
df_modelstats_test$test = 1; df_modelstats_train$test = 0
output = list()
output$test = df_modelstats_test
output$train = df_modelstats_train
output$testaverage = sapply(df_modelstats_test,mean,na.rm=1)
output$trainaverage = sapply(df_modelstats_train,mean,na.rm=1)
output$pretrained_srf_models = srf_models_for_each_cv
time_1 = Sys.time()
print (time_1 - time_0)
output$time = time_1 - time_0
return(output)
}
######################### Ensemble 1A ########################
# Method uses Cox model predictions to pass to Survival Random Forest
method_1A_train = function(df_train, predict.factors, fixed_time=NaN, inner_cv = 3,
seed_to_fix = 100, fast_version = TRUE, oob = TRUE,
useCoxLasso = FALSE, var_importance_calc=1){
#the function trains Cox model, then adds its predictions into Survival Random Forest model
# to mimic stacking procedure and reduce overfitting,
# we train Cox model on 0.9 of the data and predict on the rest 0.1 for each 1/10s fold
# so we pass out-of-the-bag prediction to SRF
predict.factors = eligible_params(predict.factors,df_train)
if(length(predict.factors)==0){print ("No eliible params"); return (NULL)}
# defining output for fixed_time
if (sum(is.nan(fixed_time))>0){fixed_time = round(quantile(df_train[df_train$event==1, "time"], 0.85),1)}
#creating folds
cv_folds = caret::createFolds(df_train$event, k=10, list = FALSE)
cindex_train = vector(length = 10); cindex_test = vector(length = 10)
for (cv_iteration in 1:10){
cox_train = df_train[cv_folds != cv_iteration, ]
cox_oob = df_train[cv_folds == cv_iteration, ]
#train cox model on cox_train
cox_m_cv = method_cox_train(cox_train, eligible_params(predict.factors, cox_train),
useCoxLasso =useCoxLasso)
#predict for cox_oob
cox_predict_oob = method_cox_predict(cox_m_cv, cox_oob, fixed_time)
#adding Cox prediction to the df_train in the column "cox_predict"
df_train[cv_folds == cv_iteration, "cox_predict"] = cox_predict_oob
}
##alternatively - just use all the data and pass apparent predictions to SRF
cox_model_for1a = method_cox_train(df_train, eligible_params(predict.factors, cox_train),
useCoxLasso =useCoxLasso)
##df_train$cox_predict = method_cox_predict(cox_model_for1a, df_train, fixed_time )
#adding new factor and tuning SRF model with this added factor using srf_train
predict.factors.1A = c(predict.factors, "cox_predict")
srf_model_for1a = method_srf_train(df_train, predict.factors = predict.factors.1A,
fixed_time=fixed_time, inner_cv = inner_cv,