forked from EGates1/OneStepDataAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdsurvival_analysis.RMD
More file actions
executable file
·1231 lines (872 loc) · 44.2 KB
/
Copy patholdsurvival_analysis.RMD
File metadata and controls
executable file
·1231 lines (872 loc) · 44.2 KB
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
---
title: "Survival Analysis"
output: pdf_document
params:
csvPath: exampledata.csv
outCsvName: !r NULL
survival: survival
status: !r NULL
regexp: original
fixed_inputs: !r NULL
exclude_inputs: !r NULL
weights: !r NULL
hierarch: !r FALSE
nfold: !r NULL
leaveOneOut: !r TRUE
foldID: !r NULL
summaryfilebase: !r NULL
cutoff: 0.7
boruta: !r TRUE
univariate: !r TRUE
removeCorrelated: !r TRUE
rescale: !r FALSE
plot: !r TRUE
breaks: !r NULL
---
## General Survival Prediction
Input parameter descriptions:
outCsvname is the file name/path for thesamed csv with validation predictions. If null, uses the input csv name as a base
regexp parameter is a regular expression to get input features
fixed_inputs is a regular expression that provides clinical inputs (age, kps, etc.) to bypass variable selection and are used in every survival model.
weights should be the name of a single column with numeric weights to use in model training.
exclude_inputs (regexp) are features known to be outliers and are excluded from inputs that match regexp:
breaks are time breakpoints (careful with units)! for short/mid/long survivival times. Current code can only handle either two categories ( breaks=c(-Inf,10,Inf) ) or three categories: breaks=c(0,10,15,Inf)
hierarch adds predicted survivor type to all regresion modeling by adding predictedSurvivorType to fixed_inputs
cutoff is a correlation threshold that roughly defines equivalent variables. It is used to 1) to filter inputs before boruta method, 2) determine how manysimilar variables to the final model inputs to print. Recommended to keep >0.7
nfold breaks the data into n evenly distrubuted folds. Used with foldID to holdout an independent test set. This is held out for the entire script and used to benchmark the accuracy of the final model.
foldID (ex. 4) is used as the hold out set and the full script is run on just the remaining data (ex. fold 1,2,3 and 5 if nfold=5). Default NULL uses all folds as training.
The status parameter encodes censoring of survival data, this code is expecting 0=censored, 1 = event.
Generally, do not trust censored data with short outcome times. This situation could ultimately have either long or short response time and is thus not included in the training data.
https://towardsdatascience.com/what-metrics-should-we-use-on-imbalanced-data-set-precision-recall-roc-e2e79252aeba
https://www.kaggle.com/lct14558/imbalanced-data-why-you-should-not-use-roc-curve
https://acutecaretesting.org/en/articles/precision-recall-curves-what-are-they-and-how-are-they-used
# Compiled: `r format(Sys.time(), "%Y-%b-%d %H:%M:%S")`
Print params to stdout: `r print(params)`
```{r libs, echo=FALSE}
# install.packages("PRROC",repos='http://cran.us.r-project.org')
libs <- c("corrplot", "survival", "ggplot2", "caret", "knitr", "MASS", "Boruta", "gridExtra", "randomForest","reshape2","glmnet", "pROC", "PRROC")
invisible(lapply(libs, require,character.only=T))
# seed for reproducibility.
set.seed(25)
```
## A summary of the data:
Please note: to remove missign NA values from the data individual cases with missing values are removed.
Future versions may allow a choice to remove inputs (columns) or cases (rows)
```{r Loading Data and Summary, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
# load data
datFull <- read.csv(params$csvPath)
survival <- params$survival
status <- params$status
inputs <- union(params$fixed_inputs,
grep(params$regexp, names(datFull),value=T))
#get weights for training data
if(!is.null(params$weights) ){
trainingWeights <- params$weights
if(!trainingWeights %in% names(datFull) || !is.numeric(datFull[,trainingWeights])){
stop("weight parameter is not the name of a numeric column")
}
} else {
trainingWeights <- NULL
}
print(sprintf("%d rows and %d columns read from csv file: %s", nrow(datFull), ncol(datFull), params$csvPath))
print(sprintf("%d inputs found with regexp parameter and fixed inputs", length(inputs)))
# remove excluded inputs if defined
if (!is.null(params$exclude_inputs)){
inputs <- union( setdiff(inputs, grep(params$exclude_inputs, names(datFull),value=T)), params$fixed_inputs)
print(sprintf("%d inputs remained after excluded inputs", length(inputs)))
} else {
print("No excluded_inputs from parameters")
}
# remove unnecessary columns and clean NAs
datCrop <- datFull[,c(inputs,survival,status,trainingWeights)]
#remove incomplete cases
incompCases <- !complete.cases(datCrop)
print("The following rows are excluded for missing data:")
print(row.names(datFull)[incompCases])
datCompleteCases <- datCrop[!incompCases,]
if(nrow(datCompleteCases) < 5){
stop("Less than 5 rows had no missing input values")
}
# remove inputs with NA or infinite values (should do nothing if complete cases was applied)
dat <- datCompleteCases[, (colSums(is.na(datCompleteCases)) == 0 ) & (sapply(datCompleteCases, function(x) sum(is.infinite(x)) ) == 0) ]
# Add categorical responders/non responders
if(!is.null(params$breaks)){
mybreaks = params$breaks
} else {
print("No time breaks given, cutting survival times into two equal groups")
mybreaks = c(-Inf,median(dat[,survival]),Inf)
}
if( any(mybreaks %in% dat[,survival]) ){warning("One survival time is exactly coincident with a break value, this will create a NA")}
if(length(mybreaks ) ==3) {
timelabs <- c("short","long")
} else if(length(mybreaks) ==4){
timelabs <-c("short","mid","long")
} else { stop("breaks must have length 3 or 4, ex: c(-Inf,10,Inf) for short/long") }
survivorType = cut(as.matrix(dat[survival]), breaks = mybreaks,
labels=timelabs
)
# subset based on validation set if provided
if(!is.null(params$foldID)){
set.seed(5)
#TODO: translate validation indices back to full data frame
if(!("validationFolds" %in% names(datFull)) ){
datFull$validationFolds <- rep(NA,nrow(datFull))
validationFolds <- createFolds(dat[,survival],k=params$nfold,list=F)
# add kfold indices to data matrix
dat$validationFolds <- validationFolds
datFull[row.names(dat),"validationFolds"] <- validationFolds
} else {
print(sprintf("validationFolds column already exists in the csv, reading it instead of generating folds."))
dat$validationFolds <- datFull[row.names(dat),"validationFolds"]
}
validationidx <- which(validationFolds == params$foldID) #gives row numbers (not row names), this will not match the original (nonfiltered) data
print(paste("Leaving out ", params$validationID, " fold number ", params$foldID,sep=""))
#bad code: order of these two lines matters
validationdat <- dat[validationidx,]
print("removing validation fold from data frame")
dat <- dat[-validationidx,]
}
# Pre-select training row subsets for classification/regression
# For classification, censored responders are Ok to include, for regression only use known time-to-events
classificationTrainingRows <- !is.na(dat[,status])
regressionTrainingRows <- !is.na(dat[,status])
#classificationTrainingRows <- dat[,status]==1 | survivorType == timelabs[length(timelabs)]
#regressionTrainingRows <- dat[,status]==1 | survivorType == timelabs[length(timelabs)]
# create data split for LOOCV or k-fold
if(params$leaveOneOut) {
classificationTrainingFolds <- lapply(1:sum(classificationTrainingRows),function(x) setdiff(1:sum(classificationTrainingRows), x))
regressionTrainingFolds <- lapply(1:sum(regressionTrainingRows), function(x) setdiff(1:sum(regressionTrainingRows), x))
} else {
#default else use nfold
set.seed(12)
classificationTrainingFolds <- createFolds(dat[classificationTrainingRows,survival],
k=ifelse(is.null(params$nfold),5,params$nfold),
list=TRUE,
returnTrain=TRUE)
set.seed(12)
regressionTrainingFolds <- createFolds(dat[regressionTrainingRows,survival],
k=ifelse(is.null(params$nfold),5,params$nfold),
list=TRUE,
returnTrain=TRUE)
}
print("List of training data used for each fold of CLASSIFICATION cross validation:")
ifelse(params$leaveOneOut,print("Leave-One-Out"),print(classificationTrainingFolds))
print("List of training data used for each fold of REGRESSION cross validation:")
ifelse(params$leaveOneOut,print("Leave-One-Out"),print(regressionTrainingFolds))
# overlapping histograms are hard to read/interpret, plotting individually
histinfo<-hist(dat[,survival],
main="Histogram of outcome for all observations",
xlab=survival,
border="blue",
col="green",
# xlim=c(0,100),
las=1,
breaks=50)
print("")
hist(dat[dat[,status] == 1,survival],
main="Histogram of NON-censored observations",
xlab=survival,
border="black",
col="blue",
# xlim=c(0,100),
las=1,
breaks=50,
add = F)
print("")
hist(dat[dat[,status] != 1,survival],
main="Histogram censored observations",
xlab=survival,
border="black",
col="red",
# xlim=c(0,100),
las=1,
breaks=50,
add = F)
print(summary(dat[,survival]))
quantile(dat[,survival], .30)
quantile(dat[,survival], .40)
# If no status, assume all obserations are non-censored
if( is.null(params$status) || !(params$status %in% names(dat))) {
status <- "status"
dat[status] <- 1
print("Assuming no subjects censored, either no status is given or status variable not found")
}
#Plot overall survival curve
survObj <- Surv(as.matrix(dat[survival]), as.matrix(dat[status]))
plot(survfit(survObj ~ 1), xlab = "Survival", ylab= "Survival Probability", col=1, main=sprintf("Survival curve, %d cases",nrow(dat) ))
```
## Mass plotting of histograms can be disabled with the _plot_ parameter
`r length(inputs)` inputs are used for initial analysis.
```{r Input Histograms, echo=FALSE, warning=FALSE, message=FALSE, fig.width=3.5, fig.height=5}
if (length(inputs) > 500) {warning("greater than 500 inputs for plotting!!")}
if(params$plot){
for (varname in inputs) {
if(!is.factor(dat[,varname])){
maxrow <- which(dat[,varname] == max(dat[,varname]))
hist(dat[,varname], col="gray", main = paste("All observations\nMax in row:", maxrow), xlab=varname)
# plot(dat[dat[,status]==1,varname], dat[dat[,status]==1,survival], xlab=varname, ylab=survival, pch=21, bg='gray',col='black', main=sprintf("Pearson r: %0.3f, non-censored only",cor(dat[dat[,status]==1,varname],dat[dat[,status]==1,survival],method="pearson")))
plot(dat[,varname], dat[,survival], xlab=varname, ylab=survival, pch=21, bg=dat[,status],col="black", main=sprintf("Pearson r on non-censored: %0.3f,\nunfilled=censored",cor(dat[regressionTrainingRows,varname],dat[regressionTrainingRows,survival],method="pearson")))
print(summary(dat[,varname]))
}
}
}
```
## Survival Modeling:
Columns with NA are excluded from the cox model for being a linar combination of other columns
The HAZARD ratio is given by the *exp(coef)* column in the cox model output.
First we determine which variables are univariate correlated with survival using the cox model and standard linear correlation.
Next we use a multiple input cox model and stepwise AIC on the result.
Bouta method (with correlation reduction) can be used with the _boruta_ parameter.
```{r Univariate Filter, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
## Moved to first block
#if(!is.null(params$breaks)){
# mybreaks = params$breaks
#} else {
# print("No time breaks given, cutting survival times into two equal groups")
# mybreaks = c(-Inf,median(dat[,survival]),Inf)
#}
#if( any(mybreaks %in% dat[,survival]) ){warning("One survival time is exactly coincident with a break value, this will create a NA")}
#
#if(length(mybreaks ) ==3) {
# timelabs <- c("short","long")
#} else if(length(mybreaks) ==4){
# timelabs <-c("short","mid","long")
#} else { stop("breaks must have length 3 or 4, ex: c(-Inf,10,Inf) for short/long") }
#survivorType = cut(as.matrix(dat[survival]), breaks = mybreaks,
# labels=timelabs
# )
datClass <- dat
datClass$survivorType <- survivorType
variableSelections <- list()
#use fixed only if length > 1 (avoid bugs)
scopefixed_inputs = NULL
if(!is.null(params$fixed_inputs) ){
scopefixed_inputs = params$fixed_inputs
variableSelections$fixed <- params$fixed_inputs
}
#TODO: test for significant short/mid/long differences too.
#list of formulas, inputs automatically includes fixed inputs
univFormulas <- sapply(inputs, function(x) as.formula(paste('survObj~',x)))
#list of models
coxUniModels <- lapply(univFormulas,
function(x) { coxph(x, data=dat) }
)
# Hazard ratio and CI
univResults <- lapply(coxUniModels, function(x) {return(exp(cbind(coef(x),confint(x))))})
# Wald test for betas
univPvals <- lapply(coxUniModels, function(x) {summary(x)$waldtest["pvalue"]})
#TODO: change p value threshold
variableSelections$coxunivariate <- union(params$fixed_inputs, inputs[which(univPvals < 0.05)]) #discard variables above threshold (also handles NAs)
print("Univariate significant variables via Cox model:")
print(variableSelections$coxunivariate)
if(length(variableSelections$coxunivariate)>1){
corrplot(cor(Filter(is.numeric,dat[variableSelections$coxunivariate]), use="pairwise"),
title = "Correlations for Cox Univariate method",
mar = c(1,2,2,0),
order = "hclust" )
print(" ")
}
```
# Non-cox variable selection based on linear correlation and Boruta method if enabled:
```{r Boruta Variable, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
print("Pre-processing to remove correlated variables before Boruta method")
#pre-process with correlations
ppMethods <- "zv"
if(params$rescale) {ppMethods <- c(ppMethods,"center","scale")}
# BUG: "zv" and "corr" do not work together for some inputs...
#if(params$removeCorrelated) {ppMethods <- c(ppMethods, "corr")}
Filteredinputs <- setdiff(inputs,params$fixed_inputs)
#apply correlation removal (not to fixed inputs)
if(length(Filteredinputs)>1){
pp <- preProcess(dat[classificationTrainingRows,Filteredinputs], method = ppMethods, cutoff=params$cutoff)
print(pp)
#get reduced input set/dataset
borutaInputs <- union( names(predict(pp, newdata = dat[Filteredinputs])), params$fixed_inputs)
} else {
print("Only 1 input, no correlation reduction before boruta")
borutaInputs <- union(Filteredinputs,params$fixed_inputs)
}
if(params$boruta){
#TODO: document boruta "Confirmed" vs "Tentative"
bor <- Boruta(x=datClass[classificationTrainingRows, borutaInputs ], y=datClass[classificationTrainingRows, "survivorType"], pValue=.35)
variableSelections$boruta <- union(params$fixed_inputs, names(datClass[,borutaInputs ])[which(bor$finalDecision == "Confirmed")])
#if boruta rejects everything, remove it from selections
if(!any(bor$finalDecision=="Confirmed") ) {
print("Boruta rejected every variable, removing boruta selection from list of variables")
variableSelections$boruta <- NULL
}
print("Finished Boruta variable selection")
print(bor)
if(length(variableSelections$boruta) > 1 & params$plot){
correlations <- cor(Filter(is.numeric,dat[classificationTrainingRows,variableSelections$boruta]), use="pairwise")
corrplot(correlations,
title = "Correlations for Boruta method",
mar = c(1,2,2,0),
order = "hclust" )
print(" ")
}
}
# Univariate (correlation) selection
if(params$univariate){
nums <- sapply(dat[inputs],is.numeric)
nums <- names(nums)[nums] #get names not T/F
pvals <- lapply(nums,
function(var) {
test <- cor.test(dat[dat[,status]==1, c(var)], dat[dat[,status]==1, survival])
test$p.value #could use 1-pchisq(test$statistic, df= test$parameter)
})
names(pvals) <- nums
#TODO: adjust p-value threshold with parameter
pvalThresh <- 0.15
print(sprintf("p-value threshold for linear correlation, %f",pvalThresh))
variableSelections$univariate <- union(nums[which(pvals < pvalThresh)], params$fixed_inputs) #discard variables above threshold
if(length(variableSelections$univariate) >1 ){
corrplot(cor(Filter(is.numeric,dat[dat[,status]==1,variableSelections$univariate]), use="pairwise"),
title = "Correlations for Linear Univariate method",
mar = c(1,2,2,0),
order = "hclust" )
}
}
```
## Further reduce the number of variables by making a multivariate cox model and checking for high correlations
```{r Multivariate, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
uniInputs <- variableSelections$coxunivariate
coxMultiModel <- coxph(survObj~., data=dat[,union(params$fixed_inputs,uniInputs),drop=F])
multiPvals <- coef(summary(coxMultiModel))[,5]
print("Multivariate cox model")
print(coxMultiModel)
variableSelections$multivariate <- union(params$fixed_inputs, names(multiPvals)[na.omit(multiPvals < 0.05)])
#TODO: correct for multiple comparisons
if(length(variableSelections$multivariate) >1 ){
corrplot(cor(Filter(is.numeric,dat[variableSelections$multivariate]), use="pairwise"),
title = "Correlations for Multivariate Cox method",
mar = c(1,2,2,0),
order = "hclust" )
}
```
# Finally, filter the model using stepwise AIC or recursive feature elimination
```{r stepwise AIC, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
#need to make the model on complete cases only
#ENH: dat should already be complete cases only
# datComplete <- dat[complete.cases(dat[,c(status,survival,variableSelections$multivariate)]) ,
# c(status,survival,variableSelections$multivariate) ]
#
#survObjComplete <- Surv(as.matrix(datComplete[survival]),as.matrix(datComplete[status]))
coxAIC <- coxph(survObj ~ ., data=dat[,variableSelections$multivariate,drop=F])
coxStep <- stepAIC(coxAIC, direction="both", trace=0)
print("variables selected by stepwise AIC (not including fixed inputs)")
print(coxStep$coefficients)
variableSelections$aic <- union(params$fixed_inputs, names(coxStep$coefficients))
if(length(variableSelections$aic) >1 ){
corrplot(cor(Filter(is.numeric,dat[variableSelections$aic]), use="pairwise"),
title = "Correlations for stepwise AIC method",
mar = c(1,2,2,0),
order = "hclust" )
}
```
## Categorical Summary: We break the dataset into short vs long survival categories and use the predicted survival class as an additional feature
# Modeling does NOT include censored data unless the censored observation is a "long" survival time
```{r Classification, echo=FALSE, warning=FALSE, message=FALSE, fig.width=6, fig.height=7}
#if(params$leaveOneOut){
#predictedSurvivorType = cut(results$pred, breaks = breaks, labels=c("short","mid","long"))
#}
#kable(table((data.frame(predicted=predictedSurvivorType, actual=survivorType))))
#TODO: make parameters different from continuous predictions for clarity
#TODO: adjust models based on number of survivor categories
#TODO: adjust for k-fold vs LOOCV
modelparams <- list(forest = list(method = "rf",
ntree = 500,
#tuneGrid = data.frame(.mtry = mtry),
#replace = TRUE,
#na.action = randomForest::na.roughfix,
importance = FALSE,
predict.all = FALSE
),
# xgboost = list(method = "xgbTree"),
#nnet = list(method = "nnet",
# #tuneGrid=data.frame(.size = 10, .decay = 0),
# #linout = TRUE,
# skip = TRUE,
# MaxNWts = 10000,
# trace = FALSE,
# maxit = 100),
svm = list(method = "svmRadial"),
gausspr = list(method="gaussprRadial")#,
# LOGIT (glm) only uses two outputs
# logit = list(method = "glm")
)
modelformula <- as.formula("survivorType~.")
dataparams <- list(form = modelformula,
# data = dat[,c(params$target,Filteredinputs)],
metric="Accuracy",
# weights = trainingWeights,
trControl=trainControl(allowParallel = T,
method = ifelse(params$leaveOneOut,"LOOCV", "repeatedcv"),
classProbs=TRUE,
returnResamp = "final",
savePredictions = "final",
index = classificationTrainingFolds, #number = 10,
#repeats= 5,
verboseIter = F) # use method="none" to disable grid tuning for speed
)
caretparams <- lapply(modelparams,function(x) c(dataparams,x))
#initialize outputs
modelsClass <- list()
acc <- list()
for(jjj in 1:length(variableSelections)){
#take uncensored rows (leave censored rows if survivor type is "long")
if(! is.null(params$status)){
modeldata <- datClass[classificationTrainingRows, c("survivorType",variableSelections[[jjj]])]
weightList <- datClass[classificationTrainingRows, trainingWeights]
} else {
modeldata <- datClass[classificationTrainingRows,c("survivorType",variableSelections[[jjj]])]
weightList <- datClass[classificationTrainingRows, trainingWeights]
}
# RE seeding: Hawthorn et al, "The design and analysis of benchmark experiments" (2005)
for (iii in 1:length(modelparams)){
model_name <-paste("Class", names(variableSelections)[jjj], names(modelparams)[iii],sep="_")
print(model_name)
set.seed(3141) #seed before train to get same subsamples
# FIXME: @egates, RF class weights not used
modelsClass[[model_name]] <- do.call(caret::train, c(caretparams[[iii]], list(data=modeldata, weights=weightList)))
metric <- modelsClass[[model_name]]$metric
#get best acuracy manually for LOOCV
acc[[model_name]] <- max(modelsClass[[model_name]]$results[metric])
}
}
#TODO: enable k-fold, check if this works with not LOOCV
#if(params$leaveOneOut){
maxacc <- max(unlist(acc))
maxmodels <- names(which(acc==maxacc))
bestClassifier <- modelsClass[[maxmodels[[1]]]] #pick first by default
resultsClass <- bestClassifier$pred
#}
accs_with_ci =data.frame( lapply(modelsClass,function(x) confusionMatrix(x$pred$pred,reference = x$pred$obs, positive = timelabs[length(timelabs)])$overall))
# print all model accuracies
print(sprintf("%d rows used for classification", nrow(modeldata)))
print("Table of accuracies by model and input variables")
kable(unlist(acc))
kable(accs_with_ci)
print(bestClassifier)
print(caret::confusionMatrix(resultsClass$pred, reference = resultsClass$obs, positive = timelabs[length(timelabs)]) )
if(length(timelabs) == 2){
# FIXME: @egates1, can you generalize the predictor input?
ROCdropcensor <- roc(response = resultsClass$obs, levels = timelabs, predictor = resultsClass$long, ci=T, of="auc")
# seed the bootstrap ROC interval
set.seed(555)
spec.ci = ci.se(ROCdropcensor, specificities = seq(0,1,0.05), boot.n=500)
print(ROCdropcensor)
plot(ROCdropcensor, print.auc=T)
plot(spec.ci,type="shape", col="lightgray")
print("")
dropcensoraccuracy = (ROCdropcensor$specificities * sum(ROCdropcensor$response==ROCdropcensor$levels[1]) + ROCdropcensor$sensitivities * sum(ROCdropcensor$response==ROCdropcensor$levels[2]) ) / length(ROCdropcensor$response)
plot(ROCdropcensor$thresholds,dropcensoraccuracy , xlab = "probability threshold", ylab= "Accuracy" )
# confusion matrix at max
idmax = which(dropcensoraccuracy ==max(dropcensoraccuracy ))
maxthreshold = ROCdropcensor$thresholds[idmax ]
print(caret::confusionMatrix( factor(ifelse( resultsClass$long < maxthreshold , timelabs[1],timelabs[length(timelabs)]), levels=timelabs), reference = resultsClass$obs, positive = timelabs[length(timelabs)]) )
}
print("Concordance < 0.5 may be due to improper factor level ordering")
#Add predictions on censored data as well
fullClassPreds <- factor(levels = levels(datClass$survivorType))
fullClassPreds[which(datClass[,status]!= 0)] = bestClassifier$pred$pred
fullClassPreds[which(datClass[,status]== 0)] = predict(bestClassifier,datClass[datClass[status] != 1,]) #make predictions on censored obs
#WIP make this concordance meaningful
print("Warning: concordance for class predictions might be erroneous")
print(survConcordance(survObj~fullClassPreds))
# add predictions to variable selections if hierarchical
if(params$hierarch){
print("Adding predSurvivorType to all input sets")
variableSelections <- lapply(variableSelections, function(x) c("predSurvivorType",x))
inputs <- c(inputs,"predSurvivorType")
}
#This line is the one that needs LOOCV
dat$predSurvivorType <- fullClassPreds
```
# ROC analysis if survival classes are just short/long
```{r ROC analysis if only 2 classes, echo=FALSE}
#WIP - @egates1 - for multi-class, need 1 ROC for each class, 0 = the class prediction, 1 = everything else
#check if binary short/long survival
if(length(timelabs) == 2){
probLong <- numeric()
#get probabilistic prediction
#probLong[which(datClass[,status] == 1)] = predict(bestClassifier,datClass[datClass[status] == 1,], type="prob")[[ timelabs[2] ]] #make predictions on known obs
#combine hold-out predictions
probLong[classificationTrainingRows] = bestClassifier$pred[, timelabs[2] ] #make predictions on known obs
if( any(!classificationTrainingRows)){
probLong[!classificationTrainingRows] = predict(bestClassifier,datClass[!classificationTrainingRows,], type="prob")[[ timelabs[2] ]] #make predictions on censored/short obsvervations
}
ROC1 <- roc(response = datClass$survivorType,
levels = timelabs,
predictor = probLong)###
print(ROC1)
plot(ROC1)
print("")
# plot accuracy
fulldataaccuracy = (ROC1$specificities * sum(ROC1$response==ROC1$levels[1]) + ROC1$sensitivities * sum(ROC1$response==ROC1$levels[2]) ) / length(ROC1$response)
plot(ROC1$thresholds,fulldataaccuracy, xlab = "probability threshold", ylab= "Accuracy" )
# confusion matrix at max
idmax = which(fulldataaccuracy ==max(fulldataaccuracy ))
maxthreshold = ROC1$thresholds[idmax ]
print(caret::confusionMatrix( factor(ifelse( probLong < maxthreshold , timelabs[1],timelabs[length(timelabs)]),levels=timelabs), reference = datClass$survivorType, positive = timelabs[length(timelabs)]) )
} else { #else >2 classes
# WIP: finish multiclass ROC. Does this need retraining or just thresholding?
for(kkk in 1:(length(timelabs)-1 )){
shortclass <- paste(timelabs[1:kkk], collapse="_")
longclass <- paste(timelabs[kkk+1:length(timelabs),collapse="_"])
}
}
# use RF on fixed variables as reference control
# TODO: check if fixedClassifier needs to be changed to probability not factor output
if(!is.null(scopefixed_inputs) ){
fixedClassPreds <- numeric()
fixedClassifier <- modelsClass$Class_fixed_forest
fixedClassPreds[ classificationTrainingRows] = fixedClassifier$pred$pred
if(any(!classificationTrainingRows)){
fixedClassPreds[!classificationTrainingRows] = predict(fixedClassifier,datClass[!classificationTrainingRows,])
}
rocFixed <- roc(response = datClass$survivorType,
levels = timelabs,
predictor = fixedClassPreds)###
print(rocFixed)
plot(rocFixed)
print("")
}
```
# CURRENTLY DISABLED:
# Next: build a cox model to predict survival (leave-one-out) and train only on the cases from the same predicted short/long survival category. We also train a linear model in the same way for comparison.
Cox model survival predictions use the median survival time from the estimated survival curve ( see references in ?survfit.coxph )
```{r Hierarchical Linear and cox predictions, echo=FALSE, warning=FALSE, message=FALSE}
#HACK disable
#TODO: review and reenable this code
if(FALSE){
#pseudo-code
# split data (LOOCV)
# for each short/mid/long build a linear model
# predict on last point
hierarchPreds <- numeric(nrow(dat))
coxPreds <- numeric(nrow(dat))
for (nn in 1:nrow(dat)) {
#from previous LOOCV
predictedSurvivorType <- bestClassifier$pred$pred
cols <- c(survival,setdiff(variableSelections$univariate,"predSurvivorType"))
trainingData <- subset(dat[-nn,],predictedSurvivorType[-nn] == predictedSurvivorType[nn],select=cols)
#fit model with data subset
linMod <- lm( as.formula(paste(survival,"~.")), data=trainingData)
hierarchPreds[nn] <- predict(linMod,dat[nn,])
#Hack: assume best model is linear on 5 predictors (multivariate cox)
#fit cox model for preds (use median survival)
fit <- coxph(survObj[-nn] ~ ., data=dat[-nn,setdiff(variableSelections$univariate,"predSurvivorType")])
coxPreds[nn] <- unname(summary(survfit(fit,newdata=dat[nn,variableSelections$univariate]))$table["median"])
}
print("Survival R(2) for linear model:")
print(cor(hierarchPreds,dat[[survival]])^2)
print("Survival R(2) for cox model Leave-one-out predictions")
print(cor(coxPreds,dat[[survival]])^2)
}
```
# Now, predict survival using all model/variable combinations. Default is leave-one-out cross validation and the predicted survivor type (short/mid/long) is used as an input feature for every model.
# Continuous predictions only use non-censored observations. The final model is applied to all non-censored observations
```{r Modeling (may take some time), echo=FALSE, warning=FALSE, message=FALSE}
#Modeling, dataparams are arguements to caret::train
# CONTINUOUS PREDICTION
#TODO: distinguish this from the categorical prediction
#@egates1 - need to weight data for class imbalance. weight training data by survival time ?
modelparams <- list(forest = list(method = "rf",
ntree = 500,
#tuneGrid = data.frame(.mtry = mtry),
#replace = TRUE,
#na.action = randomForest::na.roughfix,
importance = FALSE,
predict.all = FALSE
),
# xgboost = list(method = "xgbTree"),
linear = list(method="lm"),
# nnet = list(method = "nnet",
# #tuneGrid=data.frame(.size = 10, .decay = 0),
# #linout = TRUE,
# skip = TRUE,
# MaxNWts = 10000,
# trace = FALSE,
# maxit = 100)#,
#GLMNET bugged for DF data
# glmnet = list(method="glmnet")#,
# svm = list(method = "svmRadial")
gausspr = list(method="gaussprRadial")#,
)
modelformula <- as.formula(paste(params$survival,"~."))
#TODO: option to select RMSE vs Rsquared
dataparams <- list(form = modelformula,
# data = dat[,c(params$target,Filteredinputs)],
metric="Rsquared",
# weights = trainingWeights,
trControl=trainControl(allowParallel = T,
method = ifelse(params$leaveOneOut,"LOOCV", "repeatedcv"),
classProbs=FALSE,
returnResamp = "final",
savePredictions = "final",
index = regressionTrainingFolds,
#number = 10,
#repeats= 5,
verboseIter = F) # use method="none" to disable grid tuning for speed
)
caretparams <- lapply(modelparams,function(x) c(dataparams,x))
#fix alpha = 1 (only l1 regularization) for glmnet, lambda values taken from BraTS analysis
if ("glmnet" %in% names(caretparams)){
caretparams$glmnet[["tuneGrid"]] = data.frame(alpha=rep(1,3),lambda=2.594666*c(0.1,1,10))
}
#initialize outputs
models <- list()
acc <- list()
for(jjj in 1:length(variableSelections)){
if(! is.null(params$status) ){
modeldata <- dat[regressionTrainingRows, c(survival,variableSelections[[jjj]])]
weightList <- dat[regressionTrainingRows, trainingWeights]
} else {
modeldata <- dat[regressionTrainingRows, c(survival,variableSelections[[jjj]])]
weightList <- dat[regressionTrainingRows, trainingWeights]
}
# RE seeding: Hawthorn et al, "The design and analysis of benchmark experiments" (2005)
for (iii in 1:length(modelparams)){
model_name <-paste(names(variableSelections)[jjj], names(modelparams)[iii],sep="_")
print(model_name)
set.seed(3141) #seed before train to get same subsamples
models[[model_name]] <- do.call(caret::train, c(caretparams[[iii]], list(data=modeldata, weights=weightList)))
metric <- models[[model_name]]$metric
#get best acuracy manually for LOOCV
acc[[model_name]] <- max(models[[model_name]]$results[metric])
}
}
print(sprintf("%d rows used for regression modeling", nrow(modeldata)))
```
# Finding the best model for continuous predictions:
```{r Finding best model and plotting, echo = FALSE, fig.width=8, fig.height=10}
if(params$leaveOneOut){
maxacc <- max(unlist(acc),na.rm=T)
maxmodels <- names(which(acc==maxacc))
bestmod <- models[[maxmodels[[1]]]] #pick first by default
modpars <- bestmod$bestTune
results <- bestmod$pred
#plot
par(mar=c(8,5,1,1))
barplot(unlist(acc),las=2, ylim=c(0,1),
ylab=paste("training set LOOCV",metric))
## Redundant with other plots
# #pred vs obs
# plot(x=results$obs,y=results$pred, ylab="Predicted survival", xlab="Actual survival", main="non-censored only", xlim=c(0,max(dat[[survival]])), ylim=c(0,max(dat[[survival]]))
# )
} else {
rs <- resamples(models)
print(summary(object = rs))
acc <- rs$values[,grepl(metric, names(rs$values))]
names(acc) <- rs$models
maxacc <- max(apply(acc,2,mean))
maxmodels <- rs$models[apply(acc,2,mean)==maxacc]
bestmod <- models[[maxmodels[[1]]]] #pick first by default
# plot +1 to col arg keeps one box from being black
par(mar=c(8,5,1,1))
boxplot(acc,col=(as.numeric(as.factor(rs$methods))+1), las=2,
ylab=paste("training set cross-validation",metric) )
legend("bottomright", legend=unique(rs$methods),
fill=(as.numeric(as.factor(rs$methods))+1) )
}
```
# Best model(s): `r maxmodels`
`r sprintf("%s: %.4f", metric, maxacc)`
```{r Output model,echo = FALSE}
# Add predictions on censored data
fullPreds <- numeric(nrow(survObj))
fullPreds[regressionTrainingRows] = bestmod$pred$pred #LOOCV on non-censored
#make predictions on censored non-responders
fullPreds[!regressionTrainingRows] = predict(bestmod,dat[!regressionTrainingRows,])
print("C index (Concordance) for best model: 0.5 represents random prediction")
print(survConcordance(survObj ~ fullPreds))
print("Best model(s) and their inputs (ignore .outcome, it is not used in training)")
lapply(maxmodels, function(x) {print(x); print(models[[x]]); names(models[[x]]$trainingData)})
#TODO use fullPreds instead of results
#results is defined more than once
results <- data.frame(status = factor(dat[,status]),
pred = fullPreds,
obs = dat[,survival],
training_pred = predict(bestmod,newdata=dat),
training_err = predict(bestmod,newdata=dat) - dat[,survival],
testing_err = fullPreds - dat[,survival]
)
if(!is.null(trainingWeights)){
results[,'weights'] <- dat[,trainingWeights]
results$weightedTestingResidual <- results$weights*abs(results$testing_err)
results$weightedTrainingResidual <- results$weights*abs(results$training_err)
}
#predict function uses final model by default
#compare training, testing errors
#results$training_pred <- predict(bestmod,newdata=bestmod$trainingData[results$rowIndex,])
#results$training_err <- results$training_pred - results$obs
#results$testing_err <- results$pred - results$obs
p1 <- ggplot(data=results) +
geom_point(aes(x=obs,y=pred, col=status)) +
scale_color_manual(values=c("red","black")) +
ylab("Predicted (test set)") +
xlab("Observed") +
theme_light(base_size=12)
p2 <- ggplot(data=results) +
geom_point(aes(x=obs,y=training_pred, col=status)) +
ylab("Predicted (training set)") +
scale_color_manual(values=c("red","black")) +
xlab("Observed") +
theme_light(base_size=12)
p3 <- ggplot(data=results) +
geom_point(aes(x=pred,y=training_pred,col=obs)) +
labs(color="Actual") +
scale_color_gradientn(colours=rainbow(5)) +
ylab("Predicted (testing set)") +
xlab("Predicted (training set)") +