forked from kouroshAKiani/ML-GeneticPathotyping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiutA.R
946 lines (701 loc) · 43 KB
/
iutA.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
source('F1-score.R') # Call F1-Score function for computing point estimation and its 95% confidence interval
source('compute_metrics.R') # Call compute_metrics function for computing all classification metrics and their confidence interval.
source('Packages_installation.R')
# Parse command argumnets
parser <- OptionParser(option_list = list(
make_option(c("-f", "--file_path"), type="character", help="The file path of the data file."),
make_option(c("-t", "--task"), type="character", help="Specifying task"),
make_option(c("-o", "--model"), type="character", help="Specifying an ML model to train and evaluate"),
make_option(c("-m", "--method"), type="character", help="Specifying feature selection method"),
make_option(c("-v", "--verbose"), action="store_true", default=FALSE, help="Print detailed information")
))
args = parse_args(parser)
file_path = args$file_path
task = args$task
model = args$model
method = args$method
verbose = args$verbose
# Read data from the file
Bio_Data <- read_xlsx(file_path, sheet = "Sheet1")
# Define conversion mappings
## Genes : 1 = Gene Presence 0 = Gene Absence
## Antibiotics : 1 = isolate's Resistance 0 = isolate's Susceptiblity
# Convert categorical variables to factors
categorical_columns <- colnames(Bio_Data)
Bio_Data[, categorical_columns[-1]] <- lapply(Bio_Data[, categorical_columns[-1]], factor)
# Add a new level for Ampicillin variable
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin), "0")
# Display summary and dimensions of the data
cat("Summary:\n")
summary(Bio_Data)
cat("Dimensions:", dim(Bio_Data), "\n")
# Calculate the proportion of IUT_gene
proportion_IUT <- table(Bio_Data$IUT) %>% prop.table()
cat("Proportion of IUT gene:\n")
print(proportion_IUT)
# Response variable is imbalanced (0 : 72% , 1 : 28%) so we're using stratified sampling
# Divide the data set into train and test sets through stratified sampling method
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
# Display the dimensions of the train and test sets
cat("train set dimensions:", dim(train_strat_IUT), "\n")
cat("test set dimensions:", dim(test_strat_IUT), "\n")
if (!is.null(task)) {
if (task == "model_evaluation") {
# Task: Model Evaluation
# You can add flags for specific model training and evaluation to use, e.g., -o LR-Chi Squared test selects LR algorithm based on the
# selected antibiotics through Chi Squared test method
if (model == "LR-Chi Squared test"){
# Logistic regression (Model 1)--------------------------------------------------
model_Log1_IUT <- glm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, family = "binomial", data = train_strat_IUT)
cat("Summary of LR model based on Chi Squared test including Null deviance, Residual deviance, and AIC, etc.:\n")
print(summary(model_Log1_IUT))
test_strat_IUT$probs1_IUT<- predict(model_Log1_IUT, test_strat_IUT, type = "response")
test_strat_IUT$pred_logreg1_IUT <- ifelse(test_strat_IUT$probs1_IUT >= 0.5, 1, 0)
# Model 1 evaluation
confm_logreg1_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_logreg1_IUT)
metrics_logreg1_IUT <- compute_metrics(confm_logreg1_IUT)
cat("Performance of LR model based on Chi-Squared test results:\n")
metrics_logreg1_IUT
}
else if (model == "LR-model agnostic"){
model_Log2_IUT <- glm(IUT ~ Meropenem
, family = "binomial", data = train_strat_IUT)
cat("Summary of LR model based on the model-agnostic approach including Null deviance, Residual deviance, and AIC, etc.:\n")
print(summary(model_Log2_IUT))
##Prediction on test_strat_IUT (Model 2)--------------------------------------------------
test_strat_IUT$probs2_IUT <- predict(model_Log2_IUT, test_strat_IUT, type = "response")
test_strat_IUT$pred_logreg2_IUT <- ifelse(test_strat_IUT$probs2_IUT >= 0.5, 1, 0)
# Model 2 evaluation
confm_logreg2_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_logreg2_IUT)
metrics_logreg2_IUT <- compute_metrics(confm_logreg2_IUT)
cat("Performance of LR model based on the model-agnostic approach:\n")
metrics_logreg2_IUT
}
else if (model == "LR-Wald test"){
model_Log3_IUT <- glm(IUT ~ Imipenem + Meropenem + Aztreonam + Ceftazidime + Cefepime
, family = "binomial", data = train_strat_IUT)
cat("Summary of LR model based on the Wald test including Null deviance, Residual deviance, and AIC, etc.:\n")
print(summary(model_Log3_IUT))
##Prediction on test_strat_IUT
test_strat_IUT$probs3_IUT <- predict(model_Log3_IUT, test_strat_IUT, type = "response")
test_strat_IUT$pred_logreg3_IUT <- ifelse(test_strat_IUT$probs3_IUT >= 0.5, 1, 0)
confm_logreg3_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_logreg3_IUT)
metrics_logreg3_IUT <- compute_metrics(confm_logreg3_IUT)
cat("Performance of LR model based on the Wald test:\n")
metrics_logreg3_IUT
}
else if (model == "NBC-Chi Squared test"){
train_control <- trainControl(
method = "cv",
number = 10
)
search_grid_IUT <- expand.grid(
usekernel = c(FALSE,TRUE),
fL = seq(0,5,0.5),
adjust = seq(0,5,0.5)
)
model_nbdalex_IUT <- train(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone,
data = train_strat_IUT,
method = "nb",
metric = "Accuracy",
trControl = train_control,
tuneGrid = search_grid_IUT
)
cat("Tuned hyper-parameters through 10-fold Cross-Validation:\n")
print(model_nbdalex_IUT$bestTune)
##Prediction on test_strat_IUT (Naive Bayes Model)----------------------------------
test_strat_IUT$pred_nb_IUT <- predict(model_nbdalex_IUT, test_strat_IUT)
# Model evaluation
confm_nb_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_nb_IUT)
metrics_nb_IUT<- compute_metrics(confm_nb_IUT)
cat("Performance of NBC model based on Chi-Squared test results:\n")
metrics_nb_IUT
}
else if (model == "NBC-model agnostic"){
train_control <- trainControl(
method = "cv",
number = 10
)
search_grid_IUT <- expand.grid(
usekernel = c(FALSE,TRUE),
fL = seq(0,5,0.5),
adjust = seq(0,5,0.5)
)
model_nbdalex1_IUT <- train(IUT ~ Meropenem,
data = train_strat_IUT,
method = "nb",
metric = "Accuracy",
trControl = train_control,
tuneGrid = search_grid_IUT,
)
cat("Tuned hyper-parameters through 10-fold Cross-Validation:\n")
print(model_nbdalex1_IUT$bestTune)
##Prediction on test_strat_IUT (Naive Bayes Model)----------------------------------
test_strat_IUT$pred_nb1_IUT <- predict(model_nbdalex1_IUT, test_strat_IUT)
# Model evaluation
confm_nb1_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_nb1_IUT)
metrics_nb1_IUT<- compute_metrics(confm_nb1_IUT)
cat("Performance of NBC model based on model-agnostic approach results:\n")
metrics_nb1_IUT
}
else if (model == "NBC-Wald test"){
train_control <- trainControl(
method = "cv",
number = 10
)
search_grid_IUT <- expand.grid(
usekernel = c(FALSE,TRUE),
fL = seq(0,5,0.5),
adjust = seq(0,5,0.5)
)
model_nb2_IUT <- train( IUT ~ Meropenem + Aztreonam + Ceftazidime + Cefepime + Imipenem,
data = train_strat_IUT,
method = "nb",
metric = "Accuracy",
trControl = train_control,
tuneGrid = search_grid_IUT,
)
cat("Tuned hyper-parameters through 10-fold Cross-Validation:\n")
print(model_nb2_IUT$bestTune)
##Prediction on test_strat_IUT (Naive Bayes Model)
test_strat_IUT$pred_nb2_IUT <- predict(model_nb2_IUT, test_strat_IUT)
##confusion matrix(Naive Bayes Model)
confm_nb2_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_nb2_IUT)
metrics_nb2_IUT<- compute_metrics(confm_nb2_IUT)
cat("Performance of NBC model based on the Wald test results:\n")
metrics_nb2_IUT
}
else if (model == "LDA-Chi Squared test"){
model_ldadalex_IUT <- train(IUT ~ Cefotaxime + Gentamicin + Imipenem +
Cefepime + Ceftazidime + Ciprofloxacin +
Meropenem + Ceftriaxone + Aztreonam,
data = train_strat_IUT,
method = "lda",
metric = "Accuracy")
cat("LDA model based on Chi-Squared test results:\n")
print(model_ldadalex_IUT)
#Prediction on test_strat_IUT (LDA Model)-----------------------------------------------
test_strat_IUT$pred_lda_IUT <- predict(model_ldadalex_IUT, test_strat_IUT)
# Model Evaluation-------------------------------------------------
confm_lda_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_lda_IUT)
metrics_lda_IUT<- compute_metrics(confm_lda_IUT)
cat("Performance of LDA model based on Chi-Squared test results:\n")
metrics_lda_IUT
}
else if (model == "LDA-model agnostic"){
model_ldadalex1_IUT <- train(IUT ~ Meropenem,
data = train_strat_IUT,
method = "lda",
metric = "Accuracy")
cat("LDA model based on model-agnostic approach:\n")
print(model_ldadalex1_IUT)
#Prediction on test_strat_IUT (LDA Model)-----------------------------------------------
test_strat_IUT$pred_lda1_IUT <- predict(model_ldadalex1_IUT, test_strat_IUT)
# Model evaluation
confm_lda1_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_lda1_IUT)
metrics_lda1_IUT <- compute_metrics(confm_lda1_IUT)
cat("Performance of LDA model based on model-agnostic approach:\n")
metrics_lda1_IUT
}
else if (model == "LDA-Wald test"){
model_lda2_IUT <- train(IUT ~ Imipenem + Meropenem + Aztreonam + Ceftazidime + Cefepime,
data = train_strat_IUT,
method = "lda",
metric = "Accuracy")
cat("LDA model based on the Wald test results:\n")
print(model_lda2_IUT)
test_strat_IUT$pred_lda2_IUT <- predict(model_lda2_IUT, test_strat_IUT)
confm_lda2_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_lda2_IUT)
metrics_lda2_IUT<- compute_metrics(confm_lda2_IUT)
cat("Performance of LDA model based on the Wald test results:\n")
metrics_lda2_IUT
}
else if (model == "SVM-Sig-Chi Squared test"){
# Fit Support Vector Machine model to train data set
# This code performs a grid search for tuning the hyper-parameters of the SVM model using the tune function
# from the e1071 package. It then fits the SVM model with the specified hyper-parameters using the svm function.
# Model 1
set.seed(1234)
tune_out2_IUT <- e1071::tune("svm", IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, kernel = "sigmoid", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 10, length = 30)
, gamma = seq(0.1,1, length = 10)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out_IUT))
model_svm2_IUT = svm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone , data = train_strat_IUT,
probability = T, kernel = "sigmoid", cost = tune_out2_IUT$best.parameters$cost,
gamma = tune_out2_IUT$best.parameters$gamma )
cat("Sigmoid SVM based on Chi-Squared test results:\n")
print(model_svm2_IUT)
#Prediction on test_strat_IUT (SVM)----------------------------------------------------
test_strat_IUT$pred_svm_IUT <- predict(model_svm2_IUT, test_strat_IUT)
# Model evaluation
confm_svm_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm_IUT)
metrics_svm1_IUT_sig<- compute_metrics(confm_svm_IUT)
cat("Performance of SVM-Sigmoid model based on Chi-Squared test results:\n")
metrics_svm1_IUT_sig
}
else if (model=="SVM-Sig-model agnostic"){
set.seed(1234)
tune_out2_IUT <- e1071::tune("svm", IUT ~ Meropenem,
data = train_strat_IUT,
kernel = "sigmoid", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 30)
, gamma = seq(0, 5, length = 25)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out2_IUT))
model_svm21_IUT = svm(IUT ~ Meropenem, data = train_strat_IUT,
probability = T, kernel = "sigmoid", cost = tune_out2_IUT$best.parameters$cost,
gamma = tune_out2_IUT$best.parameters$gamma)
cat("Sigmoid SVM based on model-agnostic approach:\n")
print(model_svm21_IUT)
test_strat_IUT$pred_svm2_IUT <- predict(model_svm21_IUT, test_strat_IUT)
confm_svm2_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm2_IUT)
metrics_svm2_IUT_sig<- compute_metrics(confm_svm2_IUT)
cat("Performance of SVM-Sigmoid model based on model-agnostic approach:\n")
metrics_svm2_IUT_sig
}
else if (model=="SVM-Wald test"){
set.seed(1234)
tune_out3_IUT <- e1071::tune("svm", IUT ~ Imipenem + Meropenem + Aztreonam + Ceftazidime + Cefepime
, data = train_strat_IUT, kernel = "sigmoid", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 25)
, gamma = seq(0.1, 1, length = 20)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out3_IUT))
model_svm3_IUT = svm(IUT ~ Imipenem + Meropenem + Aztreonam + Ceftazidime + Cefepime, data = train_strat_IUT,
probability = T, kernel = "sigmoid", cost = tune_out3_IUT$best.parameters$cost,
gamma = tune_out3_IUT$best.parameters$gamma )
cat("Sigmoid SVM based on the Wald test:\n")
print(model_svm3_IUT)
#Prediction on test_strat_IUT (SVM)
test_strat_IUT$pred_svm3_IUT <- predict(model_svm3_IUT, test_strat_IUT)
# Model evaluation
confm_svm3_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm3_IUT)
metrics_svm3_IUT_sig<- compute_metrics(confm_svm3_IUT)
cat("Performance of SVM-Sigmoid model based on the Wald test results:\n")
metrics_svm3_IUT_sig
}
else if (model=="SVM-Poly-Chi Squared test"){
set.seed(1234)
tune_out4_IUT <- e1071::tune("svm", IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, kernel = "polynomial", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 20), degree = seq(1,4,1)
, gamma = seq(0.1,5, length = 20)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out4_IUT))
# Tuned hyper-parameters are placed in the model.
model_svm4_IUT = svm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, probability = T,
kernel = "polynomial", cost = tune_out4_IUT$best.parameters$cost,
degree = tune_out4_IUT$best.parameters$degree , gamma = tune_out4_IUT$best.parameters$gamma )
cat("Polynomial SVM based on Chi-Squared test results:\n")
print(model_svm4_IUT)
#Prediction on test (SVM 2)----------------------------------------------------
test_strat_IUT$pred_svm4_IUT <- predict(model_svm4_IUT, test_strat_IUT)
# Model evaluation
confm_svm4_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm4_IUT)
metrics_svm_IUT<- compute_metrics(confm_svm4_IUT)
cat("Performance of SVM-Polynomial model based on Chi-Squared test results:\n")
metrics_svm_IUT
}
else if (model=="SVM-Poly-model agnostic"){
set.seed(1234)
tune_out41_IUT <- e1071::tune("svm", IUT ~ Meropenem ,
data = train_strat_IUT,
kernel = "polynomial", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 20), degree = seq(1,3,1)
, gamma = seq(0, 5, length = 20)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out41_IUT))
model_svm41_IUT = svm(IUT ~ Meropenem, data = train_strat_IUT,
probability = T, kernel = "polynomial", cost = tune_out41_IUT$best.parameters$cost,
degree = tune_out41_IUT$best.parameters$degree , gamma = tune_out41_IUT$best.parameters$gamma )
cat("Polynomial SVM based on model-agnostic approach:\n")
print(model_svm41_IUT)
#Prediction on test_strat_IUT (SVM)----------------------------------------------------
test_strat_IUT$pred_svm41_IUT <- predict(model_svm41_IUT, test_strat_IUT)
# Model evaluation
confm_svm41_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm41_IUT)
metrics_svm41_IUT<- compute_metrics(confm_svm41_IUT)
cat("Performance of SVM-Polynomial model based on model-agnostic approach:\n")
metrics_svm41_IUT
}
else if (model=="SVM-Poly-Wald test"){
set.seed(1234)
tune_out5_IUT <- e1071::tune("svm", IUT ~ Meropenem + Ceftazidime + Cefepime +
Aztreonam + Imipenem,
data = train_strat_IUT,
kernel = "polynomial", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 20), degree = seq(1,3,1)
, gamma = seq(0, 5, length = 20)))
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(summary(tune_out5_IUT))
model_svm5_IUT = svm(IUT ~ Meropenem + Ceftazidime + Cefepime +
Aztreonam + Imipenem , data = train_strat_IUT,
probability = T, kernel = "polynomial", cost = tune_out5_IUT$best.parameters$cost,
degree = tune_out5_IUT$best.parameters$degree , gamma = tune_out5_IUT$best.parameters$gamma )
cat("Polynomial SVM based on the Wald test:\n")
print(model_svm5_IUT)
#Prediction on test_strat_IUT (SVM)----------------------------------------------------
test_strat_IUT$pred_svm5_IUT <- predict(model_svm5_IUT, test_strat_IUT)
# Model evaluation
confm_svm5_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$pred_svm5_IUT)
metrics_svm3_IUT<- compute_metrics(confm_svm5_IUT)
cat("Performance of SVM-Polynomial model based on the Wald test:\n")
metrics_svm3_IUT
}
else if (model=="CatBoost-Chi Squared test"){
# CatBoost Models
Bio_Data <- as.data.frame(Bio_Data)
#Convert categorical variables to factor
categorical_var <- c(colnames(Bio_Data))
Bio_Data[, categorical_var[-1]] <- lapply(Bio_Data[, categorical_var[-1]], factor)
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin),0)
#Divide Data set into train and test
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
# Training CatBoost model based on the selected antibiotics through chi squared test
grid_IUT <- expand.grid(
depth = c(2,3,4,5),
learning_rate = c(0.1,0.2,0.3),
l2_leaf_reg = c(1,2,3),
rsm = 1,
border_count = 1,
iterations = seq(10,150,10)
)
fitControl_IUT <- trainControl(method = "cv",
number = 10,
classProbs = TRUE)
set.seed(1234)
model_catboost_IUT <- train(x = train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin", "Meropenem", "Ceftriaxone", "Aztreonam")],
y = make.names(train_strat_IUT[,"IUT"]),
maximize = TRUE,
method = catboost.caret, metric = "Accuracy",
tuneGrid = grid_IUT,
trControl = fitControl_IUT)
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(model_catboost_IUT$bestTune)
#Prediction on test_strat_IUT (CatBoost)----------------------------------------------------
test_strat_IUT$cat_IUT = predict(model_catboost_IUT, test_strat_IUT)
# Model evaluation
cat_test_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$cat_IUT)
metrics_cat_IUT<- compute_metrics(cat_test_IUT)
cat("Performance of CatBoost model based on Chi-Squared test results:\n")
metrics_cat_IUT
}
else if (model=="CatBoost-model agnostic"){
# CatBoost Models
Bio_Data <- as.data.frame(Bio_Data)
#Convert categorical variables to factor
categorical_var <- c(colnames(Bio_Data))
Bio_Data[, categorical_var[-1]] <- lapply(Bio_Data[, categorical_var[-1]], factor)
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin),0)
#Divide Data set into train and test
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
grid_IUT <- expand.grid(
depth = c(2,3,4),
learning_rate = c(0.2,0.3,0.4),
l2_leaf_reg = c(2,3,4),
rsm = 1,
border_count = 1,
iterations = seq(10,35,1)
)
fitControl_IUT <- trainControl(method = "cv",
number = 10,
classProbs = TRUE)
model_catboost1_IUT <- train(x = train_strat_IUT[,c("Imipenem", "Meropenem", "Aztreonam")],
y = make.names(train_strat_IUT[,"IUT"]),
maximize = TRUE,
method = catboost.caret, metric = "Accuracy",
tuneGrid = grid_IUT,
trControl = fitControl_IUT)
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(model_catboost1_IUT$bestTune)
test_strat_IUT$cat_IUT1 = predict(model_catboost1_IUT, test_strat_IUT)
# Model Evaluation
confm_cat_IUT1 <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$cat_IUT1)
metrics_cat1_IUT<- compute_metrics(confm_cat_IUT1)
cat("Performance of CatBoost model based on model-agnostic approach results:\n")
metrics_cat1_IUT
}
else if (model=="CatBoost-Wald test"){
# CatBoost Models
Bio_Data <- as.data.frame(Bio_Data)
#Convert categorical variables to factor
categorical_var <- c(colnames(Bio_Data))
Bio_Data[, categorical_var[-1]] <- lapply(Bio_Data[, categorical_var[-1]], factor)
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin),0)
#Divide Data set into train and test
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
grid2_IUT <- expand.grid(
depth = c(2,3,4,5,6),
learning_rate = c(0.1,0.2,0.3,0.4,0.5),
l2_leaf_reg = c(1,2,3,4,5),
rsm = 1,
border_count = 1,
iterations = seq(10,30,1)
)
fitControl2_IUT <- trainControl(method = "cv",
number = 10,
classProbs = TRUE)
model_catboost2_IUT <- train(x = train_strat_IUT[,c("Aztreonam","Imipenem","Meropenem","Ceftazidime","Cefepime")],
y = make.names(train_strat_IUT[["IUT"]]),
maximize = TRUE,
method = catboost.caret, metric = "Accuracy",
tuneGrid = grid2_IUT,
trControl = fitControl2_IUT)
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(model_catboost2_IUT$bestTune)
test_strat_IUT$cat_IUT2 = predict(model_catboost2_IUT, test_strat_IUT)
# Model evaluation
confm_cat2_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$cat_IUT2)
metrics_cat3_IUT<- compute_metrics(confm_cat2_IUT)
cat("Performance of CatBoost model based on the Wald test results:\n")
metrics_cat3_IUT
}
else if (model=="CatBoost-Simplicity Principle_MEM"){
# CatBoost Models
Bio_Data <- as.data.frame(Bio_Data)
#Convert categorical variables to factor
categorical_var <- c(colnames(Bio_Data))
Bio_Data[, categorical_var[-1]] <- lapply(Bio_Data[, categorical_var[-1]], factor)
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin),0)
#Divide Data set into train and test
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
grid3_IUT <- expand.grid(
depth = c(2,3,4),
learning_rate = c(0.1,0.2),
l2_leaf_reg = c(1,2,3),
rsm = 1,
border_count = 1,
iterations = seq(10,30,1)
)
fitControl3_IUT <- trainControl(method = "cv",
number = 10,
classProbs = TRUE)
model_catboost3_IUT <- train(x = train_strat_IUT["Meropenem"],
y = make.names(train_strat_IUT[["IUT"]]),
maximize = TRUE,
method = catboost.caret, metric = "Accuracy",
tuneGrid = grid3_IUT,
trControl = fitControl3_IUT)
cat("Tuned hyper-parameters through 10-Fold Cross Validation:\n")
print(model_catboost3_IUT$bestTune)
test_strat_IUT$cat_IUT3 = predict(model_catboost3_IUT, test_strat_IUT)
# Model evaluation
confm_cat3_IUT <- table(actual = test_strat_IUT$IUT, prediction = test_strat_IUT$cat_IUT3)
metrics_cat4_IUT<- compute_metrics(confm_cat3_IUT)
cat("Performance of CatBoost model based on the Simplicity Principle (MEM):\n")
metrics_cat4_IUT
}
}
else if (task == 'feature_selectio') {
# Task: Feature Selection
# You can add flags for specific feature selection methods to use, e.g., -m "Chi-Squared test" or -m "Wald test".
# In order to investigate results of feature selection methods, you should specify "task" to "feature_selection"
# then you can choose a specific method by -m, e.g., if you want to see results of model-agnostic approach in LR
# algorithm, use -t "feature_selection" -m "model-agnostic-LR"
if (method == "Chi-Squared test"){
# Function to perform chi-square test and print p-value
perform_chi_square <- function(data, variable) {
result <- chisq.test(table(data[[variable]], data$IUT))
cat("p-value for", variable, ":", result$p.value, "\n")
}
# Perform chi-square test for multiple variables
variables <- c("Amikacin", "Cefotaxime", "Gentamicin", "Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin", "Meropenem", "Ampicillin",
"Ceftriaxone", "Aztreonam")
for (variable in variables) {
perform_chi_square(Bio_Data, variable)
}
}
}
else if (method == "Wald test"){
model_Log1_IUT <- glm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, family = "binomial", data = train_strat_IUT)
cat("P-values show Wald test results:\n")
print(summary(model_Log1_IUT))
}
else if (method == "model agnostic-LR"){
# Feature Selection by DALEX Package--------------------------------------
# the 'explained_glm_TEM' object is created using the 'explain' function from the DALEX package.
# The variable_importance function is used to calculate the permutation-based
# feature importance ('fi_glm_TEM') with 50 permutations. Finally, the feature importance is displayed
# using 'fi_glm_TEM' and plotted using' plot(fi_glm_TEM)'.
model_Log1_IUT <- glm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, family = "binomial", data = train_strat_IUT)
explained_glm_IUT <- explain(model = model_Log1_IUT, data=train_strat_IUT[,c(3:9,11:12)], variables = colnames(train_strat_IUT[,c(3:9,11:12)]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "LR",
type = "classification")
#50 permuatation
fi_glm_IUT = variable_importance(explained_glm_IUT, B=50 ,variables = colnames(train_strat_IUT[,c(3:9,11:12)]), loss_function = loss_root_mean_square, type = "raw")
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in LR:\n")
print(fi_glm_IUT)
}
else if (method == "model agnostic-NBC"){
train_control <- trainControl(
method = "cv",
number = 10
)
search_grid_IUT <- expand.grid(
usekernel = c(FALSE,TRUE),
fL = seq(0,5,0.5),
adjust = seq(0,5,0.5)
)
model_nbdalex_IUT <- train(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone,
data = train_strat_IUT,
method = "nb",
metric = "Accuracy",
trControl = train_control,
tuneGrid = search_grid_IUT
)
# Feature selection through model-agnostic approach (DALEX)
# the 'explain' function from the DALEX package is used to explain the Naive Bayes classifier model
# 'model_nbdalex_TEM'. The relevant variables are selected and provided as data along with their
# corresponding column names. The target variable 'IUT_gene' is transformed to numeric values
# (y = as.vector(as.numeric(train_strat_IUT$IUT_gene)) - 1). The label is set to "NBC" for Naive Bayes classifier,
# and the type is set to "classification".
expnb_IUT = explain(model = model_nbdalex_IUT, data=train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")], variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "NBC",
type = "classification")
# The 'variable_importance' function is then used to calculate the variable importance based on the
# explained model. The 'B' parameter is set to 50 for the number of permutations, and the loss function
# is set to "loss_root_mean_square". The resulting variable importance is stored in 'fitnb_TEM'
fitnb_IUT = variable_importance(expnb_IUT, B=50 ,variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]), loss_function = loss_root_mean_square, type = "raw" )
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in NBC:\n")
print(fitnb_IUT)
}
else if (method == "model agnostic-LDA"){
model_ldadalex_IUT <- train(IUT ~ Cefotaxime + Gentamicin + Imipenem +
Cefepime + Ceftazidime + Ciprofloxacin +
Meropenem + Ceftriaxone + Aztreonam,
data = train_strat_IUT,
method = "lda",
metric = "Accuracy")
explda_IUT = explain(model = model_ldadalex_IUT, data=train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")], variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "LDA",
type = "classification")
fitlda_IUT = variable_importance(explda_IUT, B=50 ,variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]), loss_function = loss_root_mean_square, type = "raw" )
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in LDA:\n")
print(fitlda_IUT)
}
else if (method == "model agnostic-Sig SVM"){
set.seed(1234)
tune_out2_IUT <- e1071::tune("svm", IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, kernel = "sigmoid", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 10, length = 30)
, gamma = seq(0.1,1, length = 10)))
model_svm2_IUT = svm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem + Aztreonam +
Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone, data = train_strat_IUT,
probability = T, kernel = "sigmoid", cost = tune_out2_IUT$best.parameters$cost,
gamma = tune_out2_IUT$best.parameters$gamma )
explained_SVM2_IUT <- explain(model = model_svm2_IUT, data=train_strat_IUT[,c(3:9,11:12)], variables = colnames(train_strat_IUT[,c(3:9,11:12)]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "SVM-Sigmoid",
type = "classification")
#50 permuatation
fi_SVM2_IUT = variable_importance(explained_SVM2_IUT, B=50 ,variables = colnames(train_strat_IUT[,c(3:9,11:12)]),loss_function = loss_root_mean_square, type = "raw")
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in sigmoid kernel of SVM:\n")
print(fi_SVM2_IUT)
}
else if (method == "model agnostic-Poly SVM"){
set.seed(1234)
tune_out4_IUT <- e1071::tune("svm", IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, kernel = "polynomial", tunecontrol=tune.control(cross=10),
ranges = list(cost = seq(0.1, 5, length = 20), degree = seq(1,4,1)
, gamma = seq(0.1,5, length = 20)))
# Tuned hyper-parameters are placed in the model.
model_svm4_IUT = svm(IUT ~ Imipenem + Gentamicin + Ciprofloxacin + Meropenem +
Aztreonam + Cefotaxime + Cefepime + Ceftazidime + Ceftriaxone
, data = train_strat_IUT, probability = T,
kernel = "polynomial", cost = tune_out4_IUT$best.parameters$cost,
degree = tune_out4_IUT$best.parameters$degree , gamma = tune_out4_IUT$best.parameters$gamma )
explained_SVM4_IUT <- explain(model = model_svm4_IUT, data=train_strat_IUT[,c(3:9,11:12)], variables = colnames(train_strat_IUT[,c(3:9,11:12)]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "SVM-Polynomial",
type = "classification")
#50 permuatation
fi_SVM4_IUT = variable_importance(explained_SVM4_IUT, B=50 ,variables = colnames(train_strat_IUT[,c(3:9,11:12)]),loss_function = loss_root_mean_square, type = "raw")
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in polynomial kernel of SVM:\n")
print(fi_SVM4_IUT)
}
else if (method == "model agnostic-CatBoost"){
# CatBoost Models
Bio_Data <- as.data.frame(Bio_Data)
#Convert categorical variables to factor
categorical_var <- c(colnames(Bio_Data))
Bio_Data[, categorical_var[-1]] <- lapply(Bio_Data[, categorical_var[-1]], factor)
levels(Bio_Data$Ampicillin) <- c(levels(Bio_Data$Ampicillin),0)
#Divide Data set into train and test
set.seed(6546)
split_strat_IUT <- initial_split(Bio_Data, prop = 0.8, strata = "IUT")
train_strat_IUT <- training(split_strat_IUT)
test_strat_IUT <- testing(split_strat_IUT)
grid_IUT <- expand.grid(
depth = c(3,4,5),
learning_rate = c(0.1,0.2,0.3),
l2_leaf_reg = c(1,2),
rsm = 1,
border_count = 1,
iterations = seq(10,61,10)
)
fitControl_IUT <- trainControl(method = "cv",
number = 10,
classProbs = TRUE)
set.seed(1234)
model_catboost_IUT <- train(x = train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin", "Meropenem", "Ceftriaxone", "Aztreonam")],
y = make.names(train_strat_IUT[,"IUT"]),
maximize = TRUE,
method = catboost.caret, metric = "Accuracy",
tuneGrid = grid_IUT,
trControl = fitControl_IUT)
expsda_IUT = explain(model = model_catboost_IUT, data=train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")], variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]),
y=as.vector(as.numeric(train_strat_IUT$IUT))-1, label = "CatBoost",
type = "classification")
fitcat_IUT = variable_importance(expsda_IUT, B=50 ,variables = colnames(train_strat_IUT[,c("Cefotaxime", "Gentamicin","Imipenem", "Cefepime",
"Ceftazidime", "Ciprofloxacin","Meropenem", "Ceftriaxone", "Aztreonam")]), loss_function = loss_root_mean_square, type = "raw" )
cat("Root mean squared dropout loss due to a feature elimination through model-agnostic approach in CatBoost:\n")
print(fitcat_IUT)
}
else {
cat("Invalid task. Please specify one of the following tasks: feature_selectio, model_evaluation\n")
}
}
if (is.null(task)){
cat("You should specify a task\n")
}