-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.Rmd
496 lines (390 loc) · 14 KB
/
Bank.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
---
title: "Marketing campaign to sell term deposits: Predicting Target Customers"
author: "Executed by Neha More"
date: "November 20, 2017"
output:
html_document: default
---
##Problem Statement:
A Portugese bank is rolling out term deposit for its customers. They have in the past connected to their customer base through phone calls. Results for these previous campaigns were recorded and have been provided to the current campaign manager to use the same in making this campaign more effective.
Challenges that the manager faces are following:
. Customers have recently started to complain that bank's marketing staff bothers them with irrelevant product calls and this should immediately stop
. There is no prior framework for her decide and choose which customer to call and which one to leave alone
She has decided to use past data to automate this decision, instead of manually choosing through each and every customer. Previous campaign data which has been made available to her; contains customer characteristics , campaign characteristics, previous campaign information as well as whether customer ended up subscribing to the product as a result of that campaign or not. Using this she plans to develop a statistical model which given this information predicts whether customer in question will subscribe to the product or not. A successful model which is able to do this, will make her campaign efficiently targeted and less bothering to uninterested customers.
##Aim:
To Build a machine learning predictive model and predict which customers should be targeted for rolling out term deposits by bank.
Evaluation Criterion :KS score on test data. larger KS, better Model
##Data:
We have given you two datasets , bank-full_train.csv and bank-full_test.csv . You need to use data bank-full_train to build predictive model for response variable "y". bank-full_test data contains all other factors except "y", you need to predict that using the model that you developed and submit your predicted values in a csv files.
##Data dictionary:
###Variables : Definition: Type and their categories
Each row represnts characteristic of a single customer . Many categorical data has
been coded to mask the data, you dont need to worry about their exact meaning
1 - age (numeric)
2 - job : type of job (categorical: "admin.","unknown","unemployed","management","housemaid","entrepre neur","student", "blue-collar", "self-employed","retired","technician", "services")
3 - marital : marital status (categorical: "married","divorced","single"; note: "divorced" means divorced or widowed)
4 - education (categorical: "unknown","secondary","primary","tertiary")
5 - default: has credit in default? (binary: "yes","no")
6 - balance: average yearly balance, in euros (numeric)
7 - housing: has housing loan? (binary: "yes","no")
8 - loan: has personal loan? (binary: "yes","no")
Related with the last contact of the current campaign:
9 - contact: contact communication type (categorical: "unknown","telephone","cellular")
10 - day: last contact day of the month (numeric))
Direct Marketing Campaign: Details and Phase I Tasks
11 - month: last contact month of year (categorical: "jan", "feb", "mar", . . . , "nov", "dec")
12 - duration: last contact duration, in seconds (numeric)
other attributes: 13 - campaign: number of contacts performed during this campaign and for this client (numeric, includes last contact)
14 - pdays: number of days that passed by after the client was last contacted from a previous campaign (numeric, -1 means client was not previously contacted)
15 - previous: number of contacts performed before this campaign and for this client (numeric)
16 - poutcome: outcome of the previous marketing campaign (categorical: "unknown","other","failure","success")
**Output variable (desired target):**
**17 - y - has the client subscribed a term deposit? (binary: "yes","no")**
##Methodology:
We will build a Logistic regression model to predict the response variable "y" (whether the client subscribed to a term deposit or No.)
Step 1: Imputing NA values in the datasets.
Step 2:Data Preparation: Grouping similar category variables and making dummies.
Step 3: Model Building( LOGISTIC REGRESSION )
Step 4. Finding Cutoff value and Perfomance measurements of the model.(Sensitivity, Specificity, Accuracy)
Step 5.Predict the final output on test dataset.(whether the client subscribe or no to term deposit)
Step 6:Creating confusion matrix and finding how good our model is. (by predicting on test_25 dataset)
####Initial setup
loading library dplyr
```{r message=FALSE}
library(dplyr)
```
```{r}
setwd("C:\\Users\\INS15R\\Documents\\R latest\\R EDVANCER\\Industry Based Projects\\Industry-Based-Projects-Edvancer-Eduventures")
getwd()
```
Reading train and test datasets:
```{r}
train=read.csv("bank-full_train.csv",stringsAsFactors = FALSE,header = T ) #31647,18
test=read.csv("bank-full_test.csv",stringsAsFactors = FALSE,header = T ) #13564,17
```
###Step 1: Imputing NA values in the datasets.
```{r}
apply(train,2,function(x)sum(is.na(x)))
```
There exist no NA values in train dataset.
```{r}
apply(test,2,function(x)sum(is.na(x)))
```
There exist no NA values in test dataset.
###Step 2:Data Preparation
####Combining both train n test datasets prior to data preparation.
```{r}
test$y=NA
train$data='train'
test$data='test'
all_data=rbind(train,test)
apply(all_data,2,function(x)sum(is.na(x)))
```
Lets see the structure and datatypes of the combined dataset.
```{r}
glimpse(all_data) #45211,19var
```
####Creating dummy variables by combining similar categories for variable job(char type)
```{r}
t=table(all_data$job)
sort(t)
final=round(prop.table(table(all_data$job,all_data$y),1)*100,1)
final
s=addmargins(final,2) #add margin across Y
sort(s[,1])
View(s)
```
```{r}
all_data=all_data %>%
mutate(job_1=as.numeric(job %in% c("self-employed","unknown","technician")),
job_2=as.numeric(job %in% c("services","housemaid","entrepreneur")),
job_3=as.numeric(job %in% c("management","admin")),
job_4=as.numeric(job=="student"),
job_5=as.numeric(job=="retired"),
job_6=as.numeric(job=="unemployed")) %>%
select(-job)
glimpse(all_data)
```
####Making dummies for variable marital
```{r}
t=table(all_data$marital)
sort(t)
```
```{r}
all_data=all_data %>%
mutate(divorced=as.numeric(marital %in% c("divorced")),
single=as.numeric(marital %in% c("single"))
) %>%
select(-marital)
glimpse(all_data)
```
####Making dummies for variable education
```{r}
t=table(all_data$education)
sort(t)
all_data=all_data %>%
mutate(edu_primary=as.numeric(education %in% c("primary")),
edu_sec=as.numeric(education %in% c("secondary")),
edu_tert=as.numeric(education %in% c("tertiary"))
) %>%
select(-education)
glimpse(all_data)
```
####Making dummies for varible default
```{r}
table(all_data$default)
all_data$default=as.numeric(all_data$default=="yes")
```
####Making dummies for variable housing
```{r}
table(all_data$housing)
all_data$housing=as.numeric(all_data$housing=="yes")
glimpse(all_data)
```
####Making dummies for variable loan
```{r}
table(all_data$loan)
all_data$loan=as.numeric(all_data$loan=="yes")
glimpse(all_data)
```
####Making dummies for variable contact
```{r}
t=table(all_data$contact)
sort(t)
all_data=all_data %>%
mutate(co_cellular=as.numeric(contact %in% c("cellular")),
co_tel=as.numeric(contact %in% c("telephone"))
) %>%
select(-contact)
glimpse(all_data)
```
####Making dummies for variable month
```{r}
table(all_data$month)
#lets convert into percentage across months.
finalmnth=round(prop.table(table(all_data$month,all_data$y),1)*100,1)
sss=addmargins(finalmnth,2) #adding margin across Y
sort(sss[,1])
```
```{r}
#may taken as base var
all_data=all_data %>%
mutate(month_1=as.numeric(month %in% c("aug","jun","nov","jan","jul")),
month_2=as.numeric(month %in% c("dec","sep")),
month_3=as.numeric(month=="mar"),
month_4=as.numeric(month=="oct"),
month_5=as.numeric(month=="apr"),
month_6=as.numeric(month=="feb")) %>%
select(-month)
glimpse(all_data)
```
####Making dummies for variable outcome
```{r}
t=table(all_data$poutcome)
sort(t)
#unknown as base var
all_data=all_data %>%
mutate(poc_success=as.numeric(poutcome %in% c("success")),
poc_failure=as.numeric(poutcome %in% c("failure")),
poc_other=as.numeric(poutcome %in% c("other"))
)%>%
select(-poutcome)
glimpse(all_data)
```
####Thus data preparation is done and we will now seperate both test n train data.
```{r}
glimpse(all_data)
```
```{r}
table(all_data$y)
table(train$y)
all_data$y=as.numeric(all_data$y=="yes")
table(all_data$y)
glimpse(all_data)
```
Separating test and train:
```{r}
train=all_data %>%
filter(data=='train') %>%
select(-data) #31647,34
test=all_data %>%
filter(data=='test') %>%
select(-data,-y)
```
Lets view the structure of test n train datasets:
```{r}
glimpse(train) #31647,34
```
```{r}
glimpse(test) #13564,33
```
####now lets divide the train dataset in the ratio 75:25.
```{r}
set.seed(5)
s=sample(1:nrow(train),0.75*nrow(train))
train_75=train[s,] #23735,34
test_25=train[-s,]#7912,34
```
###Step 3: Model Building
####We will use train for logistic regression model building and use train_25 to test the performance of the model thus built.
####Lets build logistic regression model on train dataset.
```{r warning=FALSE}
library(car)
for_vif=lm(y~.,data=train)
summary(for_vif)
```
In order to take care of multi collinearity,we remove variables whose VIF>5,as follows:
```{r}
t=vif(for_vif)
sort(t,decreasing = T)[1:5]
```
Removing variable edu_sec
```{r}
for_vif=lm(y~.-edu_sec,data=train)
t=vif(for_vif)
sort(t,decreasing = T)[1:5]
```
```{r}
summary(for_vif)
```
Now lets remove edu-sec from train dataset
```{r}
colnames(train) #34var
fit_train=train %>%
select(-edu_sec)
#1 omited
colnames(fit_train) #33var including target(y)
```
Lets build model on fit_train dataset:
```{r}
fit=glm(y~.,family = "binomial",data=fit_train) #32 predictor var
summary(fit) #we get aic:14348
```
Now lets remove all variables whose p value is >0.05 using step function.
```{r}
fit=step(fit)
```
lets check the remaining significant variables
```{r}
names(fit$coefficients) #25 significant var
```
lets build final logistic model on significant variables on dataset fit_train
```{r}
fit_final=glm(y~balance + housing + loan + duration + campaign + ID +
job_3 + job_5 + divorced + single + edu_primary +
co_cellular + co_tel + month_1 + month_2 + month_3 + month_4 +
month_5 + month_6 + poc_success + poc_failure + poc_other ,data=fit_train,family="binomial")
```
```{r}
summary(fit_final)
```
```{r}
names(fit_final$coefficients)
```
*it shows: aic:14341 and 22 significant var in final model*
####Thus logistic regression model is successfully built.
####Now lets make predict scores
```{r}
train$score=predict(fit_final,newdata = train,type="response")
#score means Pi
```
lets see how the score (Pi ) behaves.
```{r}
library(ggplot2)
ggplot(train,aes(y=y,x=score,color=factor(y)))+
geom_point()+geom_jitter()
```
###Step 4. Finding Cutoff value and Perfomance measurements of the model.
lets find cutoff based on these probability scores.
```{r}
cutoff_data=data.frame(cutoff=0,TP=0,FP=0,FN=0,TN=0)
cutoffs=seq(0,1,length=100)
```
```{r}
for (i in cutoffs){
predicted=as.numeric(train$score>i)
TP=sum(predicted==1 & train$y==1)
FP=sum(predicted==1 & train$y==0)
FN=sum(predicted==0 & train$y==1)
TN=sum(predicted==0 & train$y==0)
cutoff_data=rbind(cutoff_data,c(i,TP,FP,FN,TN))
}
## lets remove the dummy data cotaining top row in data frame cutoff_data
cutoff_data=cutoff_data[-1,]
#we now have 100 obs in df cutoff_data
```
####lets calculate the performance measures:sensitivity,specificity,accuracy, KS and precision.
```{r}
cutoff_data=cutoff_data %>%
mutate(P=FN+TP,N=TN+FP, #total positives and negatives
Sn=TP/P, #sensitivity
Sp=TN/N, #specificity
KS=abs((TP/P)-(FP/N)),
Accuracy=(TP+TN)/(P+N),
Lift=(TP/P)/((TP+FP)/(P+N)),
Precision=TP/(TP+FP),
Recall=TP/P
) %>%
select(-P,-N)
```
lets view cutoff dataset:
```{r}
#View(cutoff_data)
```
####Lets find cutoff value based on ks MAXIMUM.
```{r}
KS_cutoff=cutoff_data$cutoff[which.max(cutoff_data$KS)]
KS_cutoff
```
####hence 0.1111111 is the cutoff value by ks max method.
###Step 5.Predict the final output on test dataset.(whether the client subscribe or no to term deposit)
####lets predict test scores
```{r}
test$score=predict(fit_final,newdata =test,type = "response")#on final test dataset.
```
####Predicting whether the client has subscribed or no in final test dataset.
```{r}
test$left=as.numeric(test$score>KS_cutoff)#if score is greater dan cutoff then true(1) else false(0)
table(test$left)
```
####Thus final prediction is as follows:
```{r}
test$leftfinal=factor(test$left,levels = c(0,1),labels=c("no","yes"))
table(test$leftfinal)
```
####writing into csv file final output test$leftfinal
```{r}
write.csv(test$leftfinal,"P5_sub_1.csv")
```
####**Thus 3396 customers out of 13564 subscribe to term deposit according to the model.**
###Step 6:Creating confusion matrix and find how good our model is (by predicting on test_25 dataset)
```{r}
test_25$score=predict(fit_final,newdata =test_25,type = "response")
```
```{r}
table(test_25$y,as.numeric(test_25$score>KS_cutoff))
table(test_25$y)
```
####here TP=770,TN=5888,FP=,FN=
Accuracy=(TP+TN)/(P+N):
```{r}
a=(770+5888)/7912
a
```
Hence error will be:
```{r}
1-a
```
####**Error is 15.85%.(according to ks method)
####Lets plot the ROC curve:
```{r warning=FALSE}
library(pROC)
roccurve=roc(test_25$y,test_25$score) #real outcome and predicted score is plotted
plot(roccurve)
```
Thus area under the ROC curve is:
```{r}
auc(roccurve) #0.9218
```
###Conclusion:
####*Thus the target no. of customers to be focused upon for term deposits by the bank are predicted successfully using logistic regression model with an accuracy of 84.15% using KS method. The KS score examined came out to be: 0.72/1.00[our model and predictions wer very good]*