-
Notifications
You must be signed in to change notification settings - Fork 0
/
bankmarketing_nbandar.r
287 lines (194 loc) · 7.5 KB
/
bankmarketing_nbandar.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
#* Nirali Bandaru
#* CPSC6300 - Applied Data Science
#* Prof. Alexander Herzog
#* Clemson University
#* Purpose: Individual Project for 6300 level credit
#* Data Due: December 2, 2020
list(rm=ls())
#LOAD LIBRARIES
library(ggplot2)
library(dplyr)
library(randomForest)
library(boot)
library(caret)
library(e1071)
library(tree)
library(scales)
library(randomForest)
library(ROCR)
library(DMwR)
#LOAD DATA
mydata <- read.csv("bank-full.csv", stringsAsFactors = TRUE)
df <- data.frame(mydata)
attach(mydata)
#View(mydata)
#EXPLORATORY DATA ANALYSIS
#Data Cleaning
any(is.na(mydata))
#no na values
#no unwanted columns
#examine summary of variables
summary(mydata)
table(job)
table(marital)
table(education)
table(default)
table(housing)
table(loan)
table(contact)
table(month)
table(poutcome)
table(day)
table(y)
#visual summary of quantitative predictors
#AGE
age_plot <- ggplot(data = mydata, mapping = aes(x = age)) +
geom_histogram(color = "black", fill = "lightgreen") +
ggtitle("Age") + theme(plot.title = element_text(hjust = 0.5))
age_plot
#BALANCE
balance_plot <- ggplot(data = mydata, mapping = aes(x = balance)) +
geom_histogram(color = "black", fill = "lightblue") +
ggtitle("Balance") + theme(plot.title = element_text(hjust = 0.5))
balance_plot
balance_plot <- ggplot(data = mydata, mapping = aes(x = balance)) +
geom_boxplot(color = "black", fill = "lightblue") +
ggtitle("Balance") + theme(plot.title = element_text(hjust = 0.5))
balance_plot
balance_plot <- ggplot(data = mydata, mapping = aes(x = balance)) +
geom_density(color = "black", fill = "lightblue") +
ggtitle("Balance") + theme(plot.title = element_text(hjust = 0.5))
balance_plot
#DAY
day_plot <- ggplot(data = mydata, mapping = aes(x = day)) +
geom_histogram(color = "black", fill = "limegreen") +
ggtitle("Day") + theme(plot.title = element_text(hjust = 0.5))
day_plot
#PDAYS
pday_plot <- ggplot(data = mydata, mapping = aes(x = pdays)) +
geom_histogram(color = "black", fill = "brown") +
ggtitle("Days Passed Since Last Contact") + theme(plot.title = element_text(hjust = 0.5))
pday_plot
pday_plot <- ggplot(data = mydata, mapping = aes(x = pdays)) +
geom_boxplot(color = "black", fill = "brown") +
ggtitle("Days Passed Since Last Contact") + theme(plot.title = element_text(hjust = 0.5))
pday_plot
#DURATION
duration_plot <- ggplot(data = mydata, mapping = aes(x = duration)) +
geom_histogram(color = "black", fill = "lightyellow") +
ggtitle("Duration") + theme(plot.title = element_text(hjust = 0.5))
duration_plot
duration_plot <- ggplot(data = mydata, mapping = aes(x = duration)) +
geom_boxplot(color = "black", fill = "lightyellow") +
ggtitle("Duration") + theme(plot.title = element_text(hjust = 0.5))
duration_plot
#CAMPAIGNS
campaign_plot <- ggplot(data = mydata, mapping = aes(x = campaign)) +
geom_histogram(color = "black", fill = "purple") +
ggtitle("Campaign") + theme(plot.title = element_text(hjust = 0.5))
campaign_plot
campaign_plot <- ggplot(data = mydata, mapping = aes(x = campaign)) +
geom_boxplot(color = "black", fill = "purple") +
ggtitle("Campaign") + theme(plot.title = element_text(hjust = 0.5))
campaign_plot
#PREVIOUS
previous_plot <- ggplot(data = mydata, mapping = aes(x = previous)) +
geom_histogram(color = "black", fill = "orange") +
ggtitle("Previous") + theme(plot.title = element_text(hjust = 0.5))
previous_plot
previous_plot <- ggplot(data = mydata, mapping = aes(x = previous)) +
geom_boxplot(color = "black", fill = "orange") +
ggtitle("Previous") + theme(plot.title = element_text(hjust = 0.5))
previous_plot
#JOB
job_count <- mydata %>% count(job)
job_plot <- ggplot(data = mydata, mapping = aes(x = job)) +
geom_bar(color = "black", fill = "cyan") + ggtitle("Job") + theme(plot.title = element_text(hjust = 0.5))
job_plot
#MONTH
month_plot <- ggplot(data = mydata, mapping = aes(x = month)) +
geom_bar(color = "black", fill = "cornflowerblue") +
ggtitle("Month") + theme(plot.title = element_text(hjust = 0.5))
month_plot
#MARITAL STATUS
marital_plot <- ggplot(data = mydata, mapping = aes(x = marital)) +
geom_bar(color = "black", fill = "maroon") + ggtitle("Marital Status") + theme(plot.title = element_text(hjust = 0.5))
marital_plot
#EDUCATION
ed_plot <- ggplot(data = mydata, mapping = aes(x = education)) +
geom_bar(color = "black", fill = "purple") + ggtitle("Education") + theme(plot.title = element_text(hjust = 0.5))
ed_plot
#DEFAULT
default_plot <- ggplot(data = mydata, mapping = aes(x = default)) +
geom_bar(color = "black", fill = "violet") + ggtitle("Default Status") + theme(plot.title = element_text(hjust = 0.5))
default_plot
#HOUSING
housing_plot <- ggplot(data = mydata, mapping = aes(x = housing)) +
geom_bar(color = "black", fill = "turquoise") + ggtitle("Housing Loan Status") + theme(plot.title = element_text(hjust = 0.5))
housing_plot
#LOAN
loan_plot <- ggplot(data = mydata, mapping = aes(x = loan)) +
geom_bar(color = "black", fill = "pink") + ggtitle("Loan Status") + theme(plot.title = element_text(hjust = 0.5))
loan_plot
#CONTACT
contact_plot <- ggplot(data = mydata, mapping = aes(x = contact)) +
geom_bar(color = "black", fill = "darkblue") + ggtitle("Contact Method") + theme(plot.title = element_text(hjust = 0.5))
contact_plot
#OUTCOME
out_plot <- ggplot(data = mydata, mapping = aes(x = poutcome)) +
geom_bar(color = "black", fill = "darkgreen") + ggtitle("Previous Campaign Outcome") + theme(plot.title = element_text(hjust = 0.5))
out_plot
#Y
y_plot <- ggplot(data = mydata, mapping = aes(x = y)) +
geom_bar(color = "black", fill = "darkred") + ggtitle("Campaign Outcome") + theme(plot.title = element_text(hjust = 0.5))
y_plot
######### MACHINE LEARNING MODEL IMPLEMENTATION ##########
#DECISION TREES- RANDOM FOREST
set.seed(1)
N <- nrow(mydata)
training <- sample(1:N, 0.7*N)
testing <- seq(1:N)[-training]
rf <- randomForest(y ~ .,data=df, mtry = 4, subset = training, importance=TRUE)
summary(rf)
yhat.rf <- predict(rf,df[testing,])
df.testing <- df[testing,"y"]
yhat.predict <- predict(rf, df[testing,])
importance(rf)
print(rf)
# Plotting importance
varImpPlot(rf)
# ROC Curve Generation
pred <- predict(rf, newdata = df[testing,],type = "prob")
perf <- prediction(pred[,2], df[testing,]$y)
auc = performance(perf,"auc")
#auc
pred1 <- performance(perf, "tpr", "fpr")
plot(pred1, main = "ROC Curve for RF MODEL", col = 2, lwd = 2)
abline(a = 0, b = 1, lwd = 2, lty = 2, col = "gray")
#Balancing the data
table(df[training,]$y)
control = trainControl(
method = 'cv',
number = 10,
classProbs = TRUE,
summaryFunction = twoClassSummary
)
train_smote <- SMOTE(y ~ ., df[training,], perc.over = 100, perc.under = 200)
table(train_smote$y)
rf <- randomForest(y ~ .,data=train_smote, mtry = 4, importance=TRUE)
summary(rf)
yhat.rf <- predict(rf,df[testing,])
df.testing <- df[testing,"y"]
yhat.predict <- predict(rf, df[testing,])
importance(rf)
print(rf)
# Plotting importance
varImpPlot(rf)
# ROC Curve Generation
pred <- predict(rf, newdata = df[testing,],type = "prob")
perf <- prediction(pred[,2], df[testing,]$y)
auc = performance(perf,"auc")
#auc
pred1 <- performance(perf, "tpr", "fpr")
plot(pred1, main = "ROC Curve for RF MODEL", col = 2, lwd = 2)
abline(a = 0, b = 1, lwd = 2, lty = 2, col = "gray")