-
Notifications
You must be signed in to change notification settings - Fork 1
/
random_forest.R
161 lines (146 loc) · 6.47 KB
/
random_forest.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
# random forest model
set.seed(1)
rf_original <- train(x = data_training[, c(3, 8, 9, 10)],
y = data_training$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# original model predictions
predictions_rf_original <- predict(rf_original, data_testing)
cm_rf_original <- confusionMatrix(predictions_rf_original, data_testing$Compliant..Y.N.)
cm_rf_original
cm_rf_original$byClass["F1"]
gmean_rf_original <- unname((cm_rf_original$byClass["Specificity"] * cm_rf_original$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_original
# random forest using weighted data
set.seed(2)
rf_weighted <- train(x = data_training[, c(3, 8, 9, 10)],
y = data_training$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
weights = class_weights,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# weighted model predictions
predictions_rf_weighted <- predict(rf_weighted, data_testing)
cm_rf_weighted <- confusionMatrix(predictions_rf_weighted, data_testing$Compliant..Y.N.)
cm_rf_weighted
cm_rf_weighted$byClass["F1"]
gmean_rf_weighted <- unname((cm_rf_weighted$byClass["Specificity"] * cm_rf_weighted$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_weighted
# random forest using under-sampled data
set.seed(3)
rf_under <- train(x = data_under[, c(3, 8, 9, 10)],
y = data_under$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# under-sampled model predictions
predictions_rf_under <- predict(rf_under, data_testing)
cm_rf_under <- confusionMatrix(predictions_rf_under, data_testing$Compliant..Y.N.)
cm_rf_under
cm_rf_under$byClass["F1"]
gmean_rf_under <- unname((cm_rf_under$byClass["Specificity"] * cm_rf_under$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_under
# random forest using over-sampled data
set.seed(4)
rf_over <- train(x = data_over[, c(3, 8, 9, 10)],
y = data_over$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# over-sampled model predictions
predictions_rf_over <- predict(rf_over, data_testing)
cm_rf_over <- confusionMatrix(predictions_rf_over, data_testing$Compliant..Y.N.)
cm_rf_over
cm_rf_over$byClass["F1"]
gmean_rf_over <- unname((cm_rf_over$byClass["Specificity"] * cm_rf_over$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_over
# random forest using rose data
set.seed(5)
rf_rose <- train(x = data_rose[, c(3, 8, 9, 10)],
y = data_rose$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# rose model predictions
predictions_rf_rose <- predict(rf_rose, data_testing)
cm_rf_rose <- confusionMatrix(predictions_rf_rose, data_testing$Compliant..Y.N.)
cm_rf_rose
cm_rf_rose$byClass["F1"]
gmean_rf_rose <- unname((cm_rf_rose$byClass["Specificity"] * cm_rf_rose$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_rose
# random forest using smote data
set.seed(6)
rf_smote <- train(x = data_smote[, c(3, 8, 9, 10)],
y = data_smote$Compliant..Y.N.,
method = "ranger",
importance = "impurity",
tuneLength = 10,
metric = "ROC",
trControl = trainControl(method = "cv",
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary))
# smote model predictions
predictions_rf_smote <- predict(rf_smote, data_testing)
cm_rf_smote <- confusionMatrix(predictions_rf_smote, data_testing$Compliant..Y.N.)
cm_rf_smote
cm_rf_smote$byClass["F1"]
gmean_rf_smote <- unname((cm_rf_smote$byClass["Specificity"] * cm_rf_smote$byClass["Sensitivity"]) ^ 0.5)
gmean_rf_smote
# comparison between different random forest models
rf_models <- list(original = rf_original,
weighted = rf_weighted,
under = rf_under,
over = rf_over,
rose = rf_rose,
smote = rf_smote)
rf_models_resampling <- resamples(rf_models)
summary(rf_models_resampling)
bwplot(rf_models_resampling)
rf_models_roc <- rf_models %>%
map(test_roc, data = data_testing)
rf_models_roc %>%
map(auc)
rf_results_roc <- list(NA)
num_model <- 1
for(roc in rf_models_roc){
rf_results_roc[[num_model]] <-
data_frame(TPR = roc$sensitivities,
FPR = 1 - roc$specificities,
model = names(rf_models)[num_model])
num_model <- num_model + 1
}
rf_results_roc <- bind_rows(rf_results_roc)
# plot ROC curve for all 6 random forest models
ggplot_rf_roc_curve <- ggplot(aes(x = FPR, y = TPR, groover = model), data = rf_results_roc) +
geom_line(aes(color = model), size = 1) +
scale_color_manual(values = c("#000000", "#009E73", "#0072B2", "#D55E00", "#CC79A7", "#E69F00")) +
geom_abline(intercept = 0, slope = 1, color = "gray", size = 1) +
theme_bw(base_size = 18)
plot(ggplot_rf_roc_curve)