-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChurn_Analytics.R
1013 lines (915 loc) · 41.9 KB
/
Churn_Analytics.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
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
# ML PROJECT: Customer Churn Behaviour Analytics .
# Data used: From USCI Repository, Kaggle
# Written By Mr. Murera Gisa.
# _________________________________
install.packages("patchwork")
install.packages("caret")
install.packages("vcd")
install.packages("lme4")
install.packages(c("corrplot","DMwR","InformationValue","ROCR","randomForest",
"xgboost","xgboostExplainer","ggmosaic","e1071","ranger",
"penalized","ggcorrplot","caTools","doMC"))
install.packages("ggthemes")
install.packages("vip")
library(tidyverse)
library(ggthemes)
library(patchwork)
library(caret)
library(gridExtra)
library(grid)
library(vcd)
library(knitr)
library(corrplot)
library(scales)
library(lme4)
library(DMwR)
library(InformationValue)
library(ROCR)
library(rpart)
library(randomForest)
library(xgboost)
library(xgboostExplainer)
library(MASS)
library(ggmosaic)
library(e1071)
library(ranger)
library(penalized)
library(rpart.plot)
library(ggcorrplot)
library(caTools)
library(doSNOW)
library(doMC)
library(DT)
library(plotly)
registerDoMC(cores=4)
#____________________________________________
#Setting working directory
setwd("C:/Users/Murera Gisa/Desktop/TWETTER&CHURN")
#_____________________________________
#Importing data
bankChurn <- read_csv("C:/Users/Murera Gisa/Desktop/TWEETER&CHURN/Churn_Modelling.csv")
View(bankChurn)
dim(bankChurn)
glimpse(bankChurn)
str(bankChurn)
#Tabulating (DT library for interactive and flexible table)
#_____________________________
xtable::xtable(bankChurn)# Generating latex script to tabulate the data
datatable(bankChurn[,-1], colnames = c('ID' = 1),class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;', 'Table 1: ',
htmltools::em("Source: Mgisa Churn Analytics."))
#setting header black
,options = list(autoWidth = FALSE, initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': 'blue'});",
"}")))
#Data Munging&Wrangling (CLeaning)
#______________________________
#1. Removing unwanted variables and converting other as factors for classification
bankChurn <- bankChurn %>%
dplyr::select(-RowNumber, -CustomerId, -Surname) %>% #remove unwanted column
mutate(Geography = as.factor(Geography),
Gender = as.factor(Gender),
HasCrCard = as.factor(HasCrCard),
IsActiveMember = as.factor(IsActiveMember),
Exited = as.factor(Exited),
Tenure = as.factor(Tenure),
NumOfProducts = as.factor(NumOfProducts))
#2. DESCRIPTIVE STATISTICS
#____________________________
library(skimr)
skimmed <- skim(bankChurn)
print(skimmed)
# Checking NA
sapply(bankChurn, function(x) sum(is.na(x)))
sum(is.na(bankChurn)) #Checking the total missing values in data
# Function for detecting NA observations:
na_rate <- function(x) {x %>% is.na() %>% sum() / length(x)}
sapply(bankChurn, na_rate)
#No missing values obtained
#EXPLORATION OF VARIABLES (FEATURES ENGINEERING)
#______________________________
#Overview of data
summary(bankChurn)
#________________________________________VISUALIZATION OF LABELS______________________
#CREATION OF THEME
blank_theme <- theme_bw() +
theme(
axis.title.x = element_text(angle = 360, face = "bold", colour = "red", size = 15),
axis.text.x = element_text(angle = 360, face = "bold", colour = "black", size = 12),
axis.title.y = element_text(angle = 90, face = "bold", colour = "red", size = 15),
axis.text.y = element_text(angle = 45, face = "bold", colour = "black", size = 12),
legend.position= "none",
#panel.border = element_blank(),
#panel.grid= element_blank(),
#axis.ticks = element_blank(),
plot.title = element_text(size=16, face="bold", color="forest green")
)
#Visualization of Output variables (Counting the customer status in data)
counting_cust_status <- bankChurn %>% count(Exited) %>%
mutate(Rate=round(prop.table(table(bankChurn$Exited))*100, digits = 2)) %>%
mutate(Churnlabels= as.factor(fct_recode(Exited, Non_Churned= "0",Churned = "1"))) %>%
ggplot(aes(x=Churnlabels , y= n, fill = Churnlabels)) +
geom_bar(stat = "identity", width = 0.8, show.legend = FALSE) + theme_bw() + blank_theme+
labs(x= "Customer Churn Status", y= "Customers' total number", caption = "Source: Mgisa Churn Analytics") +
scale_fill_manual(values = c("Non_Churned" = "blue", "Churned"= "cyan"), aesthetics = "fill")+
scale_x_discrete(limits = c("Non_Churned","Churned")) + ggtitle("BANK'S CUSTOMER STATUS") +
geom_text(aes(label= str_c(Rate,"%")),vjust= 4.5,size= 6, color= "black")
#+theme(legend.position= "top", axis.text.x = element_text(angle = 45, face = "bold", colour = "black", size = 15),axis.title.x= element_text(size = 12,face = "bold"), axis.title.y = element_text(angle = 90,vjust = 0.3,face = "bold"))
plotly::plotly_build(counting_cust_status)
#______________________________________________________
#TABULATING DATA
datatable(bankChurn, colnames = c('ID' = 1),class = 'cell-border stripe',
caption = htmltools::tags$caption(style = 'caption-side: bottom; text-align: center;',
'Table 2: ',
htmltools::em("Source: Mgisa Churn Analytics."))
#setting header black
,options = list(autoWidth = FALSE, initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}")))
#______________________________________________________________
#Tabulating the output (And compute the percentage of customers status)
table(bankChurn$Exited)
round(prop.table(table(bankChurn$Exited)),3) #Most of our customers did not churn
#Overall distribution of all variables
#_______________________________________________
#1. Histogram for continuous variables
HistCont<-bankChurn %>%
keep(is.numeric) %>%
gather() %>%
ggplot() +
geom_histogram(mapping = aes(x=value,fill=key), color="black") +theme_bw()+
facet_wrap(~ key, scales = "free") +
theme_minimal() + labs(x="Corresponding Value",y= "Total Numbers",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("CONTINUOUS VARIABLE DISTRIBUTION")+
theme(legend.position = 'none',
#axis.text.x = element_text(angle = 45, face = "bold", colour = "black",size = 15),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
HistCont
plotly_build(HistCont)
#_________________________________
#Correlation Matrix (Checking multicollineality)
numericVarName <- names(which(sapply(bankChurn, is.numeric)))
corr <- cor(bankChurn[,numericVarName], use = 'pairwise.complete.obs')
ggcorrplot(corr, lab = TRUE, title = "CORRELATION AMONG CONT. VARIABLES")
# Any high correlation between the continuous variables
#(i.e. no multicollinearity). So I'll keep all this continuous variables.
#_________________________________________
#2. Categorical Variables Distribution
HistCateg<- bankChurn %>%
dplyr::select(-Exited) %>%
keep(is.factor) %>%
gather() %>%
group_by(key, value) %>%
summarize(n = n()) %>%
ggplot() +
geom_bar(mapping=aes(x = value, y = n, fill=key), color="black", stat='identity') +
coord_flip() + facet_wrap(~ key, scales = "free") +
theme_minimal() + theme_minimal() + labs(y="Counted Value",x= "Value",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("CATEGORICAL VARIABLE DISTRIBUTION")+
theme(legend.position = 'none',
#axis.text.x = element_text(angle = 45, face = "bold", colour = "black",size = 15),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
HistCateg
plotly_build(HistCateg)
#_____________________________________________________
#Continuous Variables Exploration
#1. Age
age_hist <- ggplot(bankChurn, aes(x = Age, fill = Exited)) +
geom_histogram(binwidth = 5) + theme_bw()+
theme_minimal() + labs(y="Counted Customers", x= "Customer's Age",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("AGE VARIABLE EXPLORATION")+
theme(legend.position = 'none',
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))+
scale_x_continuous(breaks = seq(0,100,by=10), labels = comma)
age_boxplot <- ggplot(bankChurn, aes(x = Exited, y = Age, fill = Exited)) +
geom_boxplot() + theme_minimal() +labs(y="Customer's Age", x= "Churn's Status",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("AGE VARIABLE EXPLORATION")+
theme(legend.position = 'right',
axis.text.x = element_text(size=18, angle = 360, vjust = 0.5, face="bold",color = "black"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
age_hist | age_boxplot
#Comments:
#Outliers above 60 years old maybe our stable customers.
#Churned customers are mostly around 40 to 50.
#They might need to switch to other banking service for retirement purpose or whole family issue.
#___________________________
#2. Balance
balance_hist <- ggplot(bankChurn, aes(x = Balance, fill = Exited)) +
geom_histogram() +
theme_minimal() + labs(y="Counted Customers", x= "Customer's Balance",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("BALANCE VARIABLE EXPLORATION")+
theme(legend.position = 'none',
axis.text.x = element_text(angle = 90, vjust = 0.5, face="bold"),
axis.text.y = element_text(angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))+
scale_x_continuous(breaks = seq(0,255000,by=30000), labels = comma)
balance_box <- ggplot(bankChurn, aes(x = Exited, y = Balance, fill = Exited)) +
geom_boxplot() +
theme_minimal() + labs(y="Customer's Total Balance", x= "Churn's Status",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("BALANCE VARIABLE EXPLORATION")+
theme(legend.position = 'right',
axis.text.x = element_text(size=18, angle = 360, vjust = 0.5, face="bold",color = "black"),
axis.text.y = element_text(angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
balance_hist | balance_box
#Comments:
#We can see the distribution of these two groups are quite similar.
#Surprisingly some non-churned customers have lower balance than churned customers.
#_________________________
#3.Credit Score
credit_hist <- ggplot(bankChurn, aes(x = CreditScore, fill = Exited)) +
geom_histogram() + theme_minimal() + labs(y="Counted Customers", x= "Customer's Credit Score",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("CREDIT SCORE VARIABLE EXPLORATION")+
theme(legend.position = 'none',
axis.text.x = element_text(size=8, angle = 360, vjust = 0.5, face="bold"),
axis.text.y = element_text(size=8, angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
credit_box <- ggplot(bankChurn, aes(x = Exited, y = CreditScore, fill = Exited)) +
geom_boxplot() +
theme_minimal() + labs(y="Customer's Credit Score", x= "Churn's Status",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("CREDIT SCORE VARIABLE EXPLORATION")+
theme(legend.position = 'right',
axis.text.x = element_text(size=18, angle = 360, vjust = 0.5, face="bold",color = "black"),
axis.text.y = element_text(size=8, angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
credit_hist | credit_box
#Comments:
#Some customers with extremely low credit score (on the left tail) as well as
#with high credit score also churned, it indicates that really low and high
#quality customer are easily churn than the average quality customer.
#_________________________________________
#4. Estimated Salary
estimated_hist <- ggplot(bankChurn, aes(x = EstimatedSalary, fill = Exited)) +
geom_histogram() + theme_minimal() + labs(y="Counted Customers", x= "Customer's Estimated Salary",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("ESTIMATED SALARY VARIABLE EXPLORATION")+
theme(legend.position = 'none',
axis.text.x = element_text(size=8, angle = 90, vjust = 0.5, face="bold"),
axis.text.y = element_text(size=8, angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
estimated_box <- ggplot(bankChurn, aes(x = Exited, y = EstimatedSalary, fill = Exited)) +
geom_boxplot() +
theme_minimal() + labs(y="Customer's Estimated Salary", x= "Churn's Status",
caption = "Source: Mgisa Churn Analytics")+
ggtitle("ESTIMATED SALARY VARIABLE EXPLORATION")+
theme(legend.position = 'right',
axis.text.x = element_text(size=18, angle = 360, vjust = 0.5, face="bold",color = "black"),
axis.text.y = element_text(size=8, angle = 45, vjust = 0.5, face="bold"),
axis.title.x= element_text(size = 18,face = "bold",color="red"),
axis.title.y = element_text(size=18, angle = 90,vjust = 0.3,face = "bold",color = "red"),
plot.title = element_text(size=16, face="bold", color="forest green"))
estimated_hist | estimated_box
#Comments:
#Both groups have a very similar distribution.
#Esimated Salary might not be a very important infomation to decide if a customer will churn or not.
#_______________________________________
#Categorical Variables Exploration
gender_graph <- bankChurn %>%
dplyr::select(Gender, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(Gender), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'Gender')
geography_graph <- bankChurn %>%
dplyr::select(Geography, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(Geography), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'Geography')
tenure_graph <- bankChurn %>%
dplyr::select(Tenure, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(Tenure), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'Tenure')
HasCrCard_graph <- bankChurn %>%
dplyr::select(HasCrCard, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(HasCrCard), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'HasCrCard')
IsActiveMember_graph <- bankChurn %>%
dplyr::select(IsActiveMember, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(IsActiveMember), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'IsActiveMember')
NumOfProducts_graph <- bankChurn %>%
dplyr::select(NumOfProducts, Exited) %>%
table(.) %>%
as.data.frame() %>%
ggplot(.) +
ggmosaic::geom_mosaic(aes(weight = Freq, x = product(NumOfProducts), fill = Exited)) +
ggthemes::theme_tufte() +
scale_fill_brewer(type = "qual") +
labs(x = 'NumOfProducts')
(gender_graph | geography_graph) / (IsActiveMember_graph | HasCrCard_graph ) / (tenure_graph | NumOfProducts_graph)
#Comments:
#1.Female are more likely to churn than male
#2.Customers in Germany are more likely to churn than customers in France and Spain
#3.In-active customers are more likely to churn than active (very reasonable)
#4.HasCrCard may not be a useful feature as we cannot really tell if a customer has credit card will churn or not
#5.Customers in different tenure groups don't have an apparent tendency to churn or stay
#6.Customers who use 3 or 4 product are extremely likely to churn
#___________________________________________________________________
#FEATURES SELECTION BY CHI-SQUARE TEST METHOD
chi.square <- vector()
p.value <- vector()
cateVar <- bankChurn %>%
dplyr::select(-Exited) %>%
keep(is.factor)
for (i in 1:length(cateVar)) {
p.value[i] <- chisq.test(bankChurn$Exited, unname(unlist(cateVar[i])), correct = FALSE)[3]$p.value
chi.square[i] <- unname(chisq.test(bankChurn$Exited, unname(unlist(cateVar[i])), correct = FALSE)[1]$statistic)
}
chi_sqaure_test <- tibble(variable = names(cateVar)) %>%
add_column(chi.square = chi.square) %>%
add_column(p.value = p.value)
knitr::kable(chi_sqaure_test)
#Comments:
#The chi-square for Tenure and HasCrCard are pretty small, at the same time,
#their p-values are greater than 0.05, so it confirms our hypothesis that these
#two features will not provide useful information on the reponse (target) variable.
#Thus I decided to drop these two variables.
#_____________________________________________
#FEATURE SELECTION USING CARET
#________________________________________
#Feature selection using rfe in caret (Recursive Feature Elimination)
control <- rfeControl(functions = rfFuncs,
method = "cv",
repeats = 3,
verbose = FALSE)
outcomeName<-'Exited'
predictors<-names(bankTrain)[!names(bankTrain) %in% outcomeName]
Loan_Pred_Profile <- rfe(bankTrain[,predictors], bankTrain[,outcomeName],
rfeControl = control)
Loan_Pred_Profile # The top variables are 5 (age,#of products, Balance and Activeness of customers)
#Taking only the top 5 predictors
predictors<-c("Age","NumOfProducts","Balance","Geography","IsActiveMember")
#__________________________________________________
#Dropping the unwanted variables
bankChurn <- bankChurn %>%
dplyr::select(-Tenure, -HasCrCard)
#______________________________________________
#Build Predictive Models
#_______________________#
#1. PREPROCESSING DATA
#1.1 Splitting data into test and training set,
#I'll split the data using a stratified sampling approach.
# Set the seed for reproducibility
set.seed(1234)
sample_set <- bankChurn %>%
pull(.) %>%
sample.split(SplitRatio = .75)
bankTrain <- subset(bankChurn, sample_set == TRUE)
dim(bankTrain)
bankTest <- subset(bankChurn, sample_set == FALSE)
dim(bankTest)
#View(bankTest)
#______________________________________
#1.2. BALANCING THE UNBALANCED CLASS
#Let's look at the class distribution again.
round(prop.table(table(bankChurn$Exited)),3) # Entire dataset
round(prop.table(table(bankTrain$Exited)),3) # Training dataset
round(prop.table(table(bankTest$Exited)),3) #Test dataset
#Comments: The class labels are not balanced, 79.6% and 20.4% for non-churned and churned respectively
#We will use SMOTE function from DMwR package to balance them (assign them the same weight)
bankTrain <- SMOTE(Exited~., data.frame(bankTrain), perc.over = 100, perc.under = 200)
#Make a look at the data
round(prop.table(table(dplyr::select(bankTrain, Exited), exclude = NULL)),4)
#Comments: The class labels are balanced 50% for churned and non-churned.
cat("The dimension of the training set is (", dim(bankTrain), ")")
# Step 3: # Create the test sample
cat("The dimension of test set is (", dim(bankTest), ")")
# SELECTING IMPORTANCE VARIABLES IN TRAINING SET
featurePlot(x = bankTrain[,1:9],
y = bankTrain$Exited,
plot = "box",
strip=strip.custom(par.strip.text=list(cex=.7)),
scales = list(x = list(relation="free"),
y = list(relation="free")))
#Note: Works after on-hot coding (Coverting tha categorical variables in dummies)
##__________ PREDICTIVE ML MODELS#____________
#______________________
# Define the training control
fitControl <- trainControl(
method = 'cv', # k-fold cross validation
number = 10, # number of folds
savePredictions = 'final', # saves predictions for optimal tuning parameter
selectionFunction = "best",
allowParallel = TRUE
)
#__________________________________
#Model1. Logit Model
logit.mod <- glm(Exited ~., family = binomial(link = 'logit'), data = bankTrain)
summary(logit.mod)
## Predict the outcomes against our test data
logit.pred.prob <- predict(logit.mod, bankTest, type = 'response')
logit.pred <- as.factor(ifelse(logit.pred.prob > 0.5, 1, 0))
head(bankTest,10)
Confmatrix<-caret::confusionMatrix(logit.pred, bankTest$Exited, positive = "1")
#________________________
#Model2 CART, Classification and Regression Trees
ctrl <- trainControl(method = "cv", #cross-validation
number = 10, #10-fold
selectionFunction = "best",
allowParallel = TRUE
)
grid <- expand.grid(
.cp = seq(from=0.0001, to=0.005, by=0.0001)
)
set.seed(1234)
tree.mod <-
train(
Exited ~.,
data = bankTrain,
method = "rpart",
metric = "Kappa",
trControl = ctrl,
tuneGrid = grid
)
## Make predictions based on our candidate model
tree.pred.prob <- predict(tree.mod, bankTest, type = "prob")
tree.pred <- predict(tree.mod, bankTest, type = "raw")
###View Confusion Matrix
ConfMatrixTree<-caret::confusionMatrix(tree.pred, bankTest$Exited, positive = "1")
#________________
#Model 3 Random Forest
#Create a control object.
ctrl <- trainControl(method = "cv",
number = 10,
selectionFunction = "best",
allowParallel = TRUE
)
## Create a grid search based on the available parameters.
grid <- expand.grid(.mtry = c(1:8))
## Build the random forest model
rf.mod <-
train(Exited ~.,
data = bankTrain,
method = 'rf',
metric = 'Kappa',
trControl = ctrl,
tuneGrid = grid)
## Make the predictions
rf.pred <- predict(rf.mod, bankTest, type = "raw")
rf.pred.prob <- predict(rf.mod, bankTest, type = "prob")
#View Confusion Matrix
ConfMatix_rf<- caret::confusionMatrix(rf.pred, bankTest$Exited, positive = "1")
#Model4: XGBTREE Model
## Create a control object
ctrl <-
trainControl(method = "cv",
number = 10,
selectionFunction = "best",
allowParallel = TRUE
)
#modelLookup("xgbTree")
## Grid Search
grid <- expand.grid(
nrounds = 40,
max_depth = c(4,5,6,7,8),
eta = c(0.1,0.2,0.3,0.4,0.5),
gamma = 0.01,
colsample_bytree = 1,
min_child_weight = 1,
subsample = c(0.5, 1)
)
## Build XGBoost
set.seed(1234)
xgb.mod <-
train(
Exited ~ .,
data = bankTrain,
method = "xgbTree",
metric = "Kappa",
trControl = ctrl,
tuneGrid = grid
)
## Make the prediction
xgb.pred <- predict(xgb.mod, bankTest, type = "raw")
xgb.pred.prob <- predict(xgb.mod, bankTest, type = "prob")
#View Confusion Matrix
ConfMatrix_xgb<-caret::confusionMatrix(xgb.pred, bankTest$Exited, positive = "1")
#________________________________
#Model 5 sda model
model_sda <- train(Exited~., data = bankTrain, method = 'sda' ,
metric= "Kappa",
trControl = fitControl)
#Make prediction
sda.pred.prob <- predict(model_sda, bankTest, type = "prob")
sda.pred <- predict(model_sda, bankTest, type = "raw")
#View Confusion Matrix
ConfMatrix_sda<-caret::confusionMatrix(sda.pred, bankTest$Exited, positive = "1")
#Model 6 C5.0Tree (Single C5.0 Tree) model
model_c5.0 <- train(Exited~., data = bankTrain, method = 'C5.0Tree' ,
trControl = fitControl,
metric = "Kappa")
# predict the outcome on a test set (Testing model or model validation)
c5.0.pred.prob <- predict(model_c5.0, bankTest, type = "prob")
c5.0.pred <- predict(model_c5.0, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_c5.0<-caret::confusionMatrix(c5.0.pred, bankTest$Exited, positive = "1")
#Model 7 Naive Bayes
model.naiveb <- train(Exited~., data = bankTrain, method = 'naive_bayes' ,
trControl = fitControl,
metric = "Kappa")
# predict the outcome on a test set (Testing model or model validation)
naiveb.pred.prob <- predict(model.naiveb, bankTest, type = "prob")
naiveb.pred <- predict(model.naiveb, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_naive<-caret::confusionMatrix(naiveb.pred, bankTest$Exited, positive = "1")
#Model 8 Multivariate Adaptive Regression Spline
model.mars <- train(Exited~., data = bankTrain, method = 'earth' ,
trControl = fitControl,
metric = "Kappa")
# predict the outcome on a test set (Testing model or model validation)
mars.pred.prob <- predict(model.mars, bankTest, type = "prob")
mars.pred <- predict(model.mars, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_mars<-caret::confusionMatrix(mars.pred, bankTest$Exited, positive = "1")
#Model 9 Adaptive Boosting Machine
model.adaboost <- train(Exited~., data = bankTrain, method = 'adaboost' ,
trControl = fitControl,
metric = "Kappa")
# predict the outcome on a test set (Testing model or model validation)
adaboost.pred.prob <- predict(model.adaboost, bankTest, type = "prob")
adaboost.pred <- predict(model.adaboost, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_adaboost<-caret::confusionMatrix(adaboost.pred, bankTest$Exited, positive = "1")
#Model 10. Ctree (Conditional Inference Tree) Model
model.ctree <- train(Exited~., data = bankTrain, method = 'ctree' ,
trControl = fitControl,
metric = "Kappa")
ctree.pred.prob <- predict(model.ctree, bankTest, type = "prob")
ctree.pred <- predict(model.ctree, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_ctree<-caret::confusionMatrix(ctree.pred, bankTest$Exited, positive = "1")
#Model 11. Linda(Robust Linear Discriminant Analysis aka Constructor) model
model.linda <- train(Exited~., data = bankTrain, method = 'Linda' ,
trControl = fitControl,
metric = "Kappa")
linda.pred.prob <- predict(model.linda, bankTest, type = "prob")
linda.pred <- predict(model.linda, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_linda<-caret::confusionMatrix(linda.pred, bankTest$Exited, positive = "1")
#Model 12. AdaBag model
model.adaBag <- train(Exited~., data = bankTrain, method = "AdaBag" ,
trControl = fitControl,
metric = "Kappa")
adaBag.pred.prob <- predict(model.adaBag, bankTest, type = "prob")
adaBag.pred <- predict(model.adaBag, bankTest, type = "raw")
#View Confusion Matric
ConfMatrix_adaBag<-caret::confusionMatrix(adaBag.pred, bankTest$Exited, positive = "1")
#___________________________
# RUN resamples() TO COMPARE THE MODELS
#____________________________________
# 1. Compare model performances using resample()
models_compare<- resamples(list(XGBTREE = xgb.mod,NBayes=model.naiveb, MARS=model.mars,RForest = rf.mod))
#,XGBTREE = xgb.mod,LINDA = model.linda,GLM=logit.mod,ADABOOST = model.adaboost,RForest = rf.mod, CART = tree.mod,C5.0Tree = model_c5.0,CITree=model.ctree,MARS=model.mars, SDA=model_sda))
# Summary of the models performances
summary(models_compare)
#Let's plot the resamples summary output.
# Draw box plots to compare models
scales <- list(x=list(relation="free"), y=list(relation="free"))
bwplot(models_compare, scales=scales, main='The Comparative Performance of Learning Algorithms')
#2. USING DIFFERENT ML METRICS
#1 LOGISTIC REGLESSION.
## Logistic Regression
test <- bankTest$Exited
pred <- logit.pred
prob <- logit.pred.prob
# Logistic Regression ROC curve
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, main = "ROC Curve for Bank Churn Prediction Learning Machine", col = 2, lwd = 2)
abline(a = 0, b = 1, lwd = 3, lty = 2, col = 1)
## Logistic Regression Performance Metrics
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- tibble(Learning_Machine="Logistic Regression", accuracy = accuracy, fmeasure = fmeasure,kappa = kappa, auc = auc)
# CLASSIFICATION TREE
test <- bankTest$Exited
pred <- tree.pred
prob <- tree.pred.prob[,2]
## Classification Tree ROC Curve
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=3, lwd = 2, add=TRUE)
## Classification Tree Performance Metrics
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Classification Tree", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
# RANDOM FOREST
test <- bankTest$Exited
pred <- rf.pred
prob <- rf.pred.prob[,2]
## Random Forest ROC Curve
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=4, lwd = 2, add=TRUE)
## Random Forest Performance Metrics
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Random Forest", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
# EXTREME GRADIENT BOOSTING MACHINE
## XGBoost
test <- bankTest$Exited
pred <- xgb.pred
prob <- xgb.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=5, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="eXtreme Gradient Boosting", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#____________________________________________________________
# Statistical Discriminant Analytics
## sda
test <- bankTest$Exited
pred <- sda.pred
prob <- sda.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=6, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Stat. Discriminant Analysis", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#___________________________________________
# Single C5.0 Tree
## C5.0Tree
test <- bankTest$Exited
pred <- c5.0.pred
prob <- c5.0.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=7, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Single C5.0Tree", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#____________________________________
# Naive Bayes
## naiveb
test <- bankTest$Exited
pred <- naiveb.pred
prob <- naiveb.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=8, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Naive Bayes", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#__________________________________
#Multivariate Adaptive Regression Spline
## mars
test <- bankTest$Exited
pred <- mars.pred
prob <- mars.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=9, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Multivariate Adaptive Regression Spline", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#_________________________________________
# Adaptive Boosting Machine
## adaboost
test <- bankTest$Exited
pred <- adaboost.pred
prob <- adaboost.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=10, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Adaptive Boosting Machine", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#_______________________________________
# CONDITIONAL INFERENCE TREE
## CTree
test <- bankTest$Exited
pred <- ctree.pred
prob <- ctree.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=11, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Conditional Inference Tree", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#___________________________________
# Constructor Model
## linda
test <- bankTest$Exited
pred <- linda.pred
prob <- linda.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=12, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Constructor Learning Machine", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
#______________________________________
# Adaptive Learning Machine with Bagging
## adaBag
test <- bankTest$Exited
pred <- adaBag.pred
prob <- adaBag.pred.prob[,2]
# Plot ROC Curve.
roc.pred <- prediction(predictions = prob, labels = test)
roc.perf <- performance(roc.pred, measure = "tpr", x.measure = "fpr")
plot(roc.perf, col=13, lwd = 2, add=TRUE)
# Get performance metrics.
accuracy <- mean(test == pred)
precision <- posPredValue(pred, test, positive = "1")
recall <- caret::sensitivity(pred, test, positive = "1")
fmeasure <- (2 * precision * recall)/(precision + recall)
confmat <- caret::confusionMatrix(pred, test, positive = "1")
kappa <- as.numeric(confmat$overall["Kappa"])
auc <- as.numeric(performance(roc.pred, measure = "auc")@y.values)
comparisons <- comparisons %>%
add_row(Learning_Machine="Adaptive Learning with Bagging", accuracy = accuracy, fmeasure = fmeasure, kappa = kappa, auc = auc)
# Draw ROC legend.
legend(1.2, 1.2, c('Logistic Regression',
'Classification Tree',
'Random Forest',
'eXtreme Gradient Boosting',
"Discriminant Analysis",
"Single C5.0Tree",
"Naive Bayes",
"Multivariate Adap. Reg.Spline",
"Adaptive Boosting Machine",
"Conditional Inference Tree",
"Constructor Learning Machine",
"Adaptive Learning with Bagging"), 2:13)
#OUTPUT COMPARISON TABLE
knitr::kable(comparisons)
#Since the response class are quite unmblanced so we will not use the prediction accuracy,
# we prefer to use other listed measures, for ex. from ROC Multivariate Adaptive Regression Spline
#achieves a better performance. I'll go with Mars as our final model.
# FEATURE IMPORTANCE OF WINNING MODEL
#Winner 1. MARS
varimp_mars <- varImp(model.mars)
plot(varimp_mars, main="Variable Importance with Multivariate Adaptive Regression Spline (MARS)")
#Other way to plpt the varimpo.
vip::vip(model.mars, color="black", fill="yellow")
varimp_rForest <- varImp(rf.mod)
plot(varimp_rForest, main="Variable Importance with Random Forest")
#Other way to plpt the varimpo.
vip::vip(rf.mod, color="black", fill="red")
#_____________________________________________________
#ENSEMBLE PREDICTION
install.packages("caretEnsemble")
library(caretEnsemble)
# Example of Stacking algorithms
# create submodels
control <- trainControl(method="repeatedcv",
number=10, repeats=3,
savePredictions=TRUE,
classProbs=TRUE,
allowParallel = TRUE
)
seed=1000
algorithmList <- c('earth', 'xgbTree', 'glm', 'naive_bayes', 'rf')
set.seed(seed)
models <- caretList(Exited~., data=bankTrain, trControl=control, methodList=algorithmList)
results <- resamples(models)
summary(results)
dotplot(results)
stackControl <- trainControl(method="repeatedcv",
number=10,
repeats=3,