-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_File.R
More file actions
2350 lines (1559 loc) · 73.4 KB
/
Copy pathProject_File.R
File metadata and controls
2350 lines (1559 loc) · 73.4 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
cat("\n","------------------ import data --------------","\n")
# Install packages
#install.packages('stargazer',dependencies=TRUE)
#install.packages('corrplot', dependencies=TRUE)
#install.packages('dplyr')
#install.packages('rbin', dependencies = TRUE)
#install.packages('OneR',dependencies=TRUE)
#install.packages('woeBinning',dependencies=TRUE)
#install.packages('ggplot2', dependencies = TRUE)
#install.packages('car', dependencies=TRUE)
# Load packages
library(stargazer)
library(tidyverse)
library(moments)
library(corrplot)
library(car)
library(OneR)
library(woeBinning)
library(ggplot2)
library(dplyr)
library(rbin)
library(expss)
library(rpart) # Popular decision tree algorithm
library(rattle) # Fancy tree plot
library(rpart.plot) # Enhanced tree plots
library(RColorBrewer)
library(caret)
library(ROCR)
# Define path and file;
# You will need to change the path value to match the location on your own computer;
my.path <- '/Users/tdubon/Documents/Northwestern/MSDS_498/Project_Data/';
my.file <- paste(my.path,'credit_card_default.RData', sep='');
# Read the RData object using readRDS();
credit_card_default <- readRDS(my.file)
# Copy original dataframe
cc_default_df <- credit_card_default
head(cc_default_df)
cat("\n","------------------ DATA QUALITY CHECK ON RAW VARIABLES --------------","\n")
# Show dataframe structure & preliminary summary stats;
str(cc_default_df)
summary(cc_default_df)
head(cc_default_df)
# categorical data to be treated as factors
cc_default_df[c(3:5, 7:12, 25, 27:30)]<-lapply(cc_default_df[c(3:5, 7:12, 25, 27:30)], factor)
# missing values
sapply(cc_default_df, function(x) sum(is.na(x)))
# Rename PAY_0 TO PAY_1
names(cc_default_df)[7] <- "PAY_1"
# apply stargazer to study and summarize continuous variables for data quality check
# define output path
out.path <- '/Users/tdubon/Documents/Northwestern/MSDS_498/Project_Data';
# summary stat table
file.name <- 'Stat_Table.html';
stargazer(cc_default_df, type=c('html'),out=paste(out.path,file.name,sep=''),
title=c('Table 1: Summary Statistics for Default of Credit Card Clients Data'),
align=TRUE, digits=2, digits.extra=2, initial.zero=TRUE, median=TRUE)
# summary of factor variables
table(cc_default_df$SEX)
table(cc_default_df$EDUCATION)
table(cc_default_df$MARRIAGE)
table(cc_default_df$PAY_1)
table(cc_default_df$PAY_2)
table(cc_default_df$PAY_3)
table(cc_default_df$PAY_4)
table(cc_default_df$PAY_5)
table(cc_default_df$PAY_6)
cat("\n","------------------ TRAIN DATA SPLIT for WOE BIN --------------","\n")
train_df <- cc_default_df[cc_default_df$train==1, 2:25]
n.train <- dim(train_df)[1] # 15,180
count_if('1', cc_default_df$train) #confirms train_df matches original
cat("\n","------------------ FEATURE ENGINEERING --------------","\n")
#-------------------------------------------------LIMIT_BAL WOE BIN
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('LIMIT_BAL')) #USED TRAINING DATA TO DETERMINE BINS
# (-Inf,30,000]
# (30,000,160,000]
# (160,000, Inf]
summary(cc_default_df$LIMIT_BAL)
# RECODE BINNED LIMIT_BAL
cc_default_df$LIMIT_BAL_Neg_29999 <- ifelse(cc_default_df$LIMIT_BAL<30000, '1','0')
cc_default_df$LIMIT_BAL_30000_159999 <- ifelse(cc_default_df$LIMIT_BAL>=30000 & cc_default_df$LIMIT_BAL<160000, '1','0')
cc_default_df$LIMIT_BAL_160000_1000000 <- ifelse(cc_default_df$LIMIT_BAL>=160000 & cc_default_df$LIMIT_BAL<=1000000, '1','0')
table(cc_default_df$LIMIT_BAL_Neg_29999)
table(cc_default_df$LIMIT_BAL_30000_159999)
table(cc_default_df$LIMIT_BAL_160000_1000000) #binned limit_bal=1 totals from all bins add to 30k
cc_default_df$LIMIT_BAL_Neg_29999 <- as.factor(cc_default_df$LIMIT_BAL_Neg_29999)
cc_default_df$LIMIT_BAL_30000_159999 <- as.factor(cc_default_df$LIMIT_BAL_30000_159999)
cc_default_df$LIMIT_BAL_160000_1000000 <- as.factor(cc_default_df$LIMIT_BAL_160000_1000000)
#---------------------------------------------AGE WOE BIN
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('AGE')) #USED TRAINING DATA TO DETERMINE BINS
# AGE: 0-25, 25-33, 33-INF
#----------------------------------------------------------------RECODE BINNED AGE
cc_default_df$AGE_0_24 <- ifelse(cc_default_df$AGE<25, '1','0')
cc_default_df$AGE_25_32 <- ifelse(cc_default_df$AGE>=25 & cc_default_df$AGE<33, '1','0')
cc_default_df$AGE_33_80 <- ifelse(cc_default_df$AGE>=33, '1','0')
table(cc_default_df$AGE_0_24)
table(cc_default_df$AGE_25_32)
table(cc_default_df$AGE_33_80) #binned AGE=1 totals from all bins add to 30k
cc_default_df$AGE_0_24 <- as.factor(cc_default_df$AGE_0_24)
cc_default_df$AGE_25_32 <- as.factor(cc_default_df$AGE_25_32)
cc_default_df$AGE_33_80 <- as.factor(cc_default_df$AGE_33_80)
#---------------------------------------------------------------recode EDUCATION: 0, 5 & 6 -> 4 "OTHERS"
table(cc_default_df$EDUCATION)
cc_default_df$EDUCATION <- as.factor(cc_default_df$EDUCATION)
cc_default_df$EDUCATION <- recode_factor(cc_default_df$EDUCATION, '0' = '4')
cc_default_df$EDUCATION <- recode_factor(cc_default_df$EDUCATION, '5' ='4')
cc_default_df$EDUCATION <- recode_factor(cc_default_df$EDUCATION, '6' ='4')
cc_default_df$EDUCATION <- factor(cc_default_df$EDUCATION, levels= c('1','2', '3', '4'))
table(cc_default_df$EDUCATION) #new total for 4: 468
#---------------------------------------------------------------recode MARRIAGE: 0 -> 3 "OTHERS"
table(cc_default_df$MARRIAGE)
cc_default_df$MARRIAGE <- recode_factor(cc_default_df$MARRIAGE, '0' ='3')
table(cc_default_df$MARRIAGE)
cc_default_df$MARRIAGE <- factor(cc_default_df$MARRIAGE, levels= c('1','2', '3'))
#---------------------------------------------------------------recode PAY variables: 0, -2 -> -1 "OTHERS"
#cc_default_df$PAY_1 <- credit_card_default$PAY_0
table(cc_default_df$PAY_1)
#cc_default_df$PAY_1 <- as.factor(cc_default_df$PAY_1)
cc_default_df$PAY_1 <- recode_factor(cc_default_df$PAY_1, '0' ='-1')
cc_default_df$PAY_1 <- recode_factor(cc_default_df$PAY_1, '-2' ='-1')
table(cc_default_df$PAY_1)
table(cc_default_df$PAY_2)
cc_default_df$PAY_2 <- recode_factor(cc_default_df$PAY_2, '0' ='-1')
cc_default_df$PAY_2 <- recode_factor(cc_default_df$PAY_2, '-2' ='-1')
table(cc_default_df$PAY_2)
table(cc_default_df$PAY_3)
cc_default_df$PAY_3 <- recode_factor(cc_default_df$PAY_3, '0' ='-1')
cc_default_df$PAY_3 <- recode_factor(cc_default_df$PAY_3, '-2' ='-1')
table(cc_default_df$PAY_3)
table(cc_default_df$PAY_4)
cc_default_df$PAY_4 <- recode_factor(cc_default_df$PAY_4, '0' ='-1')
cc_default_df$PAY_4 <- recode_factor(cc_default_df$PAY_4, '-2' ='-1')
table(cc_default_df$PAY_4)
table(cc_default_df$PAY_5)
cc_default_df$PAY_5 <- recode_factor(cc_default_df$PAY_5, '0' ='-1')
cc_default_df$PAY_5 <- recode_factor(cc_default_df$PAY_5, '-2' ='-1')
table(cc_default_df$PAY_5)
table(cc_default_df$PAY_6)
cc_default_df$PAY_6 <- recode_factor(cc_default_df$PAY_6, '0' ='-1')
cc_default_df$PAY_6 <- recode_factor(cc_default_df$PAY_6, '-2' ='-1')
table(cc_default_df$PAY_6)
#--------------------------------------------------------calculate AVG_BILL_AMT
str(cc_default_df)
cc_default_df$Avg_Bill_Amt <- NA
cc_default_df$Avg_Bill_Amt <- apply(cc_default_df[13:18], 1, mean)
summary(cc_default_df$Avg_Bill_Amt)
train_df <- cc_default_df[cc_default_df$train==1, c(2:25, 37)]
str(train_df)
# AVG_BILL_AMT WOE BIN
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Avg_Bill_Amt')) #USED TRAINING DATA TO DETERMINE BINS
# (-Inf,189.9916667]
# (189.9916667,2847.1]
# (2847.1,7488.683333]
# (7488.683333,31916.93333]
# (31916.93333, Inf]
# RECODE BINNED AVG_BILL_AMT
cc_default_df$Avg_Bill_Amt_Neg_56050_188 <- ifelse(cc_default_df$Avg_Bill_Amt<189, '1','0')
cc_default_df$Avg_Bill_Amt_189_2846 <- ifelse(cc_default_df$Avg_Bill_Amt>=189 & cc_default_df$Avg_Bill_Amt<2847, '1','0')
cc_default_df$Avg_Bill_Amt_2847_7488 <- ifelse(cc_default_df$Avg_Bill_Amt>=2847 & cc_default_df$Avg_Bill_Amt<7489, '1','0')
cc_default_df$Avg_Bill_Amt_7489_31915 <- ifelse(cc_default_df$Avg_Bill_Amt>=7489 & cc_default_df$Avg_Bill_Amt<31916, '1','0')
cc_default_df$Avg_Bill_Amt_31916_900000 <- ifelse(cc_default_df$Avg_Bill_Amt>=31916 & cc_default_df$Avg_Bill_Amt<=900000, '1','0')
table(cc_default_df$Avg_Bill_Amt_Neg_56050_188)
table(cc_default_df$Avg_Bill_Amt_189_2846)
table(cc_default_df$Avg_Bill_Amt_2847_7488)
table(cc_default_df$Avg_Bill_Amt_7489_31915)
table(cc_default_df$Avg_Bill_Amt_31916_900000)
cc_default_df$Avg_Bill_Amt_Neg_56050_188 <- as.factor(cc_default_df$Avg_Bill_Amt_Neg_56050_188)
cc_default_df$Avg_Bill_Amt_189_2846 <- as.factor(cc_default_df$Avg_Bill_Amt_189_2846)
cc_default_df$Avg_Bill_Amt_2847_7488 <- as.factor(cc_default_df$Avg_Bill_Amt_2847_7488)
cc_default_df$Avg_Bill_Amt_7489_31915 <- as.factor(cc_default_df$Avg_Bill_Amt_7489_31915)
cc_default_df$Avg_Bill_Amt_31916_900000 <- as.factor(cc_default_df$Avg_Bill_Amt_31916_900000)
#--------------------------------------------------calculate AVG_PMT_AMT
cc_default_df$Avg_Pmt_Amt <- NA
cc_default_df$Avg_Pmt_Amt <- apply(cc_default_df[19:24], 1, mean)
head(cc_default_df$Avg_Pmt_Amt)
summary(cc_default_df$Avg_Pmt_Amt)
train_df <- cc_default_df[cc_default_df$train==1, c(2:25, 37, 43)]
str(train_df)
# Avg_Pmt_Amt WOE BIN
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Avg_Pmt_Amt')) #USED TRAINING DATA TO DETERMINE BINS
#(-Inf,2832.65]
#(2832.65,12092.5]
#(12092.5, Inf]
cc_default_df$Avg_Pmt_Amt_0_2832 <- ifelse(cc_default_df$Avg_Pmt_Amt<2833, '1','0')
cc_default_df$Avg_Pmt_Amt_2833_12092 <- ifelse(cc_default_df$Avg_Pmt_Amt>=2833 & cc_default_df$Avg_Pmt_Amt<12093, '1','0')
cc_default_df$Avg_Pmt_Amt_12093_627344 <- ifelse(cc_default_df$Avg_Pmt_Amt>=12093 & cc_default_df$Avg_Pmt_Amt<627345, '1','0')
table(cc_default_df$Avg_Pmt_Amt_0_2832)
table(cc_default_df$Avg_Pmt_Amt_2833_12092)
table(cc_default_df$Avg_Pmt_Amt_12093_627344)
cc_default_df$Avg_Pmt_Amt_0_2832 <- as.factor(cc_default_df$Avg_Pmt_Amt_0_2832)
cc_default_df$Avg_Pmt_Amt_2833_12092 <- as.factor(cc_default_df$Avg_Pmt_Amt_2833_12092)
cc_default_df$Avg_Pmt_Amt_12093_627344 <- as.factor(cc_default_df$Avg_Pmt_Amt_12093_627344)
str(cc_default_df)
#------------------------------------------calculate Avg_Pmt_Ratio
#-------------------calculate ratios
ratio <- function(pay_var, bill_var, new_var){
new_var <- NA
new_var <- as.numeric(new_var)
new_var <- pay_var/bill_var
new_var[pay_var == 0 & bill_var == 0] <- 100
new_var[pay_var == 0 & bill_var != 0] <- 0
new_var[pay_var != 0 & bill_var == 0] <- 1
return(new_var)
}
# Ratio_1
cc_default_df$Pmt_Ratio1 <- ratio(pay_var=cc_default_df$PAY_AMT1, bill_var=cc_default_df$BILL_AMT2, new_var=cc_default_df$Pmt_Ratio1)
# Ratio_2
cc_default_df$Pmt_Ratio2 <- ratio(pay_var=cc_default_df$PAY_AMT2, bill_var=cc_default_df$BILL_AMT3, new_var=cc_default_df$Pmt_Ratio2)
# Ratio_3
cc_default_df$Pmt_Ratio3 <- ratio(pay_var=cc_default_df$PAY_AMT3, bill_var=cc_default_df$BILL_AMT4, new_var=cc_default_df$Pmt_Ratio3)
# Ratio_4
cc_default_df$Pmt_Ratio4 <- ratio(pay_var=cc_default_df$PAY_AMT4, bill_var=cc_default_df$BILL_AMT5, new_var=cc_default_df$Pmt_Ratio4)
# Ratio_5
cc_default_df$Pmt_Ratio5 <- ratio(pay_var=cc_default_df$PAY_AMT5, bill_var=cc_default_df$BILL_AMT6, new_var=cc_default_df$Pmt_Ratio5)
# check variables
count_if('Inf', cc_default_df$Pmt_Ratio1)
count_if('NaN', cc_default_df$Pmt_Ratio1)
count_if('0', cc_default_df$Pmt_Ratio1)
summary(cc_default_df$Pmt_Ratio1)
count_if('Inf', cc_default_df$Pmt_Ratio2)
count_if('NaN', cc_default_df$Pmt_Ratio2)
count_if('0', cc_default_df$Pmt_Ratio2)
summary(cc_default_df$Pmt_Ratio2)
count_if('Inf', cc_default_df$Pmt_Ratio3)
count_if('NaN', cc_default_df$Pmt_Ratio3)
count_if('0', cc_default_df$Pmt_Ratio3)
summary(cc_default_df$Pmt_Ratio3)
count_if('Inf', cc_default_df$Pmt_Ratio4)
count_if('NaN', cc_default_df$Pmt_Ratio4)
count_if('0', cc_default_df$Pmt_Ratio4)
summary(cc_default_df$Pmt_Ratio4)
count_if('Inf', cc_default_df$Pmt_Ratio5)
count_if('NaN', cc_default_df$Pmt_Ratio5)
count_if('0', cc_default_df$Pmt_Ratio5)
summary(cc_default_df$Pmt_Ratio5)
str(cc_default_df)
#----------------------------------------------calculate average ratio
cc_default_df$Avg_Pmt_Ratio <- NA
cc_default_df$Avg_Pmt_Ratio <- apply(cc_default_df[47:51], 1, mean)
summary(cc_default_df$Avg_Pmt_Ratio)
train_df <- cc_default_df[cc_default_df$train==1, c(2:25, 37, 43, 52)]
str(train_df)
#---------------------------------------------Bin Avg_Pmt_Ratio
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Avg_Pmt_Ratio')) #USED TRAINING DATA TO DETERMINE BINS
#(-Inf,0.03406552305]
#(0.03406552305,0.1583070591]
#(0.1583070591,1.000163684]
#(1.000163684,1.17911605]
#(1.17911605, Inf]
cc_default_df$Avg_Pmt_Ratio_Neg16429_pt02 <- ifelse(cc_default_df$Avg_Pmt_Ratio<0.03406552305, '1','0')
cc_default_df$Avg_Pmt_Ratio_pt03_pt15 <- ifelse(cc_default_df$Avg_Pmt_Ratio>=0.03406552305 & cc_default_df$Avg_Pmt_Ratio<0.1583070591, '1','0')
cc_default_df$Avg_Pmt_Ratio_pt16_1 <- ifelse(cc_default_df$Avg_Pmt_Ratio>=0.1583070591 & cc_default_df$Avg_Pmt_Ratio<1.000163684, '1','0')
cc_default_df$Avg_Pmt_Ratio_1_1pt17 <- ifelse(cc_default_df$Avg_Pmt_Ratio>=1.000163684 & cc_default_df$Avg_Pmt_Ratio<1.17911605, '1','0')
cc_default_df$Avg_Pmt_Ratio_1pt18_2688 <- ifelse(cc_default_df$Avg_Pmt_Ratio>=1.17911605 & cc_default_df$Avg_Pmt_Ratio<2688, '1','0')
table(cc_default_df$Avg_Pmt_Ratio_Neg16429_pt02)
table(cc_default_df$Avg_Pmt_Ratio_pt03_pt15)
table(cc_default_df$Avg_Pmt_Ratio_pt16_1)
table(cc_default_df$Avg_Pmt_Ratio_1_1pt17)
table(cc_default_df$Avg_Pmt_Ratio_1pt18_2688)
cc_default_df$Avg_Pmt_Ratio_Neg16429_pt02 <- as.factor(cc_default_df$Avg_Pmt_Ratio_Neg16429_pt02)
cc_default_df$Avg_Pmt_Ratio_pt03_pt15 <- as.factor(cc_default_df$Avg_Pmt_Ratio_pt03_pt15)
cc_default_df$Avg_Pmt_Ratio_pt16_1 <- as.factor(cc_default_df$Avg_Pmt_Ratio_pt16_1)
cc_default_df$Avg_Pmt_Ratio_1_1pt17 <- as.factor(cc_default_df$Avg_Pmt_Ratio_1_1pt17)
cc_default_df$Avg_Pmt_Ratio_1pt18_2688 <- as.factor(cc_default_df$Avg_Pmt_Ratio_1pt18_2688)
str(cc_default_df)
#------------------------------------------------------------Calculate Avg_Util
#----------------Calculate Utility
cc_default_df$Util1 <- NA
cc_default_df$Util2 <- NA
cc_default_df$Util3 <- NA
cc_default_df$Util4 <- NA
cc_default_df$Util5 <- NA
cc_default_df$Util6 <- NA
cc_default_df$Util1 <- ifelse(cc_default_df$BILL_AMT1==0, '0', cc_default_df$BILL_AMT1/cc_default_df$LIMIT_BAL)
cc_default_df$Util2 <- ifelse(cc_default_df$BILL_AMT2==0, '0', cc_default_df$BILL_AMT2/cc_default_df$LIMIT_BAL)
cc_default_df$Util3 <- ifelse(cc_default_df$BILL_AMT3==0, '0', cc_default_df$BILL_AMT3/cc_default_df$LIMIT_BAL)
cc_default_df$Util4 <- ifelse(cc_default_df$BILL_AMT4==0, '0', cc_default_df$BILL_AMT4/cc_default_df$LIMIT_BAL)
cc_default_df$Util5 <- ifelse(cc_default_df$BILL_AMT5==0, '0', cc_default_df$BILL_AMT5/cc_default_df$LIMIT_BAL)
cc_default_df$Util6 <- ifelse(cc_default_df$BILL_AMT6==0, '0', cc_default_df$BILL_AMT6/cc_default_df$LIMIT_BAL)
cc_default_df$Util1 <- as.numeric(cc_default_df$Util1)
cc_default_df$Util2 <- as.numeric(cc_default_df$Util2)
cc_default_df$Util3 <- as.numeric(cc_default_df$Util3)
cc_default_df$Util4 <- as.numeric(cc_default_df$Util4)
cc_default_df$Util5 <- as.numeric(cc_default_df$Util5)
cc_default_df$Util6 <- as.numeric(cc_default_df$Util6)
count_if('0', cc_default_df$Util1)
#--------------calculate average of utility
str(cc_default_df)
cc_default_df$Avg_Util <- apply(cc_default_df[58:63], 1, mean)
summary(cc_default_df$Avg_Util)
train_df <- cc_default_df[cc_default_df$train==1, c(2:25, 37, 43, 52, 64)]
str(train_df)
#-----------------Bin Avg_Util
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Avg_Util')) #USED TRAINING DATA TO DETERMINE BINS
#(-Inf,0.0009562517806]
#(0.0009562517806,0.009194422492]
#(0.009194422492,0.3673418095]
#(0.3673418095,0.8231566667]
#(0.8231566667, Inf]
cc_default_df$Avg_Util_negpt2326_pt00095 <- ifelse(cc_default_df$Avg_Util<0.0009562517806, '1','0')
cc_default_df$Avg_Util_pt00096_pt0091 <- ifelse(cc_default_df$Avg_Util>=0.0009562517806 & cc_default_df$Avg_Util<0.009194422492, '1','0')
cc_default_df$Avg_Util_pt0092_pt3673 <- ifelse(cc_default_df$Avg_Util>=0.009194422492 & cc_default_df$Avg_Util<0.3673418095, '1','0')
cc_default_df$Avg_Util_pt3674_pt8231 <- ifelse(cc_default_df$Avg_Util>=0.3673418095 & cc_default_df$Avg_Util<0.8231566667, '1','0')
cc_default_df$Avg_Util_pt8232_6 <- ifelse(cc_default_df$Avg_Util>=0.8231566667 & cc_default_df$Avg_Util<6, '1','0')
table(cc_default_df$Avg_Util_negpt2326_pt00095)
table(cc_default_df$Avg_Util_pt00096_pt0091)
table(cc_default_df$Avg_Util_pt0092_pt3673)
table(cc_default_df$Avg_Util_pt3674_pt8231)
table(cc_default_df$Avg_Util_pt8232_6)
cc_default_df$Avg_Util_negpt2326_pt00095 <- as.factor(cc_default_df$Avg_Util_negpt2326_pt00095)
cc_default_df$Avg_Util_pt00096_pt0091 <- as.factor(cc_default_df$Avg_Util_pt00096_pt0091)
cc_default_df$Avg_Util_pt0092_pt3673 <- as.factor(cc_default_df$Avg_Util_pt0092_pt3673)
cc_default_df$Avg_Util_pt3674_pt8231 <- as.factor(cc_default_df$Avg_Util_pt3674_pt8231)
cc_default_df$Avg_Util_pt8232_6 <- as.factor(cc_default_df$Avg_Util_pt8232_6)
str(cc_default_df)
#------------------------------------------------------------------Balance Growth Over 6 Months
#------------------------calculate month to month difference
cc_default_df$Bal_Chx1 <- NA
cc_default_df$Bal_Chx2 <- NA
cc_default_df$Bal_Chx3 <- NA
cc_default_df$Bal_Chx4 <- NA
cc_default_df$Bal_Chx5 <- NA
cc_default_df$Bal_Chx1 <- (cc_default_df$BILL_AMT2 - cc_default_df$BILL_AMT1) #Calc Bill2-Bill1
cc_default_df$Bal_Chx2 <- (cc_default_df$BILL_AMT3 - cc_default_df$BILL_AMT2) #Calc Bill3-Bill2
cc_default_df$Bal_Chx3 <- (cc_default_df$BILL_AMT4 - cc_default_df$BILL_AMT3) #Calc Bill4-Bill3
cc_default_df$Bal_Chx4 <- (cc_default_df$BILL_AMT5 - cc_default_df$BILL_AMT4) #Calc Bill5-Bill4
cc_default_df$Bal_Chx5 <- (cc_default_df$BILL_AMT6 - cc_default_df$BILL_AMT5) #Calc Bill6-Bill5
#---------------------Balance Growth Over 6 Months
cc_default_df$Bal_Growth_6mo <- NA
cc_default_df$Bal_Growth_6mo <- apply(cc_default_df[70:74], 1, sum) #add the changes
summary(cc_default_df$Bal_Growth_6mo)
plot(cc_default_df$Bal_Growth_6mo)
str(cc_default_df)
train_df <- cc_default_df[cc_default_df$train==1, c(3:25, 37, 43, 52, 64, 75, 84)]
str(train_df)
#--------------------Bin Bal_Growth_6mo
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Bal_Growth_6mo')) #USED TRAINING DATA TO DETERMINE BINS
#(-Inf,-123]
#(-123,21389.95]
#(21389.95, Inf]
summary(cc_default_df$Bal_Growth_6mo)
cc_default_df$Bal_Growth_6mo_neg708323_neg122 <- ifelse(cc_default_df$Bal_Growth_6mo < -123, '1','0')
cc_default_df$Bal_Growth_6mo_neg123_21389 <- ifelse(cc_default_df$Bal_Growth_6mo>= -123 & cc_default_df$Bal_Growth_6mo< 21389.95, '1','0')
cc_default_df$Bal_Growth_6mo_21390_428792 <- ifelse(cc_default_df$Bal_Growth_6mo>= 21389.95 & cc_default_df$Bal_Growth_6mo< 428792, '1','0')
table(cc_default_df$Bal_Growth_6mo_neg708323_neg122)
table(cc_default_df$Bal_Growth_6mo_neg123_21389)
table(cc_default_df$Bal_Growth_6mo_21390_428792)
cc_default_df$Bal_Growth_6mo_neg708323_neg122 <- as.factor(cc_default_df$Bal_Growth_6mo_neg708323_neg122)
cc_default_df$Bal_Growth_6mo_neg123_21389 <- as.factor(cc_default_df$Bal_Growth_6mo_neg123_21389)
cc_default_df$Bal_Growth_6mo_21390_428792 <- as.factor(cc_default_df$Bal_Growth_6mo_21390_428792)
str(cc_default_df)
#--------------------------------------------------Util_Growth_6mo
#------------------utility vectors
cc_default_df$Util_Chx1 <- NA
cc_default_df$Util_Chx2 <- NA
cc_default_df$Util_Chx3 <- NA
cc_default_df$Util_Chx4 <- NA
cc_default_df$Util_Chx5 <- NA
cc_default_df$Util_Chx1 <- (cc_default_df$Util2 - cc_default_df$Util1) #Calc Util2-Util1
cc_default_df$Util_Chx2 <- (cc_default_df$Util3 - cc_default_df$Util2) #Calc Util3-Util2
cc_default_df$Util_Chx3 <- (cc_default_df$Util4 - cc_default_df$Util3) #Calc Util4-Util3
cc_default_df$Util_Chx4 <- (cc_default_df$Util5 - cc_default_df$Util4) #Calc Util5-Util4
cc_default_df$Util_Chx5 <- (cc_default_df$Util6 - cc_default_df$Util5) #Calc Util6-Util5
#-----------------Sum Utility changes
cc_default_df$Util_Growth_6mo <- NA
str(cc_default_df)
cc_default_df$Util_Growth_6mo <- apply(cc_default_df[79:83], 1, sum) #add the changes
summary(cc_default_df$Util_Growth_6mo)
plot(cc_default_df$Util_Growth_6mo)
#---------------bin Utilization Growth
woe.binning(df=train_df,target.var=c('DEFAULT'),pred.var=c('Util_Growth_6mo')) #USED TRAINING DATA TO DETERMINE BINS
summary(train_df$Util_Growth_6mo)
#(-Inf,-0.72262]
#(-0.72262,-0.00075]
#(-0.00075,0]
#(0,0.02930625]
#(0.02930625, Inf]
cc_default_df$Util_Growth_6mo_Neg5_NegPt7 <- ifelse(cc_default_df$Util_Growth_6mo < -0.72262, '1','0')
cc_default_df$Util_Growth_6mo_NegPt7_NegPt00075 <- ifelse(cc_default_df$Util_Growth_6mo >= -0.72262 & cc_default_df$Util_Growth_6mo < -0.00075, '1','0')
cc_default_df$Util_Growth_6mo_NegPt00075_0 <- ifelse(cc_default_df$Util_Growth_6mo >= -0.00075 & cc_default_df$Util_Growth_6mo < 0, '1','0')
cc_default_df$Util_Growth_6mo_0_Pt029 <- ifelse(cc_default_df$Util_Growth_6mo >= 0 & cc_default_df$Util_Growth_6mo < 0.029, '1','0')
cc_default_df$Util_Growth_6mo_Pt029_2 <- ifelse(cc_default_df$Util_Growth_6mo >= 0.029 & cc_default_df$Util_Growth_6mo <2, '1','0')
cc_default_df$Util_Growth_6mo_Neg5_NegPt7 <- as.factor(cc_default_df$Util_Growth_6mo_Neg5_NegPt7)
cc_default_df$Util_Growth_6mo_NegPt7_NegPt00075 <- as.factor(cc_default_df$Util_Growth_6mo_NegPt7_NegPt00075)
cc_default_df$Util_Growth_6mo_NegPt00075_0 <- as.factor(cc_default_df$Util_Growth_6mo_NegPt00075_0)
cc_default_df$Util_Growth_6mo_0_Pt029 <- as.factor(cc_default_df$Util_Growth_6mo_0_Pt029)
cc_default_df$Util_Growth_6mo_Pt029_2 <- as.factor(cc_default_df$Util_Growth_6mo_Pt029_2)
#---------------------------------------------------- Max Bill Amount
cc_default_df$Max_Bill_Amt <- apply(cc_default_df[13:18], 1, max)
#-----------------------------------------------------Max Payment Amount
cc_default_df$Max_Pmt_Amt <- apply(cc_default_df[19:24], 1, max)
#----------------------------------------------------Max Delinquency
cc_default_df$Max_DLQ <- NA
cc_default_df$Max_DLQ <- apply(cc_default_df[7:12], 1, max) #using PAY_1 - PAY_6
cc_default_df$Max_DLQ <- as.numeric(cc_default_df$Max_DLQ)
#-----------------------------------------------------Max Utilization
cc_default_df$Max_Util <- NA
cc_default_df$Max_Util <- apply(cc_default_df[58:63], 1, max) #using Util Chx 1-5
cc_default_df$Max_Util <- as.numeric(cc_default_df$Max_Util)
str(cc_default_df)
table(cc_default_df$DEFAULT)
cat("\n","------------------ SPLIT DATA --------------","\n")
str(cc_default_df)
train_df <- cc_default_df[cc_default_df$train == 1, c(3:5, 7:12, 25, 31:36, 38:42, 44:46, 53:57, 65:69, 76:83, 85:93)] #split keeping only wanted col
n.train <- dim(train_df)[1] # 15,180
count_if('1', cc_default_df$train) #confirms train_df matches original
test_df <- cc_default_df[cc_default_df$test == 1, c(3:5, 7:12, 25, 31:36, 38:42, 44:46, 53:57, 65:69, 76:83, 85:93)] #split keeping only wanted col
n.test <- dim(test_df)[1] # 7323
count_if('1', cc_default_df$test) #confirms test_df matches original
validate_df <- cc_default_df[cc_default_df$validate == 1, c(3:5, 7:12, 25, 31:36, 38:42, 44:46, 53:57, 65:69, 76:83, 85:93)] #split keeping only wanted col
n.validate <- dim(validate_df)[1] # 7497
count_if('1', cc_default_df$validate) #confirms train_df matches original
str(train_df)
table(cc_default_df$DEFAULT)
cat("\n","------------------ EDA ON ENGINEERED VARIABLES --------------","\n")
# summary stat table
file.name <- 'Stat_Table.html';
stargazer(train_df, type=c('html'),out=paste(out.path,file.name,sep=''),
title=c('Table 1: Summary Statistics for Default of Credit Card Clients Data'),
align=TRUE, digits=2, digits.extra=2, initial.zero=TRUE, median=TRUE)
# summary of factor variables
table(train_df$SEX)
table(train_df$EDUCATION)
table(train_df$MARRIAGE)
table(train_df$AGE_0_24)
table(train_df$AGE_25_32)
table(train_df$AGE_33_80)
table(train_df$LIMIT_BAL_Neg_29999)
table(train_df$LIMIT_BAL_30000_159999)
table(train_df$LIMIT_BAL_160000_1000000)
table(train_df$Avg_Bill_Amt_Neg_56050_188)
table(train_df$Avg_Bill_Amt_189_2846)
table(train_df$Avg_Bill_Amt_2847_7488)
table(train_df$Avg_Bill_Amt_7489_31915)
table(train_df$Avg_Bill_Amt_31916_900000)
table(train_df$Avg_Pmt_Amt_0_2832)
table(train_df$Avg_Pmt_Amt_2833_12092)
table(train_df$Avg_Pmt_Amt_12093_627344)
table(train_df$Avg_Pmt_Ratio_Neg16429_pt02)
table(train_df$Avg_Pmt_Ratio_pt03_pt15)
table(train_df$Avg_Pmt_Ratio_pt16_1)
table(train_df$Avg_Pmt_Ratio_1_1pt17)
table(train_df$Avg_Pmt_Ratio_1pt18_2688)
table(train_df$Avg_Util_negpt2326_pt00095)
table(train_df$Avg_Util_pt00096_pt0091)
table(train_df$Avg_Util_pt0092_pt3673)
table(train_df$Avg_Util_pt3674_pt8231)
table(train_df$Avg_Util_pt8232_6)
table(train_df$Bal_Growth_6mo_neg708323_neg1)
table(train_df$Bal_Growth_6mo_neg123_21389)
table(train_df$Bal_Growth_6mo_21390_428792)
summary(train_df$Util_Growth_6mo)
summary(train_df$Max_Bill_Amt)
summary(train_df$Max_Pmt_Amt)
summary(train_df$Max_DLQ)
summary(train_df$Med_Bill)
################################### Continuous variables
# graphs of continuous variables
#par(mfrow=c(1,1))
aba <- cc_default_df[,c(25, 27, 37)]
head(aba2)
aba2 <- aba[aba$train==1, 3]
#------------------------ Avg_Bill_Amt
hist(aba2, xlab = '', main="Histogram of Avg_Bill_Amt", col="blue")
boxplot(aba2,xlab = '',main="Boxplot of Avg_Bill_Amt",col="blue")
quantile(aba2, c(0, 0.01, 0.03, 0.05, 0.25, 0.5, 0.75, 0.95, 0.97, 0.99, 1))
plot(train_df$Avg_Pmt_Ratio, na.rm = TRUE)
round(skewness(aba2),2)
round(kurtosis(aba2),2)
#------------------------ Avg_Pmt_Ratio
Avg_Pmt_Ratio_Range <- range(cc_default_df$Avg_Pmt_Ratio)
hist(cc_default_df$Avg_Pmt_Ratio, xlab = '', main="Histogram of Avg_Pmt_Ratio", col="blue")
boxplot(cc_default_df$Avg_Pmt_Ratio,xlab = '',main="Boxplot of Avg_Pmt_Ratio",col="blue")
quantile(cc_default_df$Avg_Pmt_Ratio, c(0, 0.01, 0.03, 0.05, 0.25, 0.5, 0.75, 0.95, 0.97, 0.99, 1))
plot(cc_default_df$Avg_Pmt_Ratio, na.rm = TRUE)
#------------------------
hist(train_df$Util_Growth_6mo, xlab = '', main="Histogram of Util_Growth_6mo", col="blue")
boxplot(train_df$Util_Growth_6mo,xlab = '',main="Boxplot of Util_Growth_6mo",col="blue")
quantile(train_df$Util_Growth_6mo, c(0, 0.01, 0.03, 0.05, 0.25, 0.5, 0.75, 0.95, 0.97, 0.99, 1))
round(skewness(train_df$Util_Growth_6mo),2)
round(kurtosis(train_df$Util_Growth_6mo),2)
#------------------------
hist(train_df$Max_Bill_Amt, xlab = '', main="Histogram of Max_Bill_Amt", col="blue")
boxplot(train_df$Max_Bill_Amt,xlab = '',main="Boxplot of Max_Bill_Amt",col="blue")
quantile(train_df$Max_Bill_Amt, c(0, 0.01, 0.03, 0.05, 0.25, 0.5, 0.75, 0.95, 0.97, 0.99, 1))
round(skewness(train_df$Max_Bill_Amt),2)
round(kurtosis(train_df$Max_Bill_Amt),2)
#------------------------
hist(train_df$Max_Pmt_Amt, xlab = '', main="Histogram of Max_Pmt_Amt", col="blue")
boxplot(train_df$Max_Pmt_Amt,xlab = '',main="Boxplot of Max_Pmt_Amt",col="blue")
quantile(train_df$Max_Pmt_Amt, c(0, 0.01, 0.03, 0.05, 0.25, 0.5, 0.75, 0.95, 0.97, 0.99, 1))
round(skewness(train_df$Max_Pmt_Amt),2)
round(kurtosis(train_df$Max_Pmt_Amt),2)
# #------------------------graphs of factor variables
ggplot(train_df, aes(SEX, DEFAULT))+
geom_bar(aes(fill=SEX), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF SEX VARIABLE")
ggplot(train_df, aes(EDUCATION))+
geom_bar(aes(fill=EDUCATION), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF EDUCATION VARIABLE")
ggplot(train_df, aes(MARRIAGE))+
geom_bar(aes(fill=MARRIAGE), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF MARRIAGE VARIABLE")
ggplot(train_df, aes(DEFAULT))+
geom_bar(aes(fill=DEFAULT), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF DEFAULT")
par(mfrow=c(1,1))
ggplot(train_df, aes(LIMIT_BAL_Neg_29999))+
geom_bar(aes(fill=DEFAULT), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF LIMIT_BAL_Neg_29999")
ggplot(train_df, aes(LIMIT_BAL_30000_159999))+
geom_bar(aes(fill=DEFAULT), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF LIMIT_BAL_30000_159999")
ggplot(train_df, aes(LIMIT_BAL_160000_1000000))+
geom_bar(aes(fill=DEFAULT), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF LIMIT_BAL_160000_1000000")
x <- ifelse(train_df$LIMIT_BAL_Neg_29999==1 & train_df$DEFAULT==1, "1", "na")
table(x)
z <- ifelse(train_df$LIMIT_BAL_30000_159999==1 & train_df$DEFAULT==1, "1", "na")
table(z)
v <- ifelse(train_df$LIMIT_BAL_160000_1000000==1 & train_df$DEFAULT==1, "1", "na")
table(v)
ggplot(train_df, aes(Avg_Bill_Amt_Neg_56050_188))+
geom_bar(aes(fill=Avg_Bill_Amt_Neg_56050_188), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF Avg_Bill_Amt_Neg_56050_188")
ggplot(train_df, aes(Avg_Bill_Amt_189_2846))+
geom_bar(aes(fill=Avg_Bill_Amt_189_2846), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF Avg_Bill_Amt_189_2846")
ggplot(train_df, aes(Avg_Bill_Amt_2847_7488))+
geom_bar(aes(fill=Avg_Bill_Amt_2847_7488), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF Avg_Bill_Amt_2847_7488")
ggplot(train_df, aes(Avg_Bill_Amt_7489_31915))+
geom_bar(aes(fill=Avg_Bill_Amt_7489_31915), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF Avg_Bill_Amt_7489_31915")
ggplot(train_df, aes(Avg_Bill_Amt_31916_900000))+
geom_bar(aes(fill=Avg_Bill_Amt_31916_900000), width = 0.5) +
theme(axis.text.x = element_text(angle=90, vjust=0)) +
labs(title="BAR CHART OF Avg_Bill_Amt_31916_900000")
a <- ifelse(train_df$Avg_Bill_Amt_Neg_56050_188==1 & train_df$DEFAULT==1, "1", "na")
table(a)
b <- ifelse(train_df$Avg_Bill_Amt_189_2846==1 & train_df$DEFAULT==1, "1", "na")
table(b)
c <- ifelse(train_df$Avg_Bill_Amt_2847_7488==1 & train_df$DEFAULT==1, "1", "na")
table(c)
d <- ifelse(train_df$Avg_Bill_Amt_7489_31915==1 & train_df$DEFAULT==1, "1", "na")
table(d)
e <- ifelse(train_df$Avg_Bill_Amt_31916_900000==1 & train_df$DEFAULT==1, "1", "na")
table(e)
print(table(a, b, c, d, e))
f <- subset(train_df, AGE_33_80 ==1 & train_df$DEFAULT==1, select="DEFAULT")
table(f) #1947
g <- subset(train_df, AGE_0_24 ==1 & train_df$DEFAULT==1, select="DEFAULT")
table(g) #379
h <- subset(train_df, AGE_25_32 ==1 & train_df$DEFAULT==1, select="DEFAULT")
table(h) #1097
graphics.off()
cat("\n","------------------ Correlations --------------","\n")
library(corrplot)
# Correlation Table
str(train_df)
par(mfrow=c(1,1))
corr=cor(cc_default_df[,c(2,6, 37, 43, 52, 64, 75, 84, 90:93)])
corrplot(corr,method="color", outline=T, cl.pos="n", rect.col="black",
tl.col="indianred4", addCoef.col="black", number.digits=2, number.cex=0.60,
tl.cex=0.7, cl.cex=1, col=colorRampPalette(c("green4", "white", "red"))(100))
# produce list of correlations by highest value
corr[lower.tri(corr,diag=TRUE)]=NA #Prepare to drop duplicates and meaningless information
corr=as.data.frame(as.table(corr)) #Turn into a 3-column table
corr=na.omit(corr) #Get rid of the junk we flagged above
corr=corr[order(-abs(corr$Freq)),] #Sort by highest correlation (whether +ve or -ve)
corr
graphics.off()
cat("\n","------------------ Model Based EDA --------------","\n")
#------------------------------------------------------------------ Naive Bayes MODEL
library(naivebayes)
nb <- naive_bayes(DEFAULT~., train_df_encod)
nb
summary(nb)
names(nb)
nb$levels
nb$tables
nb$prior
plot(nb)
predicted_class <- predict(nb, type=c('prob'))
pct.acc <- mean(predicted_class==train_df_encod$DEFAULT)
#------------------------------------------------------------rpart Tree & formatting
#https://blog.revolutionanalytics.com/2013/06/plotting-classification-and-regression-trees-with-plotrpart.html
library(party) # Alternative decision tree algorithm
library(partykit) # Convert rpart object to BinaryTree
library(caret)
#--------------------Define Model Formula Inputs
form <- as.formula(DEFAULT ~ .)
form1a <- as.formula(DEFAULT ~ (LIMIT_BAL*Max_Util*Avg_Util) + SEX + EDUCATION + MARRIAGE +
(Avg_Bill_Amt*Max_Bill_Amt) + (Avg_Pmt_Amt*Max_Pmt_Amt) + (PAY_1*PAY_2*PAY_3*PAY_4*PAY_5*PAY_6*Max_DLQ) + (Bal_Growth_6mo*Util_Growth_6mo))
#without PAY variables
form2 <- as.formula(DEFAULT ~ SEX + EDUCATION + MARRIAGE + LIMIT_BAL_Neg_29999 + LIMIT_BAL_30000_159999 +
LIMIT_BAL_160000_1000000 + AGE_0_24 + AGE_25_32 + AGE_33_80 +
Avg_Bill_Amt_Neg_56050_188 + Avg_Bill_Amt_189_2846 + Avg_Bill_Amt_2847_7488 +
Avg_Bill_Amt_7489_31915 + Avg_Bill_Amt_31916_900000 + Avg_Pmt_Amt_0_2832 +
Avg_Pmt_Amt_2833_12092 + Avg_Pmt_Amt_12093_627344 + Avg_Pmt_Ratio_Neg16429_pt02 +
Avg_Pmt_Ratio_pt03_pt15 + Avg_Pmt_Ratio_pt16_1 + Avg_Pmt_Ratio_1_1pt17 +
Avg_Pmt_Ratio_1pt18_2688 + Avg_Util_negpt2326_pt00095 + Avg_Util_pt00096_pt0091 +
Avg_Util_pt0092_pt3673 + Avg_Util_pt3674_pt8231 + Avg_Util_pt8232_6 +
Bal_Growth_6mo_neg708323_neg122 + Bal_Growth_6mo_neg123_21389 +
Bal_Growth_6mo_21390_428792 + Util_Growth_6mo_Neg5_NegPt7 +
Util_Growth_6mo_NegPt7_NegPt00075 + Util_Growth_6mo_NegPt00075_0 + Util_Growth_6mo_0_Pt029 +
Util_Growth_6mo_Pt029_2 + Max_Bill_Amt + Max_Pmt_Amt + Max_DLQ + Util_Chx1 + Util_Chx2 + Util_Chx3 + Util_Chx4 + Util_Chx5)
#variables id as important by simple logistic regression
form2a <- as.formula(DEFAULT ~ SEX + EDUCATION +
MARRIAGE + PAY_1 + PAY_2 + PAY_3 + PAY_4 + PAY_5 +
LIMIT_BAL_Neg_29999 + LIMIT_BAL_30000_159999 +
Avg_Bill_Amt_Neg_56050_188 +
Avg_Bill_Amt_189_2846 + Avg_Bill_Amt_2847_7488 +
Avg_Bill_Amt_7489_31915 + Avg_Pmt_Amt_0_2832 +
Avg_Pmt_Amt_2833_12092 + Max_Bill_Amt + Max_DLQ)
#with pay variables and only variables used to build the tree
form3 <- as.formula(DEFAULT ~ PAY_1 + PAY_2 + PAY_3 + PAY_4 + PAY_5 + PAY_6 + Avg_Pmt_Ratio_Neg16429_pt02 + Max_DLQ)
#removed Max_DLQ
form4 <- as.formula(DEFAULT ~ PAY_1 + PAY_2 + PAY_3 + PAY_4 + PAY_5 + PAY_6 + Avg_Pmt_Ratio_Neg16429_pt02)
#addef top 6 var from RF full run
form5 <- as.formula(DEFAULT ~ PAY_1 + Max_Util + Max_Bill_Amt + Max_Pmt_Amt + Util_Chx1 + Util_Chx3 + Util_Chx2 + Util_Chx5 + Util_Chx4 + Max_DLQ)
#--------------------------------------------------------tree model 1---------------------------
#trained only with var id as important by simple log reg
tree <- rpart(form2a, data = train_df, method = 'class', minsplit=30)
summary(tree)
par(mfrow=c(1,1))
#-------------------------perf measures on training data
library(pROC)
tree_probs_train = predict(tree, train_df, type="class")
confusionMatrix(tree_probs_train, train_df$DEFAULT)
roc_tree <- roc(train_df$DEFAULT, type.convert(tree_probs_train))
auc(roc_tree)
#-----------------------perf measusres on test data
tree_probs = predict(tree, test_df, type="class")
confusionMatrix(tree_probs, test_df$DEFAULT)
roc_tree <- roc(test_df$DEFAULT, type.convert(tree_probs))
auc(roc_tree)
#------------------------visualize
plot(tree)
text(tree)
prp(tree) #plot the tree
prp(tree, varlen=30) #shorten variables
#new.tree <- prp(tree,snip=TRUE)$obj # interactively trim the tree
#prp(new.tree.1) # display the new tree
fancyRpartPlot(tree)
#-----------------------------------------------------tree model 2---------------------------
#trained with all minus pay
tree <- rpart(form2, data = train_df, method = 'class', minsplit=30)
summary(tree)
par(mfrow=c(1,1))
#-------------------------perf measures on training data
tree_probs_train = predict(tree, train_df, type="class")
confusionMatrix(tree_probs_train, train_df$DEFAULT)
roc_tree <- roc(train_df$DEFAULT, type.convert(tree_probs_train))
auc(roc_tree)
#-----------------------perf measusres on test data
tree_probs = predict(tree, test_df, type="class")
confusionMatrix(tree_probs, test_df$DEFAULT)
roc_tree <- roc(test_df$DEFAULT, type.convert(tree_probs))
auc(roc_tree)
#------------------------visualize
plot(tree)
text(tree)
prp(tree) #plot the tree
prp(tree, varlen=30) #shorten variables
#new.tree <- prp(tree,snip=TRUE)$obj # interactively trim the tree
#prp(new.tree.1) # display the new tree
fancyRpartPlot(tree)
#-----------------------------------------------------tree model 3---------------------------
#trained with all + pay
tree <- rpart(form, data = train_df, method = 'class', minsplit=30)
summary(tree)
par(mfrow=c(1,1))
#-------------------------perf measures on training data
tree_probs_train = predict(tree, train_df, type="class")
confusionMatrix(tree_probs_train, train_df$DEFAULT)
roc_tree <- roc(train_df$DEFAULT, type.convert(tree_probs_train))
auc(roc_tree)
#-----------------------perf measusres on test data
tree_probs = predict(tree, test_df, type="class")
confusionMatrix(tree_probs, test_df$DEFAULT)
roc_tree <- roc(test_df$DEFAULT, type.convert(tree_probs))
auc(roc_tree)
#------------------------visualize
plot(tree)
text(tree)
prp(tree) #plot the tree
prp(tree, varlen=30) #shorten variables
#new.tree <- prp(tree,snip=TRUE)$obj # interactively trim the tree
#prp(new.tree.1) # display the new tree
fancyRpartPlot(tree)
# --------------------------------------------------------- OneR on select features-----------------------
#variables id as important by simple logistic regression
model.1 <- OneR(form2a, data=train_df, verbose=TRUE);
summary(model.1)
plot(model.1)
#-------------------------perf measures on training data
oneR_train = predict(model.1, train_df, type="class")
confusionMatrix(oneR_train, train_df$DEFAULT)
roc_tree <- roc(train_df$DEFAULT, type.convert(oneR_train))
auc(roc_tree)
table(train_df$PAY_2, train_df$DEFAULT)
#-----------------------perf measusres on test data
oneR_test = predict(model.1, test_df, type="class")
confusionMatrix(oneR_test , test_df$DEFAULT)
roc_tree <- roc(test_df$DEFAULT, type.convert(oneR_test))
auc(roc_tree)
# -------------------------------------------- OneR 2 all plus pay-----------------------
model.2 <- OneR(DEFAULT~., data=train_df, verbose=TRUE);
summary(model.2)
#-------------------------perf measures on training data