-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModelling script.Rmd
887 lines (512 loc) · 28.2 KB
/
Modelling script.Rmd
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
---
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
tidy = 'styler',
comment = NA,
message = FALSE,
warning = FALSE)
```
This RMarkdown script is designed to run chunk by chunk. You can press `ctrl+shift+Enter` to run each chunk.
Example of code chunk
```{r chunk_name}
2+2
# Run me with ctrl+shift+Enter
```
This program will download from the internet and install the latest version of the packages below if they are not already installed in your R environment. It is necessary to have internet connection to download those packages.
If for any reason this program fails to run, please make sure that the packages are installed.
```{r R_library, include=TRUE}
#Import packages
data_exploration_packages <- c("tidyverse", "readxl", "writexl", "inspectdf")
machine_learning_packages <- c("caret", "car", "kernlab", "rpart", "randomForest", "class", "ada", "rda", "e1071", "nnet", "ipred", "dbarts", "klaR", "glmnet", "earth")
table_formating_packages <- c("knitr", "kableExtra")
if (!require(install.load)) {
install.packages("install.load")
}
install_load(c(data_exploration_packages, machine_learning_packages, table_formating_packages))
```
Access to the dataset can be found from the chunck below:
```{r Cleveland heart disease dataset}
browseURL("https://archive.ics.uci.edu/ml/datasets/Heart+Disease")
```
```{r Statlog heart disease dataset}
browseURL("http://archive.ics.uci.edu/ml/datasets/statlog+(heart)")
```
# Importing and preprocessing of Cleveland dataset
```{r Cleveland dataset}
# We recoded some levels of categorical features in MS-Excel for both Cleveland and Statlog heart disease database so that they can match each other
cleveland_data <- read_xlsx('data/heart_disease_database.xlsx', sheet = 1) %>% mutate(restecg = as.character(restecg))
```
```{r data_processing1}
# Data wrangling for Cleveland dataset
cleveland_data <- cleveland_data %>%
na_if("?") # replacing '?' to NA
# count missing value
cleveland_data %>% inspect_na() %>% show_plot()
# drop rows with NA
cleveland_data <- cleveland_data %>% drop_na()
dim(cleveland_data)
```
Experiments with the Cleveland database have concentrated on simply attempting to distinguish presence (values 1,2,3,4) from absence (value 0) in the predicted attribute (num)
So we are recoding label to (1-4)- present and (0)- absent.
```{r data_processing2}
cleveland_data <-
cleveland_data %>%
mutate(heart_disease_present= factor(ifelse(num == 0, 'absent', 'present')))
# removing variable num which was the label with values 1:4
cleveland_data <- cleveland_data %>% dplyr::select(-num)
```
We changed the data type in Cleveland data to the right type
```{r data_processing3, include = FALSE}
cleveland_data <-
cleveland_data %>%
mutate(across(!where(is_double) , as_factor))
```
`vt` function This function will output a descriptive variable table that can be viewed continuously while working with data
```{r vtable}
label <- data.frame(age = "age in years",
sex = "sex of the patients",
cp = "chest pain type",
trestbps = "resting blood pressure (in mm Hg on admission to the hospital)",
chol= "serum cholestoral in mg/dl",
fbs = "fasting blood sugar > 120 mg/dl",
restecg = "resting electrocardiographic results",
thalach = "maximum heart rate achieved (beats per minute)",
exang = "exercise induced angina",
oldpeak = "ST depression induced by exercise relative to rest, a measure of abnormality in electrocardiograms",
slope = "the slope of the peak exercise ST segment",
ca = "number of major vessels colored by flourosopy",
thal = "results of thallium stress test measuring blood flow to the heart")
vtable::vt(cleveland_data, labels = label, factor.limit = 0)
```
```{r Scaling_1}
# Scaling the continuous variables
preProcess_scale_model <- preProcess(cleveland_data, method = c("center", "scale"))
cleveland_data_scaling <- predict(preProcess_scale_model, cleveland_data)
```
```{r One_hot_encoding_1}
# Removing the class column on train data to be able to create a one hot encoding
cleveland_data_one_hot <- cleveland_data_scaling[-length(cleveland_data_scaling)]
# `fullRank = T` to avoid dummmy trap
dummies <- dummyVars("~.", data = cleveland_data_one_hot, fullRank = TRUE)
cleveland_data_one_hot <- predict(dummies, newdata = cleveland_data_one_hot)
# Convert to dataframe
cleveland_data_one_hot <- as_tibble(cleveland_data_one_hot)
```
```{r Feature_selection_1}
# Eliminate low variance features
low_variance_cols <- nearZeroVar(cleveland_data_one_hot, freqCut = 95 / 5, uniqueCut = 10, saveMetrics = FALSE)
# dropped restecg.1 variable
cleveland_data_low_var_rm <- cleveland_data_one_hot[, -low_variance_cols]
# recursive features selection
rfProfile <- rfe(cleveland_data_low_var_rm, cleveland_data$heart_disease_present,
sizes = c(1:19),
rfeControl = rfeControl(functions = rfFuncs), number = 200)
plot(rfProfile, type = c("o", "g"))
predictors(rfProfile)
varImp(rfProfile)
# We shall include features that have variable of importance greater than or equal to 10
features_selection <- varImp(rfProfile) %>% rownames_to_column(var = 'Features') %>% dplyr::filter(Overall >= 10) %>% pull(Features)
features <- cleveland_data_low_var_rm %>% dplyr::select(all_of(features_selection))
```
## Evaluation metric
```{r R_Function}
# A function that generate data for class prediction probabilities and class prediction outcome
pred_data <- function(pred_probs){
as_tibble(pred_probs) %>%
mutate(
# keep predicted probabilities for the positive class only
prob = present,
class = names(.)[apply(., 1, which.max)]
)
}
# Logloss metric for evaluating performance of classifier
logloss <- function(actual_class, pred_prob, eps = 1e-15) {
yhat <- pmin(pmax(pred_prob, eps), 1 - eps)
logloss <- -(mean(actual_class * log(yhat) + (1 - actual_class) * log(1 - yhat)))
return(logloss)
}
# fct_int convert factor class levels (present/ absent) to integer 0/1
fct_int <- function (class_fct) {
class_int = as.numeric(as.character(fct_recode(class_fct, '0' = 'absent', '1' = 'present')))
return(class_int)
}
# int_fct convert integer class (1/ 0) to factor class present/absent
int_fct <- function (class_int) {
class_fct = fct_recode(factor(class_int), 'absent' = '0', 'present' = '1')
return(class_fct)
}
```
# Training models
```{r model-building1_10}
model_data <- bind_cols(features, y = cleveland_data$heart_disease_present) %>% janitor::clean_names()
# Set the total number of replications
R <- 200
# Initialize the evaluation metrics matrix for performances of the models
acc_metric <- matrix(0, ncol = 10, nrow = R)
pre_metric <- matrix(0, ncol = 10, nrow = R)
sen_metric <- matrix(0, ncol = 10, nrow = R)
spe_metric <- matrix(0, ncol = 10, nrow = R)
auc_metric <- matrix(0, ncol = 10, nrow = R)
logloss_metric <- matrix(0, ncol = 10, nrow = R)
f1_metric <- matrix(0, ncol = 10, nrow = R)
for (r in 1: R) {
# Create the training and test datasets for the Cleveland dataset
split <- sample(nrow(model_data), nrow(model_data) * 0.8)
train_clevelandData <- model_data[split, ]
test_clevelandData <- model_data[-split, ]
# Store X and Y for later use
x_train <- train_clevelandData[-ncol(train_clevelandData)]
y_train <- train_clevelandData$y
xy_train <- train_clevelandData
x_test <- test_clevelandData[-ncol(test_clevelandData)]
y_test <- test_clevelandData$y
# Converting y_test to class integer
y_test <- fct_int(y_test)
ntr <- nrow(train_clevelandData)
nte <- nrow(test_clevelandData)
# Models to consider
# LDA model
lda.model <- train(y ~ ., data = xy_train, method = "lda", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary), metric = "ROC")
lda_pred <- pred_data(pred_probs = predict(lda.model, x_test, type = "prob"))
lda_class <- fct_int(lda_pred$class)
acc_metric[r, 1] <- Metrics::accuracy(y_test, lda_class)
pre_metric[r, 1] <- Metrics::precision(y_test, lda_class)
sen_metric[r, 1] <- Metrics::recall(y_test, lda_class)
spe_metric[r, 1] <- ModelMetrics::specificity(y_test, lda_class)
auc_metric[r, 1] <- Metrics::auc(y_test, lda_pred$prob)
logloss_metric[r, 1] <- logloss(y_test, lda_pred$prob)
f1_metric[r, 1] <- ModelMetrics::f1Score(y_test, lda_class)
# SVM model
svm.model <- train(y ~ ., data = xy_train, method = "svmLinear2", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE), tuneGrid = data.frame(cost = c(.25, .5, 1)))
svm_pred <- pred_data(pred_probs = predict(svm.model, x_test, type = "prob"))
svm_class <- fct_int(svm_pred$class)
acc_metric[r, 2] <- Metrics::accuracy(y_test, svm_class)
pre_metric[r, 2] <- Metrics::precision(y_test, svm_class)
sen_metric[r, 2] <- Metrics::recall(y_test, svm_class)
spe_metric[r, 2] <- ModelMetrics::specificity(y_test, svm_class)
auc_metric[r, 2] <- Metrics::auc(y_test, svm_pred$prob)
logloss_metric[r, 2] <- logloss(y_test, svm_pred$prob)
f1_metric[r, 2] <- ModelMetrics::f1Score(y_test, svm_class)
# KNN model
knn.model <- train(y ~.,data=xy_train, method = "knn",
trControl=trainControl(method = "cv", number = 10,
returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary), metric = "ROC")
knn_pred <- pred_data(pred_probs = predict(knn.model, x_test, type = "prob"))
knn_class <- fct_int(knn_pred$class)
acc_metric[r, 3] <- Metrics::accuracy(y_test, knn_class)
pre_metric[r, 3] <- Metrics::precision(y_test, knn_class)
sen_metric[r, 3] <- Metrics::recall(y_test, knn_class)
spe_metric[r, 3] <- ModelMetrics::specificity(y_test, knn_class)
auc_metric[r, 3] <- Metrics::auc(y_test, knn_pred$prob)
logloss_metric[r, 3] <- logloss(y_test, knn_pred$prob)
f1_metric[r, 3] <- ModelMetrics::f1Score(y_test, knn_class)
# rForest
rforest.model <- train(y ~ ., data = xy_train, method = "rf", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary, seeds = vector(mode = "list", length = nrow(xy_train) + 1) %>% lapply(., function(x) 1:20)), metric = "ROC", ntree = 20,importance = TRUE)
rforest_pred <- pred_data(pred_probs = predict(rforest.model, x_test, type = "prob"))
rforest_class <- fct_int(rforest_pred$class)
acc_metric[r, 4] <- Metrics::accuracy(y_test, rforest_class)
pre_metric[r, 4] <- Metrics::precision(y_test, rforest_class)
sen_metric[r, 4] <- Metrics::recall(y_test, rforest_class)
spe_metric[r, 4] <- ModelMetrics::specificity(y_test, rforest_class)
auc_metric[r, 4] <- Metrics::auc(y_test, rforest_pred$prob)
logloss_metric[r, 4] <- logloss(y_test, rforest_pred$prob)
f1_metric[r, 4] <- ModelMetrics::f1Score(y_test, rforest_class)
# DT
d.tree.model <- train(y ~ ., data = xy_train, method = "rpart", trControl = trainControl(method = "cv", number = 10, returnResamp = "all"))
d.tree_pred <- pred_data(pred_probs = predict(d.tree.model, x_test, type = "prob"))
d.tree_class <- fct_int(d.tree_pred$class)
acc_metric[r, 5] <- Metrics::accuracy(y_test, d.tree_class)
pre_metric[r, 5] <- Metrics::precision(y_test, d.tree_class)
sen_metric[r, 5] <- Metrics::recall(y_test, d.tree_class)
spe_metric[r, 5] <- ModelMetrics::specificity(y_test, d.tree_class)
auc_metric[r, 5] <- Metrics::auc(y_test, d.tree_pred$prob)
logloss_metric[r, 5] <- logloss(y_test, d.tree_pred$prob)
f1_metric[r, 5] <- ModelMetrics::f1Score(y_test, d.tree_class)
# ANN
nnet.model <- train(y ~ ., data = xy_train, method = "nnet",
trControl = trainControl(method = "cv", number = 10, returnResamp = "all"), trace = FALSE)
nnet_pred <- pred_data(pred_probs = predict(nnet.model, x_test, type = "prob"))
nnet_class <- fct_int(nnet_pred$class)
acc_metric[r, 6] <- Metrics::accuracy(y_test, nnet_class)
pre_metric[r, 6] <- Metrics::precision(y_test, nnet_class)
sen_metric[r, 6] <- Metrics::recall(y_test, nnet_class)
spe_metric[r, 6] <- ModelMetrics::specificity(y_test, nnet_class)
auc_metric[r, 6] <- Metrics::auc(y_test, nnet_pred$prob)
logloss_metric[r, 6] <- logloss(y_test, nnet_pred$prob)
f1_metric[r, 6] <- ModelMetrics::f1Score(y_test, nnet_class)
# LogitBoost
logit.model <- train(y ~ ., data = xy_train, method = "LogitBoost", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary), metric = "ROC")
logit_pred <- pred_data(pred_probs = predict(logit.model, x_test, type = "prob"))
logit_class <- fct_int(logit_pred$class)
acc_metric[r, 7] <- Metrics::accuracy(y_test, logit_class)
pre_metric[r, 7] <- Metrics::precision(y_test, logit_class)
sen_metric[r, 7] <- Metrics::recall(y_test, logit_class)
spe_metric[r, 7] <- ModelMetrics::specificity(y_test, logit_class)
auc_metric[r, 7] <- Metrics::auc(y_test, logit_pred$prob)
logloss_metric[r, 7] <- logloss(y_test, logit_pred$prob)
f1_metric[r, 7] <- ModelMetrics::f1Score(y_test, logit_class)
# NB
naiveBayes.model <- train(y ~ ., data = xy_train, method = "naive_bayes", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary), metric = "ROC")
naiveBayes_pred <- pred_data(pred_probs = predict(naiveBayes.model, x_test, type = "prob"))
naiveBayes_class <- fct_int(naiveBayes_pred$class)
acc_metric[r, 8] <- Metrics::accuracy(y_test, naiveBayes_class)
pre_metric[r, 8] <- Metrics::precision(y_test, naiveBayes_class)
sen_metric[r, 8] <- Metrics::recall(y_test, naiveBayes_class)
spe_metric[r, 8] <- ModelMetrics::specificity(y_test, naiveBayes_class)
auc_metric[r, 8] <- Metrics::auc(y_test, naiveBayes_pred$prob)
logloss_metric[r, 8] <- logloss(y_test, naiveBayes_pred$prob)
f1_metric[r, 8] <- ModelMetrics::f1Score(y_test, naiveBayes_class)
# XGBTree
xgbtree.model <- train(y ~ ., data = xy_train,
method = "xgbTree", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary), metric = "ROC", tuneGrid = expand.grid(nrounds = c(1, 10),
max_depth = c(1, 4), eta = c(.1, .4), gamma = 0, colsample_bytree = .7, min_child_weight = 1, subsample = c(.8, 1)))
xgbtree_pred <- pred_data(pred_probs = predict(xgbtree.model, x_test, type = "prob"))
xgbtree_class <- fct_int(xgbtree_pred$class)
acc_metric[r, 9] <- Metrics::accuracy(y_test, xgbtree_class)
pre_metric[r, 9] <- Metrics::precision(y_test, xgbtree_class)
sen_metric[r, 9] <- Metrics::recall(y_test, xgbtree_class)
spe_metric[r, 9] <- ModelMetrics::specificity(y_test, xgbtree_class)
auc_metric[r, 9] <- Metrics::auc(y_test, xgbtree_pred$prob)
logloss_metric[r, 9] <- logloss(y_test, xgbtree_pred$prob)
f1_metric[r, 9] <- ModelMetrics::f1Score(y_test, xgbtree_class)
# Cforest
cforest.model <- train(y ~ ., data = xy_train,
method = "cforest", trControl = trainControl(method = "cv", number = 10, returnResamp = "all", classProbs = TRUE, summaryFunction = twoClassSummary, seeds = vector(mode = "list", length = nrow(xy_train) + 1) %>% lapply(., function(x) 1:20)), metric = "ROC", controls = party::cforest_unbiased(ntree = 20))
cforest_pred <- pred_data(pred_probs = predict(cforest.model, x_test, type = "prob"))
cforest_class <- fct_int(cforest_pred$class)
acc_metric[r, 10] <- Metrics::accuracy(y_test, cforest_class)
pre_metric[r, 10] <- Metrics::precision(y_test, cforest_class)
sen_metric[r, 10] <- Metrics::recall(y_test, cforest_class)
spe_metric[r, 10] <- ModelMetrics::specificity(y_test, cforest_class)
auc_metric[r, 10] <- Metrics::auc(y_test, cforest_pred$prob)
logloss_metric[r, 10] <- logloss(y_test, cforest_pred$prob)
f1_metric[r, 10] <- ModelMetrics::f1Score(y_test, cforest_class)
if (r %% 25 == 0) cat('\n', paste(round(100 * r / R, 0), '%', 'completed\n'))
}
```
# Evaluation metric tables
```{r Performance_metrics}
col_names <- c("LDA", "SVM", "KNN", "Rforest", "DT", "ANN", "Logit", "NB", "XGBTree", "Cforest")
# Accuracy across 200 replications
colnames(acc_metric) <- col_names
acc_metric <- as_tibble(acc_metric)
# Precision across 200 replications
colnames(pre_metric) <- col_names
pre_metric <- as_tibble(pre_metric)
# Sensitivity across 200 replications
colnames(sen_metric) <- col_names
sen_metric <- as_tibble(sen_metric)
# Specificity across 200 replications
colnames(spe_metric) <- col_names
spe_metric <- as_tibble(spe_metric)
# AUC across 200 replications
colnames(auc_metric) <- col_names
auc_metric <- as_tibble(auc_metric)
# Logloss across 200 replications
colnames(logloss_metric) <- col_names
logloss_metric <- as_tibble(logloss_metric)
# F1 across 200 replications
colnames(f1_metric) <- col_names
f1_metric <- as_tibble(logloss_metric)
```
# Evaluation metric plots
```{r plot_metric}
plot_metric <- function(df, metric = 'name_metric') {
df %>%
pivot_longer(1:10, names_to = "models", values_to = "metric") %>%
ggplot(., aes(x = reorder(models, metric, FUN = median), y = metric, fill = models)) + geom_boxplot(show.legend = FALSE, width = 0.3, outlier.size = 1, outlier.shape = 5, outlier.colour = "purple") + theme_bw() + labs(y = paste0(metric), x = "Method (Classifier)") + theme(axis.title.x = element_text(face = "bold", size = 12), axis.title.y = element_text(face = "bold", size = 12), axis.text.x = element_text(angle = 50, vjust = 0.5, face = "bold"))
}
```
```{r accuracy}
plot_metric(acc_metric, 'Accuracy')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/Accuracy.png', width = 6.74, height = 4.54)
```
```{r Precision}
plot_metric(pre_metric, 'Precision')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/Precision.png', width = 6.74, height = 4.54)
```
```{r Sensitivity}
plot_metric(sen_metric, 'Sensitivity')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/Sensitivity.png', width = 6.74, height = 4.54)
```
```{r Specificity}
plot_metric(spe_metric, 'Specificity')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/Specificity.png', width = 6.74, height = 4.54)
```
```{r auc}
plot_metric(auc_metric, 'AUC')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/AUC.png', width = 6.74, height = 4.54)
```
```{r f1}
plot_metric(f1_metric, 'F1 score')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/F1_score.png', width = 6.74, height = 4.54)
```
```{r logloss}
plot_metric(logloss_metric, 'logLoss')
ggsave('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/Log-loss.png', width = 6.74, height = 4.54)
```
# Table of ranks of models for each metric
```{r Accuracy_rank}
# Table of mean, median, standard deviation
acc_table <- acc_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "accuracy") %>%
group_by(models) %>%
summarise(avg_accuracy = round(mean(accuracy), 4), med_accuracy = round(median(accuracy), 4), std_accuracy = round(sd(accuracy), 4))
# Table of rank
acc_table.rk <- acc_table %>% mutate(avg_accuracy.rk = rank(desc(avg_accuracy)), med_accuracy.rk = rank(desc(med_accuracy)), std_accuracy.rk = rank(std_accuracy)) %>% arrange(med_accuracy.rk)
acc_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/acc_table.rk.csv")
```
```{r Precision_rank}
# Table of mean, median, standard deviation
precision_table <- pre_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "precision") %>%
group_by(models) %>% summarise(avg_precision = round(mean(precision),4), med_precision = round(median(precision), 4), std_precision = round(sd(precision), 4))
# Table of rank
precision_table.rk <- precision_table %>% mutate(avg_precision.rk=rank(desc(avg_precision)), med_precision.rk =rank(desc(med_precision)), std_precision.rk=rank(std_precision)) %>% arrange(med_precision.rk)
precision_table.rk %>% write_csv('Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/precision_table.rk.csv')
```
```{r Sensitivity_rank}
# Table of mean, median, standard deviation
sensitivity_table <- sen_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "sensitivity") %>%
group_by(models) %>% summarise(avg_sensitivity = round(mean(sensitivity),4), med_sensitivity = round(median(sensitivity), 4), std_sensitivity = round(sd(sensitivity), 4))
# Table of rank
sensitivity_table.rk <- sensitivity_table %>% mutate(avg_sensitivity.rk = rank(desc(avg_sensitivity)), med_sensitivity.rk = rank(desc(med_sensitivity)), std_sensitivity.rk = rank(std_sensitivity)) %>% arrange(med_sensitivity.rk)
sensitivity_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/sensitivity_table.rk.csv")
```
```{r Specificity_rank}
# Table of mean, median, standard deviation
specificity_table <- spe_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "specificity") %>%
group_by(models) %>%
summarise(avg_specificity = round(mean(specificity), 4), med_specificity = round(median(specificity), 4), std_specificity = round(sd(specificity), 4))
# Table of rank
specificity_table.rk <- specificity_table %>% mutate(avg_specificity.rk = rank(desc(avg_specificity)), med_specificity.rk = rank(desc(med_specificity)), std_specificity.rk = rank(std_specificity)) %>% arrange(med_specificity.rk)
specificity_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/specificity_table.rk.csv")
```
```{r AUC_rank}
# Table of mean, median, standard deviation
auc_table <- auc_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "auc") %>%
group_by(models) %>%
summarise(avg_AUC = round(mean(auc), 4), med_AUC = round(median(auc), 4), std_AUC = round(sd(auc), 4))
# Table of rank
auc_table.rk <- auc_table %>% mutate(avg_AUC.rk = rank(desc(avg_AUC)), med_AUC.rk = rank(desc(med_AUC)), std_AUC.rk = rank(std_AUC)) %>% arrange(med_AUC.rk)
auc_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/AUC_table.rk.csv")
```
```{r F1_rank}
# Table of mean, median, standard deviation
f1_table <- f1_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "f1_score") %>%
group_by(models) %>%
summarise(avg_F1 = round(mean(f1_score), 4), med_F1 = round(median(f1_score), 4), std_F1 = round(sd(f1_score, 4)))
# Table of rank
f1_table.rk <- f1_table %>% mutate(avg_F1.rk = rank(desc(avg_F1)), med_F1.rk = rank(desc(med_F1)), std_F1.rk = rank(std_F1)) %>% arrange(med_F1.rk)
f1_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/F1_table.rk.csv")
```
```{r logLoss_rank}
# Table of mean, median, standard deviation
logloss_table <- logloss_metric %>%
pivot_longer(1:10, names_to = "models", values_to = "logloss") %>%
group_by(models) %>%
summarise(avg_logloss = round(mean(logloss), 4), med_logloss = round(median(logloss), 4), std_logloss = round(sd(logloss), 4))
# Table of rank
logloss_table.rk <- logloss_table %>% mutate(avg_logloss.rk = rank(avg_logloss), med_logloss.rk = rank(med_logloss), std_logloss.rk = rank(std_logloss)) %>% arrange(std_logloss.rk)
logloss_table.rk %>% write_csv("Heart-disease-data-analysis/Models-evaluation-metrics-figures-and-tables/logloss_table.rk.csv")
```
# Validation dataset from the Statlog heart disease database
```{r statlog_data_data}
statlog_data <- read_xlsx('data/heart_disease_database.xlsx', sheet = 2) %>%
mutate(across(.cols = c(7, 12), as.character))
statlog_data <- statlog_data %>%
mutate(heart_disease_present= factor(ifelse(heart_disease_present == 0, 'absent', 'present')))
# Changing the data types to the right type in statlog_data data
statlog_data <-
statlog_data %>%
mutate(across(!where(is_double) , as_factor))
```
```{r statlog_data_scaling}
# Scaling the continuous variables
statlog_data_scaling <- predict(preProcess_scale_model, statlog_data)
```
```{r statlog_data_One_hot_encoding}
# Removing the class column on train data to be able to create a one hot encoding
statlog_data_one_hot <- statlog_data_scaling[-length(statlog_data_scaling)]
# `fullRank = T` to avoid dummmy trap
statlog_data_one_hot <- predict(dummies, newdata = statlog_data_one_hot)
# Convert to dataframe
statlog_data_one_hot <- as_tibble(statlog_data_one_hot)
```
```{r statlog_data_Feature_selection}
# Eliminate low variance features
statlog_data_low_var_rm <- statlog_data_one_hot[, -low_variance_cols] # dropped restecg.1 variable
# recursive features selection
# We shall include features that have variable of importance greater than or equal to 10
x_statlog_data <- statlog_data_low_var_rm %>% dplyr::select(all_of(features_selection)) %>% janitor::clean_names()
y_statlog_data <- statlog_data$heart_disease_present
```
# Validating the best model (SVM) on statlog_data set
```{r Model_Production}
svm_val_pred <- pred_data(pred_probs = predict(svm.model, x_statlog_data, type = "prob"))
svm_val_class <- as.factor(svm_val_pred$class)
```
# Confusion matrix with some other metrics
```{r confusion matrix}
cm <- confusionMatrix(svm_val_class, y_statlog_data, positive = 'present')
cm$byClass[6] <- ModelMetrics::auc(y_statlog_data, svm_val_pred$prob)
cm$byClass[11] <- logloss(fct_int(y_statlog_data), svm_val_pred$prob)
names(cm$byClass) = c("Sensitivity", "Specificity", "Pos Pred Value", "Neg Pred Value", "Precision", "AUC", "F1 score", "Prevalence", "Detection Rate", "Detection Prevalence", "logloss")
conf_matrix_cust_plot <- function(cm_input, model_pred, class_label1 = "Class Negative", class_label2 = "Class Positive", quadrant_col1 = "#3F97D0", quadrant_col2 = "#F7AD50", custom_title = "Confusion matrix", text_col = "black", round_dig = 2) {
layout(matrix(c(1, 1, 2)))
par(mar = c(2, 2, 2, 2))
plot(c(100, 345), c(300, 450), type = "n", xlab = "", ylab = "", xaxt = "n", yaxt = "n")
# n is specified in plot to indicate no plotting
title(custom_title, cex.main = 2)
# Create the matrix visualisation using custom rectangles and text items on the chart
rect(150, 430, 240, 370, col = '#009900')
text(195, 435, class_label1, cex = 1.2)
rect(250, 430, 340, 370, col = quadrant_col2)
text(295, 435, class_label2, cex = 1.2)
text(125, 370, "Predicted", cex = 1.3, srt = 90, font = 2)
text(245, 450, "Actual", cex = 1.3, font = 2)
rect(150, 305, 240, 365, col = quadrant_col2)
rect(250, 305, 340, 365, col = '#ff0000')
text(140, 400, class_label1, cex = 1.2, srt = 90)
text(140, 335, class_label2, cex = 1.2, srt = 90)
# Add the results of the confusion matrix - as these will be saved to cm$table
result <- as.numeric(cm$table)
text(195, 400, result[1], cex = 1.6, font = 2, col = text_col)
text(195, 335, result[2], cex = 1.6, font = 2, col = text_col)
text(295, 400, result[3], cex = 1.6, font = 2, col = text_col)
text(295, 335, result[4], cex = 1.6, font = 2, col = text_col)
# Add in other confusion matrix statistics
plot(c(100, 0), c(100, 0), type = "n", xlab = "", ylab = "", main = "Model performance metrics", xaxt = "n", yaxt = "n")
text(10, 85, names(cm$byClass[1]), cex = 1.6, font = 2)
text(10, 70, round(as.numeric(cm$byClass[1]), round_dig), cex = 1.2)
text(30, 85, names(cm$byClass[2]), cex = 1.6, font = 2)
text(30, 70, round(as.numeric(cm$byClass[2]), round_dig), cex = 1.2)
text(50, 85, names(cm$byClass[5]), cex = 1.6, font = 2)
text(50, 70, round(as.numeric(cm$byClass[5]), round_dig), cex = 1.2)
text(65, 85, names(cm$byClass[6]), cex = 1.6, font = 2)
text(65, 70, round(as.numeric(cm$byClass[6]), round_dig), cex = 1.2)
text(86, 85, names(cm$byClass[11]), cex = 1.6, font = 2)
text(86, 70, round(as.numeric(cm$byClass[11]), round_dig), cex = 1.2)
# add in the accuracy information
text(30, 35, names(cm$overall[1]), cex = 1.5, font = 2)
text(30, 20, round(as.numeric(cm$overall[1]), 4), cex = 1.4)
text(70, 35, names(cm$byClass[7]), cex = 1.5, font = 2)
text(70, 20, round(as.numeric(cm$byClass[7]), round_dig), cex = 1.4)
}
conf_matrix_cust_plot(cm,
class_label1 = "Absent",
class_label2 = "Present",
quadrant_col1 = "#009900",
quadrant_col2 = "#ec008e",
custom_title = "",
round_dig = 3
)
```
# The end!