-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathImbalanced_GermanCredit.Rmd
198 lines (151 loc) · 4.86 KB
/
Imbalanced_GermanCredit.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
---
title: "Unbalanced German Credit"
output:
html_document:
toc: yes
toc_float: yes
code_folding: hide
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(dplyr)
library(caret)
library(rpart)
library(rpart.plot)
library(purrr) # for functional programming (map)
```
Originally inspired by https://www.r-bloggers.com/handling-class-imbalance-with-r-and-caret-an-introduction/.
```{r}
# Helper function to print the confusion matrix and other performance metrics of the models.
printPerformance = function(pred, actual, positive="Yes") {
print(caret::confusionMatrix(data=pred, reference=actual, positive=positive, dnn=c("Predicted", "Actual")))
}
```
```{r}
data(GermanCredit, package = "caret")
df = GermanCredit
df$Class = as.character(df$Class)
df$Class[df$Class == "Bad"] = "Not Good"
df$Class = as.factor(df$Class)
str(df)
head(df)
table(df$Class)
```
# Splitting the Data
```{r}
set.seed(123) # Set the seed to make it reproducible
train.index <- createDataPartition(df$Class, p = .8, list = FALSE)
train <- df[ train.index,]
test <- df[-train.index,]
# Double check that the stratefied sampling worked
table(df$Class)/nrow(df)
table(train$Class)/nrow(train)
table(test$Class)/nrow(test)
actual = test$Class
formula = Class ~ .
positive = "Good"
```
# Training Models withe the Various Techniques
```{r}
metric = "Accuracy"
ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 5, classProbs = FALSE)
orig_fit <- train(formula, data = train, method = "rpart", metric = metric, trControl = ctrl)
kappa_fit <- train(formula, data = train, method = "rpart", metric = "Kappa", trControl = ctrl)
weight = table(train$Class)["Not Good"] / table(train$Class)["Good"]
model_weights <- ifelse(train$Class == "Good", weight, 1)
weight_fit <- train(formula, data = train, method = "rpart", metric = metric, weights=model_weights, trControl = ctrl)
FN_cost = 10
FP_cost = 1
cost_fn <- train(formula, data = train, method = "rpart", metric = metric,
parms=list(loss=matrix(c(0,FN_cost,FP_cost,0), byrow=TRUE, nrow=2)),
trControl = ctrl)
FN_cost = 1
FP_cost = 10
cost_fp <- train(formula, data = train, method = "rpart", metric = metric,
parms=list(loss=matrix(c(0,FN_cost,FP_cost,0), byrow=TRUE, nrow=2)),
trControl = ctrl)
ctrl$sampling = "down"
down_fit <- train(formula, data = train, method = "rpart", metric = metric, trControl = ctrl)
ctrl$sampling = "smote"
smote_fit <- train(formula, data = train, method = "rpart", metric = metric, trControl = ctrl)
ctrl$sampling = "smote"
metric="Kappa"
all_fit <- train(formula, data = train, method = "rpart", metric = metric, trControl = ctrl)
```
# Assessing the Performance of the techniques
```{r}
assessModel = function(m_name, m){
pred = predict(m$finalModel, test, type="class")
a = caret::confusionMatrix(data=pred, reference=actual, positive=positive, dnn=c("Predicted", "Actual"))
res1 = data.frame(name=m_name,
accuracy=a$overall["Accuracy"],
precision=a$byClass["Precision"],
recall=a$byClass["Recall"],
specificity=a$byClass["Specificity"],
kappa=a$overall["Kappa"])
res1
}
res = data.frame(name=character(), accuracy=numeric(), precision=numeric(), recall=numeric(), specificity=numeric(), kappa=numeric())
res = rbind(res, assessModel("orig", orig_fit))
res = rbind(res, assessModel("kappa", kappa_fit))
res = rbind(res, assessModel("weights", weight_fit))
res = rbind(res, assessModel("cost fn", cost_fn))
res = rbind(res, assessModel("cost fp", cost_fp))
res = rbind(res, assessModel("down", down_fit))
res = rbind(res, assessModel("smote", smote_fit))
res = rbind(res, assessModel("all", all_fit))
row.names(res) = NULL
res
```
```{r}
library(formattable)
library(kableExtra)
#res %>%
#mutate(
# accuracy = color_tile("white", "orange")(accuracy),
# precision = ifelse(precision > 200,
# kableExtra::cell_spec(precision, color = "red", bold = T),
# kableExtra::cell_spec(precision, color = "green", italic = T)),
# recall = color_bar("lightgreen")(recall)
#) %>%
#kable(escape = F) %>%
#kable_styling("hover", full_width = F)
```
```{r}
# Function to show the confusion matrix and resulting tree
showResults = function(model){
pred = predict(model$finalModel, test, type="class")
print(caret::confusionMatrix(data=pred, reference=actual, positive=positive, dnn=c("Predicted", "Actual")))
rpart.plot(model$finalModel, extra=2, type=2)
}
```
# Original
```{r}
showResults(orig_fit)
```
# Kappa
```{r}
showResults(kappa_fit)
```
# Weights
```{r}
showResults(weight_fit)
```
# Costs - FP
```{r}
showResults(cost_fp)
```
# Costs - FN
```{r}
showResults(cost_fn)
```
# Down sampling
```{r}
showResults(down_fit)
```
# All
```{r}
showResults(all_fit)
```