-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAlgorithm Comparison.Rmd
279 lines (181 loc) · 8.17 KB
/
Algorithm Comparison.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
---
title: "Classification 2 Homework"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r, message=F, warning=F, eval=T, echo=T}
library(tidyverse)
library(MASS)
library(caret)
library(kableExtra)
library(e1071)
library(ISLR)
library(rpart)
library(DMwR)
```
### Data
Using the Default data from ISLR
```{r, message=F, warning=F, fig.width=6, fig.height=3, fig.align="center"}
set.seed(913)
dfDefault = Default
dfDefault$default = factor(dfDefault$default)
dfDefault$student = factor(dfDefault$student)
ggplot(dfDefault, aes(balance, fill = default)) +
geom_histogram(binwidth = 500) +
facet_wrap(~student)
dfDefault <- dfDefault %>% rownames_to_column("SampleID")
xTrain <- sample_n(dfDefault, round(nrow(dfDefault)*.6))
xTest <- dfDefault %>% anti_join(xTrain, by = "SampleID")
```
## Comparison of Algorithms
Use the follwing template that compares algorithms and then adds a SMOTE section and compares again. Follow this template, except use the Employee Turnover data.
### Logistic Regression
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
glModS <- glm(default ~ student + balance + income, data = xTrain, family = binomial)
glmPred <- predict(glModS, type = "response", newdata = xTest)
xTest$GLM = if_else(glmPred < .5, "No", "Yes")
CM = confusionMatrix(factor(xTest$GLM), factor(xTest$default), positive = "Yes")
Summary = data.frame(Algorithm = "GLM",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
```
### Linear Discriminant Analysis
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
lda.fit <- lda(default ~ student + balance + income, xTrain)
lda.pred <- predict(lda.fit, xTest)
xTest$LDA = lda.pred$class
CM = confusionMatrix(xTest$LDA, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "LDA",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### Naive Bayes
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
NBmodel <- naiveBayes(default ~ student + balance + income, data = xTrain)
xTest$NB <- predict(NBmodel, xTest, prob = TRUE)
CM = confusionMatrix( xTest$NB, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "NB",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### Decision Tree
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
Treefit <- rpart(default ~ student + balance + income,
data = xTrain,
method="class")
xTest$Tree = predict(Treefit, type = "class", newdata = xTest) # factor
CM = confusionMatrix(xTest$Tree, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "Tree",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### Support Vector Machine
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
svmMod <- svm(default ~ student + balance + income, data = xTrain)
xTest$SVM <- predict(svmMod, xTest)
CM = confusionMatrix(xTest$SVM, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "SVM",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
## SMOTE Sampling
### Data Creation
```{r, message=F, warning=F, echo = T, results = "hide", fig.width=4, fig.height=3, fig.align="center"}
smoteData <- SMOTE(default ~ student + balance + income, data = Default, perc.over = 350, perc.under=130) # SMOTE only works with factors
prop.table(table(smoteData$default))
```
### Logistic Regression with SMOTE
```{r, message=F, warning=F, eval=T, echo=T}
glModSmote <- glm(default ~ student + balance + income, data = smoteData, family = binomial)
glmPredSmote <- predict(glModSmote, type = "response", newdata = xTest)
xTest$GLMSmote = if_else(glmPredSmote < .5, "No", "Yes")
CM = confusionMatrix(factor(xTest$GLMSmote), factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "GLMSmote",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### LDA with SMOTE
```{r, message=F, warning=F, eval=T, echo=T}
lda.fit <- lda(default ~ student + balance + income, smoteData)
lda.pred <- predict(lda.fit, xTest)
xTest$LDASmote = lda.pred$class
CM = confusionMatrix(xTest$LDASmote, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "LDASmote",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### Naive Bayes with SMOTE
```{r, message=F, warning=F, eval=T, echo=T}
model <- naiveBayes(default ~ student + balance + income, data = smoteData)
xTest$NBSmote <- predict(model, xTest, prob = TRUE)
CM = confusionMatrix(xTest$NBSmote, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "NBSmote",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### Decision Tree with SMOTE
```{r, message=F, warning=F, eval=T, echo=T}
TreefitSmote <- rpart(default ~ student + balance + income,
data = xTrain,
method="class")
xTest$TreeSmote = predict(TreefitSmote, type = "class", newdata = xTest) # factor
CM = confusionMatrix( xTest$TreeSmote, factor(xTest$default),positive = "Yes")
Summaryadd = data.frame(Algorithm = "TreeSmote",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
### SVM with SMOTE
```{r, message=F, warning=F, eval=T, echo=T}
svmMod <- svm(default ~ student + balance + income, data = smoteData)
xTest$SVMSmote <- predict(svmMod, xTest)
CM = confusionMatrix(xTest$SVMSmote, factor(xTest$default), positive = "Yes")
Summaryadd = data.frame(Algorithm = "SVMSmote",
Sensitivity = CM$byClass[1],
Specificity = CM$byClass[2],
PosPredVal = CM$byClass[3],
NegPredVal = CM$byClass[4],
Prevalence = CM$byClass[8])
Summary = bind_rows(Summary, Summaryadd)
```
## Results and Review
```{r, message=F, warning=F, eval=T, echo=T}
knitr::kable(Summary) %>%
kable_styling(full_width = F, bootstrap_options = "striped", font_size = 9)
```