-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run_Models_CrossValidation.R
793 lines (607 loc) · 38.4 KB
/
Run_Models_CrossValidation.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
# Setup stores for CV performance measures
n_comparisons = 7 #glm, lasso, bayes-flat, bayes-hs, lasso inspired glm, projpred
auc_matrix_train = matrix(data = NA, n_models, n_comparisons)
auc_matrix_gen = matrix(data = NA, n_models, n_comparisons)
auc_matrix_train_diff = matrix(data = NA, n_models, n_comparisons)
auc_matrix_train_diff2_5 = matrix(data = NA, 1, n_comparisons)
auc_matrix_train_diff50 = matrix(data = NA, 1, n_comparisons)
auc_matrix_train_diff97_5 = matrix(data = NA, 1, n_comparisons)
auc_matrix_gen_diff = matrix(data = NA, n_models, n_comparisons)
auc_matrix_gen_diff2_5 = matrix(data = NA, 1, n_comparisons)
auc_matrix_gen_diff50 = matrix(data = NA, 1, n_comparisons)
auc_matrix_gen_diff97_5 = matrix(data = NA, 1, n_comparisons)
sensitivity_matrix_train = matrix(data = NA, n_models, n_comparisons)
sensitivity_matrix_gen = matrix(data = NA, n_models, n_comparisons)
sensitivity_matrix_train_diff = matrix(data = NA, n_models, n_comparisons)
sensitivity_matrix_gen_diff = matrix(data = NA, n_models, n_comparisons)
specificity90_matrix_train = matrix(data = NA, n_models, n_comparisons)
specificity90_matrix_gen = matrix(data = NA, n_models, n_comparisons)
specificity90_matrix_train_diff = matrix(data = NA, n_models, n_comparisons)
specificity90_matrix_gen_diff = matrix(data = NA, n_models, n_comparisons)
specificity95_matrix_train = matrix(data = NA, n_models, n_comparisons)
specificity95_matrix_gen = matrix(data = NA, n_models, n_comparisons)
specificity95_matrix_train_diff = matrix(data = NA, n_models, n_comparisons)
specificity95_matrix_gen_diff = matrix(data = NA, n_models, n_comparisons)
#--------------------------------------------------------------------------------------
##---- Standard GLM Models ------
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
#INTERNAL VALIDATION (Train on training set and test on training set)
#Do GLM cross validation training on (1 - 1/outsidefolds )*100, e.g. train 75% test 25%
generalise_flag <- 0 # if == 1, do not test CV with 20% held out, instead test on specified avicw
cv_desc = ''
p_str <- 'T_GLM_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_CVTestData_', sep = '')
lasso_run = FALSE
print('Run GLM with CrossVal Train only UHB/NBT 80/20 split...')
source(paste(work_path,'CrossValidate_GLM_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df1[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df1, file = paste(output_path,
'GLM_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 1
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
# GENERALISATION
# Do GLM cross validation training on (1 - 1/outsidefolds) but test on excluded dataset
crossval.test.data <- test.data
generalise_flag <- 1 # if == 1, do not test CV with 20% held out, instead test on specified above
p_str <- 'G_GLM_' ##(G)eneralise, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
lasso_run = FALSE
print('Run GLM with CrossVal Train UHB/NBT 80/20 split, but test generalisation Weston')
source(paste(work_path,'CrossValidate_GLM_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df2[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df2, file = paste(output_path,
'GLM_CV_Generalise_Summary_Compendium.csv'
,sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 1
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
# LASSO MODEL
#--------------------------------------------------------------------------------------
#INTERNAL VALIDATION (Train and test on training data with CV split)
#--------------------------------------------------------------------------------------
#Do Lasso cross validation training on (1 - 1/outsidefolds )*100, e.g. train 75% test 25%
#crossval.train.data <- train.data
#don't need to specify crossval.test.data as it will be a % from crossval.train.data
generalise_flag <- 0 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_LASSO_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_CVTestData_', sep = '')
print('Run LASSO with CrossVal Train only UHB/NBT 80/20 split...')
source(paste(work_path,'CrossValidate_LASSO_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df3[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df3, file = paste(output_path, 'LASSO_CV_Train_Summary_Compendium.csv',sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 2
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
if (m_ctr == 1) { #first time create table
cv_batch_varlist_df1 <- data.frame(matrix(data = NA, nrow = 1,
ncol = 4 + length(varnames[[1]])))
colnames(cv_batch_varlist_df1)[1:4] <- c('ModelType', 'Reading', 'Day', 'Outcome')
colnames(cv_batch_varlist_df1)[5:dim(cv_batch_varlist_df1)[2]] <- varnames[[1]]
}
#save variable frequency selection
cv_batch_varlist_df1[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
varcount/(n_models)*100)
write.table(cv_batch_varlist_df1, file = paste(output_path,
'LASSO_CV_Variable_Selection_Compendium.csv',
sep = ''), row.names = FALSE, sep = ',')
#Find which variables in LOO are non-zero at least 50% of the time
equal_above_50 = (varcount/(n_models)*100) >= 50
vname = varnames[[1]]
#save lasso names
lasso_var_ls = vname[equal_above_50]
#LASSO output appends superfluous TRUE, refine so we can save the list for later
for (i in seq(1, length(lasso_var_ls))) {
if (grepl("TRUE", lasso_var_ls[i] , fixed = TRUE)) {
#remove redundant true
lasso_var_ls[i] = substr(lasso_var_ls[i], 1, nchar(lasso_var_ls[i])-4)
}
}
#remove intercept as this is auto created
lasso_var_ls = lasso_var_ls[2:length(lasso_var_ls)]
#add outcome so models can use
lasso_var_ls[length(lasso_var_ls) + 1] = "outcome"
sink(paste(save_path ,cv_desc, '_LASSO_VarNames.txt',sep = ''))
print(lasso_var_ls)
sink()
#--------------------------------------------------------------------------------------
#EXTERNAL VALIDATION (Train on training data and test with validation set)
#--------------------------------------------------------------------------------------
#Do Lasso cross validation training on (1 - 1/outsidefolds )*100, e.g. train 75% test 25%
#crossval.train.data <- train.data
crossval.test.data <- test.data
generalise_flag <- 1 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'G_LASSO_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
print('Run LASSO with CrossVal Train UHB/NBT 80/20 split, but test generalisation Weston')
source(paste(work_path,'CrossValidate_LASSO_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df4[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df4, file = paste(output_path,
'LASSO_CV_Generalise_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 2
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#if (TRUE) { #run bayesian cross val - can take several hours due to model compilation}
#--------------------------------------------------------------------------------------
#----- Bayesian Logistic Regression-------------------
#--------------------------------------------------------------------------------------
#-----------------FLAT PRIOR----------------------------
#INTERNAL VALIDATION Training Data performance
generalise_flag <- 0 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_BAYES_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_TrainData_', sep = '')
prior_type = 'Flat'
print('Run Flat BAYES with CrossVal Train UHB/NBT 80/20 split, test on Train')
source(paste(work_path,'CrossValidate_BAYES_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df5[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df5, file = paste(output_path,
'Flat_BAYES_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 3
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#-----Generalise Performance
#crossval.train.data <- train.data
crossval.test.data <- test.data
generalise_flag <- 1 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'G_BAYES_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
prior_type = 'Flat'
print('Run Flat BAYES with CrossVal Train UHB/NBT 80/20 split, test on Generalise')
source(paste(work_path,'CrossValidate_BAYES_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df6[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df6, file = paste(output_path,
'Flat_BAYES_CV_Generalise_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 3
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#----------------------------HORSE SHOE ----------------------------
#INTERNAL VALIDATION Training Data performance
#crossval.train.data <- train.data
crossval.test.data <- train.data
generalise_flag <- 0 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_BAYES_HS' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_TrainData_', sep = '')
print('Run HS BAYES with CrossVal Train UHB/NBT 80/20 split, test on Train')
prior_type = 'Horseshoe'
source(paste(work_path,'CrossValidate_BAYES_On_Selected_Data.R', sep = ''))
# Projective prediction below relies on an initial bayesian model from BRMS, we can
# save the models here and apply ProjPred variable selection and projection later
BAYES_HS_StatModel_ls = StatModel_ls
#save things to our summary data table
cv_batch_df7[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df7, file = paste(output_path,
'HS_BAYES_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 4
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#-----Generalise Performance
#crossval.train.data <- train.data
crossval.test.data <- test.data
generalise_flag <- 1 # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'G_BAYES_HS_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
prior_type = 'Horseshoe'
print('Run HS BAYES with CrossVal Train UHB/NBT 80/20 split, test on Generalise')
source(paste(work_path,'CrossValidate_BAYES_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df8[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df8, file = paste(output_path,
'HS_BAYES_CV_Generalise_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 4
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#-----------------------------------------------------------------------------------------
#} else { print('Skip Bayesian CV...')}
#-----------------------------------------------------------------------------------------
#REDUCED VARIABLE MODELS
#-----------------------------------------------------------------------------------------
#this seems to be loaded or saved from elsewhere reintialise so it is empty
StatModel_ls <- vector(mode = "list", length = n_models)
#Evaluate LASSO Inspired Model
#--------------------------------------------------------------------------------------
#INTERNAL VALIDATION (Train on training set and test on training set)
#Do GLM cross validation training on (1 - 1/outsidefolds )*100, e.g. train 75% test 25%
#crossval.train.data <- train.data[, lasso_var_ls]
lasso_run = TRUE
#crossval.train.data <- # don't need to specify crossval.test.data as it will be a % from crossval.train.data
generalise_flag = 0 # if == 1, do not test CV with 20% held out, instead test on specified avicw
p_str <- 'T_LASSO_REDUCE_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_CVTestData_', sep = '')
print('Run GLM (LASSO selected reduced vars) CrossVal. Train/Test only UHB/NBT 80/20 split...')
source(paste(work_path,'CrossValidate_GLM_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df9[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df9, file = paste(output_path,
'GLM_LASSO_REDUCED_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 5
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
#Do GLM cross validation training on (1 - 1/outsidefolds) but test on excluded dataset
#for generalisation
#crossval.train.data <- train.data[, lasso_var_ls]
lasso_run = TRUE
crossval.test.data <- test.data[, lasso_var_ls]
generalise_flag <- 1 # if == 1, do not test CV with 20% held out, instead test on specified above
p_str <- 'G_LASSO_REDUCE_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
print('Run GLM (LASSO selected reduced vars) with CrossVal Train UHB/NBT 80/20 split, but test generalisation Weston')
source(paste(work_path,'CrossValidate_GLM_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df10[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df10, file = paste(output_path,
'GLM_LASSO_REDUCED_CV_Generalise_Summary_Compendium.csv'
,sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 5
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
###---------------------
# PROJECTIVE PREDICTION
###---------------------
#--------------------------------------------------------------------------------------
# FULL VARIABLE PROJPRED MODEL (HORSE SHOE PRIOR)
# INTERNAL VALIDATION (Training Data performance)
#--------------------------------------------------------------------------------------
# SLOW!!!!!!!
# Using naive variable selection speeds up results (default)
# LOO is much slower
crossval.test.data = train.data
varselect_flag = TRUE
generalise_flag = FALSE # if == 1, do not test CV with 20% held out, instead test on specified
#we do not need to recompute the BRMS fits (generalise flag is a slight misnomer as
# it really means recompute models (which we don't need in generalisation either)
# we can use StatModel_ls from the ones in BAYES_HS_StatModel_ls
p_str <- 'T_PP_FULL_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_TrainData_', sep = '')
#if testing all variables set solution tems list to null
solterms_ls = NULL
nt_max = 70
print('Run full ProjPred with CrossVal Train UHB/NBT 80/20 split, test on Train')
prior_type = 'Horseshoe'
source(paste(work_path,'CrossValidate_ProjPred_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df11[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df11, file = paste(output_path,
'ProjPred_FULL_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 6
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
# GENERALISATION
# FULL VARIABLE PROJPRED MODEL (HORSE SHOE PRIOR)
#--------------------------------------------------------------------------------------
crossval.test.data <- test.data
varselect_flag = FALSE
generalise_flag <- TRUE # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_PP_FULL_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
print('Run full ProjPred with CrossVal Train UHB/NBT 80/20 split, test on Train')
prior_type = 'Horseshoe'
source(paste(work_path,'CrossValidate_ProjPred_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df12[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df12, file = paste(output_path,
'ProjPred_FULL_CV_Generalise_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 6
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#-------------------------------------------------------
# REDUCED VARIABLE PROJPRED MODEL
#-------------------------------------------------------
# INTERNAL VALIDATION Training Data performance
crossval.test.data <- train.data
#this will reuse the models from above as we do not need to recompute whole model
#just need to reproject the smaller version using solterms_ls
varselect_flag = FALSE
generalise_flag <- TRUE # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_PP_REDUCE_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_TrainData_', sep = '')
#limited set of terms selected from running CV varsel LOO on entire 590 training set
#this is a bit unconventional as each 80/20 split might come up with a different set
#of variables, so this is more of a test of the performance across 80/20 splits than
#mimcking the LASSO varibale frequency
solterms_ls = c( "UreaAbnormal", "UreaNA", ##AUC=0.62
"Age", #AUC=0.64
"PTAbnormal","PTNA", ##AUC=0.68
"NLRMild","NLRModerate","NLRSevere","NLRNA" #AUC = G 0.70 // I 0.75 **
)
nt_max = length(solterms_ls)
print('Run small ProjPred with CrossVal Train UHB/NBT 80/20 split, test on Train')
prior_type = 'Horseshoe'
source(paste(work_path,'CrossValidate_ProjPred_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df13[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df13, file = paste(output_path,
'ProjPred_REDUCE_CV_Train_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 7
auc_matrix_train[,cnum] = auc_store
sensitivity_matrix_train[,cnum] = sensitivity_store
specificity90_matrix_train[,cnum] = spec90_mat[3,]
specificity95_matrix_train[,cnum] = spec95_mat[3,]
#--------------------------------------------------------------------------------------
# GENERALISE TEST
# REDUCED VARIABLE PROJPRED
crossval.test.data <- test.data
varselect_flag = FALSE
generalise_flag <- TRUE # if == 1, do not test CV with 20% held out, instead test on specified
p_str <- 'T_PP_REDUCE_' #(T)rain, text prefix for roc curve compendium variables
cv_desc = paste(p_str , 'Train_CVTrainData_Test_GeneraliseData_', sep = '')
print('Run small ProjPred with CrossVal Train UHB/NBT 80/20 split, test on Train')
prior_type = 'Horseshoe'
source(paste(work_path,'CrossValidate_ProjPred_On_Selected_Data.R', sep = ''))
#save things to our summary data table
cv_batch_df14[m_ctr,] <- c(mnum, readingwanted_str, dateRange, outcome_str,
median(auc_store), auc_quantile[1],auc_quantile[2],auc_quantile[3],
median(brier), brier_quantile[1],brier_quantile[2],brier_quantile[3],
median(lambda.store), lambda.store_quantile[1],
lambda.store_quantile[2],lambda.store_quantile[3],
median(sensitivity_store),sensitivity_quantile[1],
sensitivity_quantile[2],sensitivity_quantile[3],
median(specificity_store),specificity_quantile[1],
specificity_quantile[2],specificity_quantile[3],
specificity90_spec_quantile[1],
specificity90_spec_quantile[2],specificity90_spec_quantile[3],
specificity95_spec_quantile[1],
specificity95_spec_quantile[2],specificity95_spec_quantile[3])
write.table(cv_batch_df14, file = paste(output_path,
'ProjPred_REDUCE_CV_Generalise_Summary_Compendium.csv',
sep = ''),
row.names = FALSE, sep = ',')
#save AUC, sensitivty, spec (@ 0.9 and 0.95 sensitivity) for delta calculation
cnum = 7
auc_matrix_gen[,cnum] = auc_store
sensitivity_matrix_gen[,cnum] = sensitivity_store
specificity90_matrix_gen[,cnum] = spec90_mat[3,]
specificity95_matrix_gen[,cnum] = spec95_mat[3,]
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
sink(paste(save_path ,cv_desc, '_AUC_Difference_table.csv',sep = ''))
print('AUC difference CIs compared to Bayesian HS (cnum 4)')
print('auc difference train')
#prepare table with distribution of differences between models (using GLM as standard)
for (i in seq(1,n_comparisons)) {
auc_matrix_train_diff[,i] = auc_matrix_train[,i] - auc_matrix_train[,4]
auc_matrix_train_diff2_5[i] = quantile(auc_matrix_train_diff[,i],0.025, na.rm = TRUE)
auc_matrix_train_diff50[i] = quantile(auc_matrix_train_diff[,i],0.5, na.rm = TRUE)
auc_matrix_train_diff97_5[i] = quantile(auc_matrix_train_diff[,i],0.975, na.rm = TRUE)
s = sprintf('%8.2f [%8.2f,%8.2f]',auc_matrix_train_diff50[i],auc_matrix_train_diff2_5[i],auc_matrix_train_diff97_5[i])
print(s)
}
print('auc difference generalize')
for (i in seq(1,n_comparisons)) {
auc_matrix_gen_diff[,i] = auc_matrix_gen[,i] - auc_matrix_gen[,4]
auc_matrix_gen_diff2_5[i] = quantile(auc_matrix_gen_diff[,i],0.025, na.rm = TRUE)
auc_matrix_gen_diff50[i] = quantile(auc_matrix_gen_diff[,i],0.5, na.rm = TRUE)
auc_matrix_gen_diff97_5[i] = quantile(auc_matrix_gen_diff[,i],0.975, na.rm = TRUE)
s = sprintf('%8.2f [%8.2f,%8.2f]',auc_matrix_gen_diff50[i],auc_matrix_gen_diff2_5[i],auc_matrix_gen_diff97_5[i])
print(s)
}
sink()
#create table with quartiles