-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1.Predict Bank Mktg Campaign Response-Logistic Regression & Decision Tree Models.R
More file actions
1015 lines (729 loc) · 31.8 KB
/
1.Predict Bank Mktg Campaign Response-Logistic Regression & Decision Tree Models.R
File metadata and controls
1015 lines (729 loc) · 31.8 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############Customer response prediction using Logistic Regression & Decision Trees ################
# The classification goal is to predict if the client will subscribe (yes/no) a
# term deposit (variable y).
# In this analysis we will be using past data available on the
# demography, bank details, and transaction patterns of customers who have
# responded and not responded to a campaign, as training data to
# predict the probability if a customer will respond to the campaign. With this information we can
# inform marketing on how to more effectively target customers with marketing.
######Load libraries###########
library(rpart)
library(psych)
library(corrplot)
library(cowplot)
library(tree)
library(caret)
library(rpart)
library(rattle)
library(rpart.plot)
library(RColorBrewer)
library(tidyverse)
library(lubridate)
#All datasets available via UCI repository
browseURL("https://archive.ics.uci.edu/ml/datasets/Bank+Marketing")
##########Import Data###################
getwd() #check working directory
#change working directory if needed
#setwd("/Users/anitaowens/R_Programming/UCI Machine Learning Repo/Bank Marketing Data") #change working directory
getwd() #check results
#Import the full dataset
bank_data <- read.csv("~/Documents/GitHub/Machine-Learning-R/Machine-Learning-R/datasets/bank-full.csv", sep = ';')
#full data for comparison
#df.orig <- read.csv("bank-additional-full.csv", sep = ';')
head(bank_data)
glimpse(bank_data)
table(bank_data$default)
table(bank_data$loan)
table(bank_data$campaign)
table(bank_data$y) #the variable we're trying to predict
table(bank_data$housing)
######### Encoding - Recode binary variables into 0/1 #####
bank_data <- bank_data %>%
mutate(default = ifelse(default == "no", 0,1),
housing = ifelse(housing == "no", 0,1),
loan = ifelse(loan == "no", 0,1),
y = ifelse(y == "no", 0,1)) #y is now the variable we want to predict
#check results
str(bank_data)
#Convert all character variables into factor in one line:
bank_data <- bank_data %>% mutate_if(is.character, factor)
glimpse(bank_data)
#factor numeric variables
bank_data$default<- as.factor(bank_data$default)
bank_data$housing<- as.factor(bank_data$housing)
bank_data$loan<- as.factor(bank_data$loan)
#bank_data$y<- as.factor(bank_data$y)
glimpse(bank_data) #check results
#Variable 13 - pdays: number of days that passed by after the client
#was last contacted from a previous campaign (numeric; 999 means client was not previously contacted
max(bank_data$pdays)
min(bank_data$pdays)
hist(bank_data$pdays)
########## Transformation: Working with dates #############
str(bank_data$month)
#add dummy year & day to dataframe since we don't have this information
bank_data$year <- as.numeric("2014")
bank_data$day <- as.numeric("01")
#create new variable for campaign date
bank_data <- bank_data %>%
unite("campaign_date", day, month, year, sep = "")
#check results
table(bank_data$campaign_date)
#Now we need to reformat this
str(bank_data$campaign_date)
bank_data$campaign_date <- as.Date(bank_data$campaign_date, "%d%b%Y")
#check results
str(bank_data$campaign_date)
#Let's pull floor month and day back into dataframe as objects
bank_data$month <- lubridate::floor_date(bank_data$campaign_date, "month")
table(bank_data$month)
#check columns
names(bank_data)
str(bank_data)
########## Baseline Response Rate & Segments Comparison #############
#check y variable
str(bank_data$y)
prop.table(table(bank_data$y))
conversion_rate <- 0.1126542
#data frame grouped by job
job.df <- bank_data %>%
group_by(job) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
job.df #check results
#check conversions colum
sum(job.df$total_resp)
sum(job.df$total_count)
#add highlight flag column
job.df <- job.df %>%
mutate(highlight_flag =
ifelse(response_rate > conversion_rate, 1, 0))
#check results
head(job.df$highlight_flag)
#plot response rate by job
(p1job <- ggplot(data=job.df, aes(x=reorder(job, response_rate), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
labs(x = ' ', y = 'Response Rate', title = str_c("Response rate by job type")) +
theme(legend.position = "none"))
#Insights: Students, retired, unemployed, & admin
#professions have higher response rates.
#data frame grouped by marital status
marital.df <- bank_data %>%
group_by(marital) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
marital.df #check results
#check conversions colum
sum(marital.df$total_resp)
sum(marital.df$total_count)
#add highlight flag column
marital.df <- marital.df %>%
mutate(highlight_flag =
ifelse(response_rate > conversion_rate, 1, 0))
#plot response rate by marital status
(p2marstatus<-ggplot(data=marital.df, aes(x=reorder(marital, response_rate), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
labs(x = ' ', y = 'Response Rate', title = str_c("Response rate by marital status")) +
theme(legend.position = "none"))
##Insights: singles response at much higher rate, but there is an
#even larger category of unknowns so not sure how reliable this
#variable is
#data frame grouped by education
educ.df <- bank_data %>%
group_by(education) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
educ.df #check results
#check conversions colum
sum(educ.df$total_resp)
sum(educ.df$total_count)
#add highlight flag column
educ.df <- educ.df %>%
mutate(highlight_flag =
ifelse(response_rate > conversion_rate, 1, 0))
#plot response rate by education
(p3educ<-ggplot(data=educ.df, aes(x=reorder(education, response_rate), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
labs(x = 'Education', y = 'Response Rate', title = str_c("Education")) +
theme(legend.position = "none"))
##Insights: Illiterates, unknown, and university degrees
#respond at a higher rate
#data frame grouped by contact
contact.df <- bank_data %>%
group_by(contact) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
contact.df #check results
#check conversions colum
sum(contact.df$total_resp)
sum(contact.df$total_count)
#add highlight flag column
contact.df <- contact.df %>%
mutate(highlight_flag =
ifelse(response_rate > conversion_rate, 1, 0))
#plot response rate by contact
(p4cont <- ggplot(data=contact.df, aes(x=reorder(contact, response_rate), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
labs(x = 'contact', y = 'Response Rate', title = str_c("Contact information")) +
theme(legend.position = "none"))
#Insights: Higher response rate via cellular
#data frame grouped by month
month.df <- bank_data %>%
group_by(month) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
month.df #check results
#check conversions colum
sum(month.df$total_resp)
sum(month.df$total_count)
#add highlight flag column
month.df <- month.df %>%
mutate(highlight_flag =
ifelse(response_rate > conversion_rate, 1, 0))
#plot response rate by month
(p5month <- ggplot(data=month.df, aes(x=factor(month), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
# theme(axis.text.y = element_text(angle = 0)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
ylim(c(0, 1)) +
labs(x = 'month', y = 'Response Rate', title = str_c("By month")) +
theme(legend.position = "none"))
#The response rates are quite high for certain months
#Sep, Oct & Dec & Feb, March, April
#data frame grouped by poutcome
poutcome.df <- bank_data %>%
group_by(poutcome) %>%
summarize(total_count = n(),
total_resp = sum(y)) %>%
mutate(response_rate = total_resp/total_count)
poutcome.df #check results
#check conversions colum
sum(poutcome.df$total_resp)
sum(poutcome.df$total_count)
#add highlight flag column
poutcome.df <- poutcome.df %>%
mutate(highlight_flag =
ifelse(response_rate == max(response_rate), 1, 0))
poutcome.df$highlight_flag
#plot response rate by poutcome
(p6outcome <- ggplot(data=poutcome.df, aes(x=reorder(poutcome, response_rate), y=response_rate,
fill = factor(highlight_flag))) +
geom_bar(stat="identity") +
geom_hline(yintercept=conversion_rate, linetype="dashed", color = "black") +
theme(axis.text.x = element_text(angle = 90)) +
coord_flip() +
scale_fill_manual(values = c('#595959', 'red')) +
labs(x = ' ', y = 'Response Rate', title = str_c("Outcome of the previous marketing campaign")) +
theme(legend.position = "none"))
#The response rate is 64% (through the roof) if the customer had responded positively
#to a previous marketing campaign. Is this significant?
#use chi-square test
chisq<- chisq.test(bank_data$y, bank_data$poutcome)
chisq
if(chisq$p.value < 0.05) {
print("Significant")
} else {
print("Not significant")
}
plot_grid(p1job, p2marstatus, p3educ, p4cont, p5month, p6outcome, labels = "AUTO")
###### Univariate Analysis: Histogram plots for important independent numeric variables##
#?colors(distinct = FALSE)
p1 <- ggplot(data = bank_data, aes(x = age))+
geom_histogram(fill = "lightblue", binwidth = 5, colour = "black") +
geom_vline(aes(xintercept = median(age)), linetype = "dashed")
p2 <- ggplot(data = bank_data, aes(x = balance))+
geom_histogram(fill = "blue", colour = "black") +
geom_vline(aes(xintercept = median(balance)), linetype = "dashed")
# campaign: number of contacts performed during this campaign and for this
# client (numeric, includes last contact)
p3 <- ggplot(data = bank_data, aes(x = campaign))+
geom_histogram(fill = "red", binwidth = 3, colour = "black") +
geom_vline(aes(xintercept = median(campaign)), linetype = "dashed")
# duration: last contact duration, in seconds (numeric). Important note: this
# attribute highly affects the output y (e.g., if duration=0 then y='no').
# Yet, the duration is not known before a call is performed. Also, after the end
# of the call y is obviously known. Thus, this input should only be included for
# benchmark purposes and should be discarded if the intention is to have a
# realistic predictive model.
p4 <- ggplot(data = bank_data, aes(x = duration))+
geom_histogram(fill = "springgreen", binwidth = 365, colour = "black") +
geom_vline(aes(xintercept = median(duration)), linetype = "dashed")
# pdays: number of days that passed by after the client was last contacted from
# a previous campaign (numeric; 999 means client was not previously contacted)
p5 <- ggplot(data = bank_data, aes(x = pdays))+
geom_histogram(fill = "yellow", binwidth = 365, colour = "black") +
geom_vline(aes(xintercept = median(pdays)), linetype = "dashed")
# previous: number of contacts performed before this campaign and for this
# client (numeric)
p6 <- ggplot(data = bank_data, aes(x = previous))+
geom_histogram(fill = "slateblue1", binwidth = 1, colour = "black") +
geom_vline(aes(xintercept = median(previous)), linetype = "dashed")
plot_grid(p1, p2, p3, p4, p5, p6, labels = "AUTO")
#Just to get a sense of the data
#####Univariate Analysis: Boxplots for important independent numeric variables ######
#age on y axis
ggbi1 <- ggplot(data = bank_data, aes(x = job, y = age, fill = job))+geom_boxplot() + stat_summary(fun=mean, geom="point", shape=20, size=14, color="red", fill="red")
ggbi2 <- ggplot(data = bank_data, aes(x = marital, y = age, fill = marital))+geom_boxplot()
ggbi3 <- ggplot(data = bank_data, aes(x = housing, y = age, fill = housing))+geom_boxplot()
ggbi4 <- ggplot(data = bank_data, aes(x = default, y = age, fill = default))+geom_boxplot()
plot_grid(ggbi1, ggbi2, ggbi3, ggbi4, labels = "AUTO")
#unsurprisingly retired customers skew older
#the avg age across the different professions are pretty similar (middle-aged) with
#the exception of the student segment with an avg age of 26
aggregate(bank_data$age, by=list(bank_data$job), FUN = summary)
#loan on x axis
ggbi5 <- ggplot(data = bank_data, aes(x = loan, y = age, fill = loan))+geom_boxplot()
ggbi6 <- ggplot(data = bank_data, aes(x = loan, y = duration, fill = loan))+geom_boxplot()
plot_grid(ggbi5, ggbi6, labels = "AUTO")
#no difference to response based on age if they already had a loan or duration
#response variable on x axis - numeric variables on y axis
ggbi7 <- ggplot(data = bank_data, aes(x = y, y = age, fill = y))+geom_boxplot()
ggbi8 <- ggplot(data = bank_data, aes(x = y, y = balance, fill = y))+geom_boxplot()
plot_grid(ggbi7, ggbi8, labels = "AUTO")
##### Univariate Analysis: Barplots for important categorical variables (x) ######
# Don't map a variable to y
ggplot(bank_data, aes(x=factor(job)))+
geom_bar(stat="count", width=0.7, fill="sienna1")+
theme_minimal()
ggplot(bank_data, aes(x=job))+
geom_bar(stat="count", width=0.7, fill="steelblue")+
theme_minimal()
ggplot(bank_data, aes(x=education))+
geom_bar(stat="count", width=0.7, fill="salmon4")+
theme_minimal()
# default: has credit in default? (categorical: 'no','yes','unknown')
ggplot(bank_data, aes(x=default))+
geom_bar(stat="count", width=0.7, fill="red4")+
theme_minimal()
#housing: has housing loan? (categorical: 'no','yes','unknown')
ggplot(bank_data, aes(x=housing))+
geom_bar(stat="count", width=0.7, fill="plum")+
theme_minimal()
#loan: has personal loan? (categorical: 'no','yes','unknown')
ggplot(bank_data, aes(x=loan))+
geom_bar(stat="count", width=0.7, fill="yellowgreen")+
theme_minimal()
#contact: contact communication type (categorical: 'cellular','telephone')
ggplot(bank_data, aes(x=contact))+
geom_bar(stat="count", width=0.7, fill="wheat1")+
theme_minimal()
# poutcome: outcome of the previous marketing campaign (categorical:
# 'failure','nonexistent','success')
ggplot(bank_data, aes(x=poutcome))+
geom_bar(stat="count", width=0.7, fill="violetred")+
theme_minimal()
##########Bivariate analysis: Boxplots for y (as x variable) vs important numeric (y variables) ##########
bi1 <- ggplot(data = bank_data, aes(x = factor(y), y = age, fill = y))+geom_boxplot()
bi2 <- ggplot(data = bank_data, aes(x = factor(y), y = balance, fill = y))+geom_boxplot()
bi3 <- ggplot(data = bank_data, aes(x = factor(y), y = duration, fill = y))+geom_boxplot()
plot_grid(bi1, bi2, bi3, labels = "AUTO")
# When we look at the data visualization from bivariate analysis of numeric
# variables against the categorical y variable, we get the following
# insights:
# -AVG Duration of last contact seems to be the only variable that is higher among responders.
#We can infer that higher duration suggests there is a higher response rate,but we don't know if this
#is significant yet
bi3
# duration: last contact duration, in seconds (numeric). Important note: this
# attribute highly affects the output y (e.g., if duration=0 then y='no').
# Yet, the duration is not known before a call is performed. Also, after the end
# of the call y is obviously known. Thus, this input should only be included for
# benchmark purposes and should be discarded if the intention is to have a
# realistic predictive model.
#######Bivariate analysis: Stacked Bar plot for Categorical x variables vs y (y variable)############
# Stacked barplot with multiple groups
sb1 <- ggplot(data=bank_data, aes(x=job, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb2 <- ggplot(data=bank_data, aes(x=marital, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb3 <- ggplot(data=bank_data, aes(x=education, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb4 <- ggplot(data=bank_data, aes(x=default, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb5 <- ggplot(data=bank_data, aes(x=housing, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb6 <- ggplot(data=bank_data, aes(x=loan, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
sb7 <- ggplot(data=bank_data, aes(x=month, y=as.factor(y), fill=as.factor(y))) +
geom_bar(stat="identity")
plot_grid(sb1, sb2, sb3, labels = "AUTO")
plot_grid(sb4, sb5, sb6, sb7, labels = "AUTO")
########What were the campaign parameters?############
#last contact day of the week
table(bank_data$day)
#last contact month of the year
table(bank_data$month)
# number of contacts performed during this campaign and for this client (numeric, includes last contact)
table(bank_data$campaign)
# - poutcome: outcome of the previous marketing campaign (categorical: 'failure','nonexistent','success')
table(bank_data$poutcome)
## related with the last contact of the current campaign:
table(bank_data$contact)
########## Correlation Analysis & Matrix ############
#Threshold for correlation
#Cohen's rule of thumb (if data are normally distributed)
# r = 0.1 weak
# r = 0.3 medium
# r = 0.5 strong
# Build Correlation Matrix structure:
bank_data %>%
select_if(is.numeric) %>%
cor() %>%
corrplot(type = "upper", insig = "blank", diag = FALSE, addCoef.col = "grey")
#duration appears to have a moderate relationship to the response variable
#previous and pdays appear to have a moderate linear relationshp with each other
#re-build as matrix
corr_matrix <- bank_data %>%
select_if(is.numeric) %>%
cor()
round(corr_matrix,2)
####Observations
tab <- table(bank_data$y)
prop.table(tab) #12% baseline response rate
# 2-Way Cross Tabulation
library(gmodels)
CrossTable(bank_data$job, bank_data$y, digits=2, prop.c = TRUE,
prop.r = TRUE, prop.t = FALSE, chisq = FALSE, format = "SAS", expected = FALSE)
# -We see that More retired responents responded to the campaign when
#compared to other professionals.
#retired responded at a rate of 25%
#students - 31%
#Admins also at 29%
# -We see more single individuals responded to the campaign 14% response rate
CrossTable(bank_data$marital, bank_data$y, digits=2, prop.c = TRUE,
prop.r = TRUE, prop.t = FALSE, chisq = FALSE, format = "SAS", expected = FALSE)
#Not Having a housing loan
CrossTable(bank_data$housing, bank_data$y, digits=2, prop.c = TRUE,
prop.r = TRUE, prop.t = FALSE, chisq = FALSE, format = "SAS", expected = FALSE)
#-Respondents who didn't already have a house loan were roughly the same as those who already had a housing loan, but the distribution
#of the groups were 45% of respondents had no mortgage vs 55% respondents)
#Not Having a personal loan
CrossTable(bank_data$loan, bank_data$y, digits=2, prop.c = TRUE,
prop.r = TRUE, prop.t = FALSE, chisq = FALSE, format = "SAS", expected = FALSE)
##-Respondents who didn't already have a personal loan were just
#slightly higher to respond (11% vs 10%), but this is hardly significant
##(82% had no prior personal loan)
####### Revisit pdays variable ############
#Variable 13 - pdays: number of days that passed by after the client
#was last contacted from a previous campaign (numeric; 999 means client was not previously contacted
#Let's create a binary variable
summary(bank_data$pdays)
#since there were no 999 in dataset we will leave variable as is
bank_data %>%
filter(pdays == "999")
#######ML Model 1: Logistic Regression: Split dataset into development (train) and holdout (validation or test) sets#######
glimpse(bank_data$y)
#factor y variable if needed
bank_data$y <- as.factor(bank_data$y)
#check results
str(bank_data$y)
#month variable also
bank_data$month <- as.factor(bank_data$month)
#check results
str(bank_data$month)
str(bank_data)
#import library and set seed for reproducibility
library(caTools)
set.seed(123)
## split the dataset into training and test samples at 70:30 ratio
split <- sample.split(bank_data$y, SplitRatio = 0.7)
train_data <- subset(bank_data, split == TRUE)
test_data <- subset(bank_data, split == FALSE)
## Check if distribution of partition data is correct for the development dataset
prop.table(table(train_data$y))
prop.table(table(test_data$y))
# The prop.table output above confirms that the imbalanced dataset
# characteristic that we saw in the original dataset is maintained at the same
# proportions in the development and hold out samples as well. The training
# dataset is now ready to build the model.
############### Step 1: Build logistic regression model########################
library(glmnet)
# Use the glm() function in order to model the probability that a customer will
# respond to campaign by using a logistic regression. Include every
# explanatory variable of the dataset and specify the data that shall be used.
# Specify the argument family.
# Then, extract the coefficients and transform them to the odds ratios.
#Step 1: Run Logistic Regression on the train data
#not running with unneeded variables
model_01_logreg <- glm(y ~ . -campaign_date,
family = binomial, data = train_data)
# Take a look at the model
summary(model_01_logreg)
#Coefficients:
# -Negative coefficients that are statistically significant
#correspond to variables that are negatively correlated to the odds (and hence
#to the probability) of a positive outcome
# - Positive coefficients that are statistically significant are
# positively correlated to
#the odds of a positive outcome.
# Take a look at the odds
coefsexp <- coef(model_01_logreg) %>% exp()%>% round(2)
coefsexp
# Odds ratio, represents which group has better odds of success,
# and it’s given by calculating the ratio of odds for each group
# If we know the coefficients of independent variables Xs and the intercept a, we can predict the probability.
########## Step 1a: Variable Inflation Factor check##############################
# In general, I want my R2 to be as high as possible.
#
# R2 & F-test Is a test of the overall fit of the model whether or not r2 is
# equal to 0. Does one or more regressors has significant explantory power.
#Overfitting
#Methods to avoid overfitting
# -Keep your model lean
# -AIC() from stats package --controls for overfitting
# -stepAIC() from MASS package
# -out-of-sample model validation
# -cross-validation
library(car)
vif(model_01_logreg)
# Feature (x) variables with a VIF value above 5 indicate high degree of
# multi-collinearity.
#multicollinearity does not bias coefficients; it inflates their standard errors.
# multicollinearity does not usually alter the
#interpretation of the coefficients of interest
#unless they lose statistical significance.
#Rule of thumb for interpreting VIF value
#if VIF of a variable is one, it means that it is
#not correlated with any of the other variables.
#No extremely high VIF's
#Insights from our logistic regression model
#Coefficients that are positive and significant:
# - marital status - single
# - education - tertiary
# - had responded to a previous campaign and it was a positive outcome
# - has balance
# - is retired or a student
#Coefficients that are negative and significant:
# - contacttelephone
# - campaign
# - has a personal loan
# - has mortgage loan
#what happens if we remove month
model_02_logreg <- glm(y ~ . -campaign_date -month,
family = binomial, data = train_data)
# Take a look at the model
summary(model_02_logreg)
#Let's compare the models we've run so far
anova(model_01_logreg, model_02_logreg, test="Chisq")
#Model 1 was not significant but model 2 is
# significant 95%.
library(lmtest)
lrtest(model_01_logreg, model_02_logreg)
#According to likelihood ratio test, model 2 is significantly better
########## Step 2: Overall significance of the model##############################
#Compare our best logistic regression model against null model
lrtest(model_02_logreg)
# We can see that the low p value indicates the model is highly significant i.e.
# the likelihood of a customer responding to the campaign (y) depends on
# independent x variables in the dataset.
########## Step 3: McFadden or pseudo R² interpretation##############################
# How to evaluate your model.
#
# With logistic regression there are several ways to go about fitting your model
#
# There are 3 pseudo r2 statistics:
# -McFadden
# -Cox & Snell
# -Nagelkerke
#
# Interpretation:
#
# reasonable if > 0.2
# good if > 0.4
# very good if > 0.5
##Goodness of fit packages & measures
library(pscl)
model_fit <- pR2(model_02_logreg)
print(model_fit)
#retrieve just the mcfadden r2 from model_fit
model_fit[4]
#pass the Mcfadden R2 into the function to get fit estimate
pseudoR_func <- function(pseudoR){
if(pseudoR >= 0.5){print("very good fit")}
else if(pseudoR >= 0.4 && pseudoR < 0.5) {print("good fit")}
else if (pseudoR >= 0.2 && pseudoR < 0.4) {print("reasonable fit")}
else{print("try again")}
}
pseudoR_func(model_fit[4])
#Albeit not perfect the fit is reasonable
########## Step 4: Individual coefficients significance and interpretation#############################
#library(coef.lmList)
summary(model_02_logreg)
#prints confidence intervals
exp(confint(model_02_logreg))
# The Odds Ratio and Probability of each x variable is calculated based on the
# formulae, Odds Ratio = exp(Co-efficient estimate) Probability = Odds Ratio /
# (1 + Odds Ratio)
#plot coefficients on odds ratio
library(sjPlot)
plot_model(model_02_logreg, vline.color = "red",
sort.est = TRUE, show.values = TRUE)
######Step 5: Model Accuracy#################
pred <- predict(model_02_logreg, test_data, type = "response") #predict using test data
#check results
head(pred)
predicted <- round(pred) #>0.5 will convert to 1
#contingency table
contingency_tab <- table(test_data$y, predicted)
contingency_tab
#sum the diagnals for the percentage accuracy classified
sum(diag(contingency_tab))/sum(contingency_tab) *100
# Confusion Matrix using the caret package to validate above
caret::confusionMatrix(contingency_tab)
#Our model leads to rougly 90% of correct predictions
######### Step 6: Model Diagnostics - ROC/AUC/K-Fold CV #################
#Plot ROC Curve & Calculate AUC area
library(ROCR)
#ROC Curves are useful for comparing classifiers
#check data structures first
typeof(predicted)
typeof(test_data$y)
pr <- prediction(pred, test_data$y)
prf <- performance(pr, measure = "tpr", x.measure = "fpr")
plot(prf)
#The ideal ROC curve hugs the top left corner, indicating a high
#true positive rate and a low false positive rate.
#True positive rate on y-axis
#False positive rate on the x-axis
#The larger the AUC, the better the classifier
#The AUC line is insufficient to identify a best model
#It's used in combination with qualitative examination
#of the ROC curve
auc <- performance(pr, measure = "auc")
auc
# AUC is 0.8781366 - definitely a B
as.numeric(performance(pr, measure = "auc")@y.values)
#AUC Interpretation
#A: Outstanding = 0.9 to 1.0
#B: Excellent/Good = 0.8 to 0.9
#C: Acceptable/Fair = 0.7 to 0.8
#D: Poor = 0.6 to 0.7
#E: No Discrimination = 0.5 to 0.6
#now let's just plot the ROC and look at true positive vs false positive
perf <- performance (pr, measure = 'tpr', x.measure = "fpr")
plot(perf) + abline(a=0, b=1, col = 'red') # the red line is randomness
## Cross-validation
library(C50)
library(irr)
#create 10 folds
set.seed(123)
folds <- createFolds(bank_data$y, k = 10)
#peek at the results
str(folds)
######### Step 7: Variable Importance #################
#Let's look at the absolute value of the t-statistic for
#each model parameter using caret package
varImp(model_02_logreg)
#########Summary of Conclusions from Log Regression Model#################
# Marketing should contact customers with these characteristics
# - marital status - single
# - education - tertiary
# - had responded to a previous campaign and it was a positive outcome
# - has balance
# - is retired or a student
#Marketing should not contact customers with these characteristics
# - has a personal loan
# - has mortgage loan
#The bank should limit the number of contacts it has with
# a customer. What's most likely happening is that customers
#may ignore communication from the bank if its gets too many.
############ML Model 2: Decision Tree ###########
#######Step 1: Test/Train Split on trimmed dataset###################
bank_data2 <- bank_data %>%
select(-campaign_date, -job, -pdays,
-poutcome, -month)
#check results
names(bank_data2)
set.seed(123)
## split the dataset into training and test samples at 70:30 ratio
split2 <- sample.split(bank_data2$y, SplitRatio = 0.7)
train_data2 <- subset(bank_data2, split == TRUE)
test_data2 <- subset(bank_data2, split == FALSE)
#######Step 2: Build the decision tree ###################
# build the simplest classification tree model with C50
library(C50)
model_03_tree <- C5.0(x = train_data2[,-12], y = train_data2$y)
# The summary() function lists the variables that are used as internal nodes in
# the tree, the number of terminal nodes, and the (training) error rate.
summary(model_03_tree)
#Error rate is 6.8%
#re-build tree without duration variable
model_04_tree <- C5.0(x = train_data2[,c(1:8, 10:11)], y = train_data2$y)
summary(model_04_tree)
################Step 3 : Evaluate Model Performance & Accuracy #############
#Model 3
# Generate predicted classes using the model object
class_prediction <- predict(object = model_03_tree,
newdata = test_data2,
type = "class")
# Calculate the confusion matrix for the test set
confusionMatrix(data = class_prediction,
reference = test_data2$y)
#Model 4
# Generate predicted classes using the model object
class_prediction2 <- predict(object = model_04_tree,
newdata = test_data2,
type = "class")
# Calculate the confusion matrix for the test set
confusionMatrix(data = class_prediction2,
reference = test_data2$y)
#The data argument takes a vector of predicted class labels on a test set
#the reference argument a vector of the true class labels
#Both models gave the same amount of accuracy
#but model 2 had more Type I and Type II errors
#What about the Kappa?
#Interpretation of Cohen's Kappa
#Model 1 had a higher Kappa .5043 vs. 0.2943
##### Step 4: Can we improve the model? #####
#Boosting the tree algorithm
model_05_boost <- C5.0(x = train_data2[,-13], y = train_data2$y, trials = 10)
# The summary() function lists the variables that are used as internal nodes in
# the tree, the number of terminal nodes, and the (training) error rate.
summary(model_05_boost)
# Generate predicted classes using the model object
class_prediction3 <- predict(object = model_05_boost,
newdata = test_data2,
type = "class")
confusionMatrix(data = class_prediction3,
reference = test_data2$y)
CrossTable(test_data2$y, class_prediction3)
#this model predicted correctly 46% - we didn't really improve the model
# significantly with the boosted model