-
Notifications
You must be signed in to change notification settings - Fork 1
/
Final_Version_1.R
1111 lines (864 loc) · 39.9 KB
/
Final_Version_1.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
### Introductory Code
library(forecast)
library(ade4)
library(e1071)
library(ggplot2)
library("rpart")
library("rpart.plot")
library(corrplot)
library(plotly)
library(dplyr)
library(reshape2)
#install.packages("nFactors")
library(nFactors)
library(nnet)
#install.packages("stargazer")
library(stargazer)
library(Metrics)
#install.packages("rattle")
#library(rattle)
library(rpart)
library(randomForest)
library(ggplot2)
library(xgboost)
library(DT)
#install.packages("pROC")
library(pROC)
library(MASS)
#install.packages("performanceEstimation")
library(performanceEstimation)
#require(rattle)
train <- read.csv('train.csv')
colnames(train)
str(train)
test <- read.csv("test.csv")
View(var_kind)
var_kind<-c("Product_Info_", "Ins_Age", "Ht", "Wt","BMI","Employment_Info_","InsuredInfo_",
"Insurance_History_", "Family_Hist_","Medical_History_", "Medical_Keyword_")
########## remove variables with excess NAs in both test and train#######
#rmNAvars<-function(dat,threshold){
# dat<-dat[, -which(colMeans(is.na(dat)) > threshold)]
#}
#?colMeans
#train_clean<-rmNAvars(train,0.3)
#?intersect
test_clean<-test[,intersect(colnames(test), colnames(train_clean))]
#View(test_clean)
train_clean<-train
################### replacing Missing value with median ###########
sort(colSums(is.na(train_clean)),decreasing = TRUE)
#str(train_clean)
manage_na <- function(datafra)
{
for(i in 1:ncol(datafra))
{
if(is.numeric(datafra[,i]))
{
datafra[is.na(datafra[,i]),i] <- median(datafra[!is.na(datafra[,i]),i])
}
}
datafra
}
#####################Function and linear model
train_clean <- manage_na(train_clean)
test_clean <- manage_na(test)
#View(train_clean)
str(train_clean)
train_conti<-train_clean[,c("Product_Info_4", "Ins_Age", "Ht", "Wt", "BMI",
"Employment_Info_1", "Employment_Info_4", "Employment_Info_6")]
#View(train_conti)
#levels(train_clean$Medical_Keyword_46)
####### converting nonnumeric column to numeric ####
train_clean[,!(sapply(train_clean,class) == "numeric" | sapply(train_clean, class) == "integer")]<-
as.numeric(train_clean[,
!(sapply(train_clean, class) == "numeric" | sapply(train_clean, class) == "integer")])
str(train_clean)
#?sapply
test_clean[,!(sapply(train_clean,class) == "numeric" | sapply(test_clean, class) == "integer")]<-
as.numeric(test_clean[,
!(sapply(test_clean, class) == "numeric" | sapply(test_clean, class) == "integer")])
## **1. Introduction**
#In a one-click shopping world, the life insurance application process is antiquated.
#Customers provide extensive information to identify risk classification and eligibility,
#including scheduling medical exams, a process that takes an average of 30 days for the purchase of
#an Insurance product. Hence, only 40% of U.S. households own individual life insurance.
#We aim to make the process of issuing life insurance quicker and less labor intensive for new and existing
#customers to get a quote while maintaining privacy boundaries. The model aims to automate the process
#of risk assessment for issue of various insurance products. This model will help the firm to generate
#more revenues by optimally targeting more profitable and less risky customers. The data set used includes
#the insurance company's earlier data-set that contains various parameters of an insurance application along
#with a risk score computed by the company's earlier internal model. The results will to better understand
#the predictive power of the data points in the existing assessment, enabling streamlining the process.
## **2. Data used**
#The model data set is pre-separated among training and test sample in the ratio of 3:1 and
#have a random sampling being done. The train data set is a transactional (low level) data consists of
#59,381 customers and 126 variables as predictors. The test dataset contains the same variables for another
#set of 19,766 customers. The variables are categorized based on the type of information they provide.
#Due to proprietary reasons the variable names have been masked, however they have been numbered within
#the category type for identification. The categories of variables present are:
#* Product Information
#* Insurance Age
#* Height
#* Weight
#* BMI
#* Employment Information
#* Insured Information
#* Insurance History
#* Family History
#* Medical History
#* Medical Keyword
#Each of these variable categories contain multiple variables they represent different items under
#that variable class.
## **3. Explanatory Data Analysis**
### **3.1 Variable Types**
#A primary data analysis was performed through visual inspection of the training data-set to identify
#the different types of variables among Continuous, Categorical (Nominal and Ordinal) and
#Dummy (Binary/Indicator) variables. This analysis helps us identify the choice of variable selection
#and reduction algorithms in the next stage of modelling.
temp1<- data.frame(Variable_Type = c("Product Information",
"Insurance Age",
"Height",
"Weight",
"BMI",
"Employment Information",
"Insured Information",
"Insurance History",
"Family History",
"Medical History",
"Medical Keyword"))
#str(train_clean)
temp1$Continous<-c(1,1,1,1,1,3,0,1,4,0,0)
temp1$Categorical<-c(6,0,0,0,0,3,7,8,1,41,0)
temp1$Dummy<-c(0,0,0,0,0,0,0,0,0,0,48)
temp1$Total<-rowSums(temp1[,-1])
temp1[12,2:5]<-colSums(temp1[,-1])
temp1$Variable_Type[12]<-"Total"
temp1$Continous
temp1$Variable_Type
temp1[,2:5]
#View(temp1)
#str(temp1)
library(data.table)
data.table(temp1,
options = list(pageLength = 13,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}")))
##Continuous variables are analyzed using summary statistics, box plots and density plots.
##The categorical variables are analyzed using event rate chart to track the variation to the response.
### **3.2 Histogram of Response plot**
#The response is a nominal variable with levels from 1 to 8 and associates to the risk level of a customer.
p<-ggplot(train, aes(x=Response))+ geom_histogram(fill="Red", alpha=0.3)
p
ggplotly(p, color=~Response, width = 800, height = 400)%>%
layout(title="Distribution of Response Variable",
plot_bgcolor= "white",
xaxis=list(gridcolor="lightgrey", opacity=0.5),
yaxis=list(gridcolor="lightgrey",opacity = 0.5),
autosize = T, width = 800, height = 400)
#While it is not mentioned whether the scale is in increasing order of riskiness or otherwise,
#from the distribution of the response variable we can infer that 8 could possibly refer to less
#risky customers
missing_prct<-data.frame(variable=colnames(train),
missing=sapply(train,
function(x){
sum(is.na(x))
}/nrow(train)
)
)
missing_prct_test<-data.frame(variable=colnames(test),
missing=sapply(test,
function(x){
sum(is.na(x))
}/nrow(test)
)
)
#missing_prct
#missing_prct_test
### **3.3 Summary Statistics**
#To allow for easier convergence of machine learning algorithms variables are normalized to the
#range of [0, 1]. The most common normalizing function used is given below:
#X_{norm} = frac{x_{i}-x_{min}}{x_{max}-x_{min}}
#The same function had been applied to the continuous variables in the input data-set.
#The summary statistics help understand the distribution of the underlying dataset,
#the box plots and density plots enable visualizing the data-set
## Generating Summary Table
summ_conti<-data.frame(Variables = colnames(train_conti))
summ_conti$Min<-apply(train_conti,2,function(x){min(x, na.rm = T)})
summ_conti$Max<-apply(train_conti,2,function(x){max(x, na.rm = T)})
summ_conti$Mean<-apply(train_conti,2,function(x){mean(x, na.rm = T)})
summ_conti$Median<-apply(train_conti,2,function(x){median(x, na.rm = T)})
datatable(summ_conti, options = list(initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}")))
### **3.4 Continuous Variable Analysis** {.tabset .tabset-fade}
#### Box Plots
#The box plots enable visualization of the data-set especially in relation to outliers.
#However considering the large number of data
#plt<-htmltools::tagList()
#temp1<-train_conti[1:5]
p<-plot_ly( data=melt(temp1), type = "box",
split = ~variable,y = ~value)%>%
layout( title = "Box-Plots of variables")
p
#plt[[1]] <- as_widget(p)
p<-plot_ly( data=melt(train_conti[,6:length(train_conti)]), type = "box",
split = ~variable,y = ~value)%>%
layout( title = "Box-Plots of variables")
p
#plt[[2]] <- as_widget(p)
#plt
#### Density Plot
#View(train_conti$Employment_Info_4)
#temp1$Ht
#The density plots help visualize the characteristics of the distribution
#including statistical metrics such as mean, standard deviation and kurtosis.
#It also enables us to visually identify if any relationship exists with the response variable.
#For example: The density plot of variable Employment_Info_6 is similar to the histogram of the
#response variable, this probably indicated that this variable could be a good predictor of the
#response variable
#train_conti[,1:2]
temp_melt<-melt(train_conti[,1:2])
#str(train_conti)
p1<-ggplot(temp_melt,aes(value, fill = variable ))+geom_density(alpha = 0.5)+ggtitle("Density Plots")
p1
ggplotly(p1, height= 800, width = 1000)%>%
layout(plot_bgcolor="transparent",paper_bgcolor= "transparent",autosize = F, width = 1000, height = 800)
temp_melt<-melt(train_conti[,c(3,4,5)])
p2<-ggplot(temp_melt,aes(value, fill = variable ))+geom_density(alpha = 0.5)+ggtitle("Density Plots")
p2
ggplotly(p2, height= 800, width = 1000)%>%
layout(plot_bgcolor="transparent",paper_bgcolor= "transparent",autosize = F, width = 1000, height = 800)
temp_melt<-melt(train_conti[,c(6,8)])
p3<-ggplot(temp_melt,aes(value, fill = variable ))+geom_density(alpha = 0.5)+ggtitle("Density Plots")
p3
ggplotly(p3, height= 800, width = 1000)%>%
layout(plot_bgcolor="transparent",paper_bgcolor= "transparent",autosize = F, width = 1000, height = 800)
temp_melt<-melt(train_conti[,7])
temp_melt$variable<-"Employment_Info_4"
p4<-ggplot(temp_melt,aes(value, fill = variable ))+geom_density(alpha = 0.5)+ggtitle("Density Plots")
p4
ggplotly(p4, height= 800, width = 1000)%>%
layout(plot_bgcolor="transparent",paper_bgcolor= "transparent",autosize = F, width = 1000, height = 800)
### **3.5 Missing Value Analysis** {.tabset .tabset-fade}
#### Missing Value Plots {.tabset #missing1}
#Missing value percentage charts evaluate whether the variable has sufficient
#number of data records for predictions. The plot presents the percentage of observations missing for each variable
par(mfrow=c(2,2))
for(i in var_kind){
plot(x=as.factor(as.character(missing_prct[grep(i, row.names(missing_prct)),1])),
y=missing_prct$missing[grep(i, row.names(missing_prct))], ylim = c(0,1),
main =gsub("_"," ",i))
}
#### Missing vs Response Chart
#This chart enables us to identify whether variables with a
#high percentage of missing values actually help in predicting the response variable.
#The distribution missing values for each response category has been retained within the
#variable and hence the missing values are random in nature.
train_na_response <- sapply(sort(unique(train$Response)), function(x) {
apply(train[train$Response == x, ], 2, function(y) { sum(is.na(y)) }) })
train_na_response<-data.frame(train_na_response)
train_na_response<-train_na_response[which(rowSums(train_na_response)>0),]
train_na_response$ID<-rownames(train_na_response)
train_na_response_melt<-melt(train_na_response)
plot_ly(train_na_response_melt, x = ~ID, y =~value , color = ~variable)%>%
layout(title ="Missing vs Response Chart")
### **3.6 Event Rate Chart**
#In an attempt to capture the conditional probability of the response given a specific bin
#of the categorical variable
#P(y=1|ProdInfo_2= A_1)=\frac{P(y=1 \cap ProdInfo_2= A_1 )}{P(ProdInfo_2= A_1)}
#### Product Information
train_categ<-train_clean[,-which(colnames(train_clean) %in% colnames(train_conti))]
i="Product_Info"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
#plt
#### Employment Information
i="Employment_Info"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
plt
#### Insured Information
i="InsuredInfo"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
plt
#### Insurance History
i="Insurance_History"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
plt
#### Medical History
par(mfrow=c(2,2))
i="Medical_History"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
plt
#### Medical Keyword
i="Medical_Keyword"
train_temp<-train_categ[,grep(i,colnames(train_categ))]
index<-1
plt<-htmltools::tagList()
for (i in colnames(train_temp)){
data_freq<-as.data.frame(table(train_temp[,i],train_clean$Response)/(as.data.frame(table(train_temp[,i]))[,2]))
p<-plot_ly(data_freq, x = ~Var1, y = ~Freq, color = ~Var2, type="bar")%>%
layout(title = paste0("Event Rate Chart- ",gsub("_"," ",i)),
xaxis = list(title = gsub("_"," ",i),showgrid = T))
plt[[index]] <- as_widget(p)
index <- index + 1
}
plt
### **3.7 Correlation Plots** {.tabset .tabset-fade #corr}
#After data analysis and data treatment, the next stage of model development would be variable reduction.
#### Product Information
i="Product_Info"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),119)])
corrplot(m, method = "number", type="lower")
}
#?corrplot
#corrplot(train_conti,method = "circle")
#### Employment Information
i="Employment_Info"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),119)])
corrplot(m, method = "number", type="lower")
}
#### Insured Information
i="InsuredInfo_"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),119)])
corrplot(m, method = "number", type="lower")
}
#### Insurance History
i="Insurance_History"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),119)])
corrplot(m, method = "number", type="lower")
}
#### Medical History
i="Medical_History"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),119)])
corrplot(m, method = "circle", type="lower")
}
### Medical Keyword
i="Medical_Keyword"
if(class(train_clean[,grep(i, colnames(train_clean))])=="data.frame"){
m<-cor(train_clean[,c(grep(i, colnames(train_clean)),118)])
corrplot(m, method = "circle", type="lower")
}
## **4. Variable treatment**
### **4.1 Missing data treatment**
#As a preliminary step in data treatment, variables that have a high percentage
#of missing values are removed. While the threshold for removal is user determined,
#for this exercise the threshold was 30%.
### **4.2 Missing Value treatment**
#For the variables that are not dropped at the previous step of modeling,
#variables that have missing values in lesser percentages are imputed.
#The methodology used for imputation is using median of the remaining data series.
#This is a commonly used industry practice and is efficient as the missing data for all variables
#is randomly distributed over the response variable.
View(train)
### **4.3 PCA** {.tabset .tabset-fade #PCA}
#####################Tried by Ganesh #########################
#### Product Information
train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
i="Product_Info"
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
summary(pc)
plot(pc)
View(pc$scores)
loadings(pc)
var(mydata)
pc$loadings###95 % data
optimal_PCA<-6
#View(pc$scores[,1:6])
#mydata_hat<-predict(pc, as.data.frame(mydata))
#View(mydata_hat)
train_apca1<-cbind(train_apca,pc$scores[,1:optimal_PCA])
colnames(train_apca1)[grepl("PC",colnames(train_apca1))]<-paste0(i,1:optimal_PCA)
View(train_apca1)
#plotnScree(nS)
#### Employment Information
#train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
i="Employment_Info"
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
#summary(pc)
#pc$scores
plot(pc)
#loadings(pc)
#var(mydata)
#pc$loadings
optimal_PCA<-5
#mydata_hat<-predict(pc, as.data.frame(mydata))
train_apca2<-cbind(train_apca1,pc$scores[,1:optimal_PCA])
#View(train_apca)
colnames(train_apca2)[grepl("PC",colnames(train_apca2))]<-paste0(i,1:optimal_PCA)
#### Insured Information
#train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
i="InsuredInfo_"
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
#summary(pc)
plot(pc)
#loadings(pc)
#var(mydata)
#pc$loadings
optimal_PCA<-6
#mydata_hat<-predict(pc, as.data.frame(mydata))
train_apca3<-cbind(train_apca2,pc$scores[,1:optimal_PCA])
#View(train_apca)
colnames(train_apca3)[grepl("PC",colnames(train_apca3))]<-paste0(i,1:optimal_PCA)
#### Insurance History
#train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
i="Insurance_History"
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
#summary(pc)
plot(pc)
#loadings(pc)
#var(mydata)
#pc$loadings
optimal_PCA<-6
#mydata_hat<-predict(pc, as.data.frame(mydata))
train_apca4<-cbind(train_apca3,pc$scores[,1:optimal_PCA])
#View(train_apca)
colnames(train_apca4)[grepl("PC",colnames(train_apca4))]<-paste0(i,1:optimal_PCA)
#### Medical History
i="Medical_History"
#train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
#summary(pc)
#plot(pc)
#loadings(pc)
#var(mydata)
#pc$loadings
optimal_PCA<-35
#mydata_hat<-predict(pc, as.data.frame(mydata))
train_apca5<-cbind(train_apca4,pc$scores[,1:optimal_PCA])
#View(train_apca)
colnames(train_apca5)[grepl("PC",colnames(train_apca5))]<-paste0(i,1:optimal_PCA)
#### Medical Keyword
i="Medical_Keyword"
#train_apca<-data.frame(ID=train_clean$Id)
#View(train_apca)
mydata<-train_clean[,grep(i, colnames(train_clean))]
pc <- princomp(mydata,cor='TRUE')
#summary(pc)
#plot(pc)
#loadings(pc)
#var(mydata)
#pc$loadings
optimal_PCA<-46
#mydata_hat<-predict(pc, as.data.frame(mydata))
train_apca6<-cbind(train_apca5,pc$scores[,1:optimal_PCA])
#View(train_apca)
colnames(train_apca6)[grepl("PC",colnames(train_apca6))]<-paste0(i,1:optimal_PCA)
####
View(train_apca6)
Response<-train_clean$Response
train_apca6<-cbind(train_apca6,Response)
train_apca6$random <- runif(nrow(train_apca6))
train_apca6_70 <- train_apca6[train_apca6$random <= 0.7,]
train_apca6_30 <- train_apca6[train_apca6$random > 0.7,]
train_apca6<-train_apca6[,-c(106,107)]
temp1<- data.frame(Variable_Type = c("Product Information",
"Insurance Age",
"Height",
"Weight",
"BMI",
"Employment Information",
"Insured Information",
"Insurance History",
"Family History",
"Medical History",
"Medical Keyword"))
names(train_apca6)
temp1$Prior_to_PCA<-c(7,1,1,1,1,6,7,7,1,37,48)
temp1$After_PCA<-c(6,1,1,1,1,5,6,6,1,35,46)
colSums(temp1[1,2,drop=FALSE])
temp1[12,2:3]<-colSums(temp1[,-1])
temp1$Variable_Type[12]<-"Total"
datatable(temp1, options = list(pageLength = 13,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}")))
library(DT)
########################Linear regression
nonlintra <- function(y)
{
hardcoded_values <- c(-1.6, 0.7, 0.3, 3.15, 4.53, 6.5, 6.77, 9.0)
return(hardcoded_values[y])
}
y <- nonlintra(train_clean$Response)
qbmic <- 0.8
qbmic2 <- 0.9
model <- return_model(tra, test, 'lm')
#**The dimension of data of post PCA is reduced from `r length(train_clean[,1])` * `r length(train_clean)` to `r length(train_apca[,1])` * `r length(train_apca)`**
### **4.4 Boruta Classification Algorithm**
#Boruta is an all relevant feature selection wrapper algorithm.
#The method performs a top-down search for relevant features by comparing original attributes'
#importance with importance achievable at random. It computes this by using permuted copies of
#features, and progressively eliminating irrelevant features. All the atributes post PCA are deemed
#important by the algorithm.
## **5. Methodology**
### **5.1 Multinominal Logistict Model**
#We start by dividing the data inot two portions and using one portion to predict the model and the other portion for model validation and insample prediction.
#View(train_apca)
train_apca6$Response<-train$Response
train_apca6$random <- runif(nrow(train_apca6))
write.csv(train_apca6,'file1.csv')
train_apca_70 <- train_apca6[train_apca6$random <= 0.7,]
train_apca_30 <- train_apca6[train_apca6$random > 0.7,]
train_apca_90 <- train_apca6[train_apca6$random <= 0.9,]
train_apca_10<-train_apca6[train_apca6$random > 0.9,]
#View(train_apca_70)
#View(train_apca_30)
#We look at distribution of response on both the portions of train data.
temp1<-data.frame(round(table(train_apca_70$Response)/nrow(train_apca_70),2))
round(table(train_apca_70$Response)/nrow(train_apca_70),2)
View(temp1)
temp1$Freq_test<-as.data.frame(round(table(train_apca_30$Response)/nrow(train_apca_30),2))[,2]
round(table(train_apca_30$Response)/nrow(train_apca_30),2)
#### Comparing distribution of test and train data
plot_ly(data = temp1, x=~Var1,y=~Freq, type = "bar", name="Train")%>%
add_trace(y=~Freq_test, name="Test")%>%
layout(barmode = 'group')
colnames(train_apca_70)
#The distribution looks same for both the portions therefore we assume that both the portions follow similar distribution.
nodel.mlm <- multinom(Response ~ ., data = train_apca_70)
#str(train_apca_70)
# summary(nodel.mlm)
train_apca_30$Prediction<-predict(nodel.mlm, newdata=train_apca_30, type="class")
kappa_multi<-ScoreQuadraticWeightedKappa(as.numeric(train_apca_30$Prediction),as.numeric(train_apca_30$Response))
auc_multi<-multiclass.roc(train_apca_30$Response,as.numeric(train_apca_30$Prediction),levels = levels(factor(train_apca_30$Response)))
accuracy_multi<-(classificationMetrics(train_apca_30$Response,as.numeric(train_apca_30$Prediction)))[1]
accuracy_multi
str(auc_multi)
#### Performance Metrics
#The Kappa score for this multinominal model is `
a<-ScoreQuadraticWeightedKappa(as.numeric(train_apca_30$Prediction),as.numeric(train_apca_30$Response))
a
library(pROC)
### **5.2 Random Forest**
#Here as well we start by dividing the data inot two portions and using one portion to predict the model and the other portion for model validation and insample prediction. We will be using the entire data set for prediction.
train_clean$random <- runif(nrow(train_clean))
train_clean_70 <- train_clean[train_clean$random <= 0.7,]
train_clean_30 <- train_clean[train_clean$random > 0.7,]
train_clean_90 <- train_clean[train_clean$random <= 0.9,]
train_clean_10 <- train_clean[train_clean$random <= 0.1,]
test_clean$random <- runif(nrow(test_clean))
dim(test_clean)
#We look at distribution of response on both the portions of train data.
temp1<-data.frame(round(table(train_clean_70$Response)/nrow(train_clean_70),2))
round(table(train_clean_70$Response)/nrow(train_clean_70),2)
temp1$Freq_test<-as.data.frame(round(table(train_clean_30$Response)/nrow(train_clean_30),2))[,2]
round(table(train_clean_30$Response)/nrow(train_clean_30),2)
#### Comparing distribution of test and train data
plot_ly(data = temp1, x=~Var1,y=~Freq, type = "bar", name="Train")%>%
add_trace(y=~Freq_test, name="Test")%>%
layout(barmode = 'group')
#The distribution looks same for both the portions therefore we assume that
#both the portions follow similar distribution.
#model <- randomForest(Response~., data = train_clean_70[,-c(1,120)], importance = TRUE)
model<-rpart(Response~., data = train_clean_70[,-c(1,120)])
train_clean_30$Prediction <- as.integer(round(predict(model, train_clean_30)))
kappa_rf<-ScoreQuadraticWeightedKappa(train_clean_30$Prediction,as.numeric(train_clean_30$Response))
auc_rf<-multiclass.roc(train_clean_30$Prediction,
as.numeric(train_clean_30$Response),
levels = levels(factor(train_clean_30$Response)))
library(pROC)
accuracy_rf<-classificationMetrics(train_clean_30$Prediction,
as.numeric(train_clean_30$Response))[1]
install.packages("ranger")
library(ranger)
score.cv<-as.numeric()
vec.n.trees <- as.numeric()
vec.n.mtry <-as.numeric()
for (n.trees in seq(500, 5500, by = 1000)){
for (n.mtry in seq(12, 120, by = 24)){
set.seed(888)
md.rf <- ranger(Response ~.
, data = train_clean_90
, num.trees = n.trees
, mtry = n.mtry
, importance = "impurity"
, write.forest = T
, min.node.size = 20
, num.threads = 8
, verbose = T
)
pred.train <- predict(md.rf, train_clean_90)
pred.train <- predictions(pred.train)
# pred.train <- as.integer(pred.train)
#pred.valid <- predict(md.rf, dt.valid)
#pred.valid <- predictions(pred.valid)
# pred.valid <- as.integer(pred.valid)
pred.test <- predict(md.rf, train_clean_10)
pred.test <- predictions(pred.test)
# pred.test <- as.integer(pred.test)
cat("optimising the cuts on pred.train ...\n")
SQWKfun <- function(x = seq(1.5, 7.5, by = 1)){
cuts <- c(min(pred.train), x[1], x[2], x[3], x[4], x[5], x[6], x[7], max(pred.train))
pred <- as.integer(cut2(pred.train, cuts))
err <- ScoreQuadraticWeightedKappa(pred, train_clean_90$Response, 1, 8)
return(-err)
}
optCuts <- optim(seq(1.5, 7.5, by = 1), SQWKfun)
optCuts
cat("applying optCuts on valid ...\n")
cuts.test <- c(min(pred.test), optCuts$par, max(pred.test))
pred.test.op <- as.integer(cut2(pred.test, cuts.test))
score <- ScoreQuadraticWeightedKappa(train_clean_10$Response, pred.test.op)
# [1] 0.6265719 (originally .550676; num.trees = 500, regression tree on raw features)
# [1] 0.5286518 (originally .5286518; num.trees = 500, classification tree on raw features)
# [1] 0.6444031 (originally .5558768; num.trees = 5000, regression tree on raw features)
print(paste("-------- n.trees:", n.trees, "; mtry:", n.mtry, "; score:", score))
score.cv <- c(score.cv, score)
vec.n.trees <- c(vec.n.trees, n.trees)
vec.n.mtry <- c(vec.n.mtry, n.mtry)
}
}
#########################
md.rf <- ranger(Response ~.
, data = train_clean
, num.trees = 3500
, mtry = 36
, importance = "impurity"
, write.forest = T
, min.node.size = 20
, num.threads = 8
, verbose = T
)
pred.train <- predict(md.rf, train_clean)
pred.train <- predictions(pred.train)
# pred.train <- as.integer(pred.train)
#pred.valid <- predict(md.rf, dt.valid)
#pred.valid <- predictions(pred.valid)
# pred.valid <- as.integer(pred.valid)
test_clean
pred.test <- predict(md.rf, test_clean)
View(pred.test)
write.csv(cbind(test_clean$Id,pred.test),'File4.csv')
?write.csv
pred.test <- predictions(pred.test)
pred.test <- round(as.numeric(pred.test))
# pred.test <- as.integer(pred.test)
cat("optimising the cuts on pred.train ...\n")
SQWKfun <- function(x = seq(1.5, 7.5, by = 1)){
cuts <- c(min(pred.train), x[1], x[2], x[3], x[4], x[5], x[6], x[7], max(pred.train))
pred <- as.integer(cut2(pred.train, cuts))
err <- ScoreQuadraticWeightedKappa(pred, train_clean$Response, 1, 8)
return(-err)
}
optCuts <- optim(seq(1.5, 7.5, by = 1), SQWKfun)
optCuts
cat("applying optCuts on valid ...\n")
cuts.test <- c(min(pred.test), optCuts$par, max(pred.test))
pred.test.op <- as.integer(cut2(pred.test, cuts.test))
score <- ScoreQuadraticWeightedKappa(train_clean_10$Response, pred.test.op)
# [1] 0.6265719 (originally .550676; num.trees = 500, regression tree on raw features)
# [1] 0.5286518 (originally .5286518; num.trees = 500, classification tree on raw features)
# [1] 0.6444031 (originally .5558768; num.trees = 5000, regression tree on raw features)
print(paste("-------- n.trees:", n.trees, "; mtry:", n.mtry, "; score:", score))
score.cv <- c(score.cv, score)
vec.n.trees <- c(vec.n.trees, n.trees)
vec.n.mtry <- c(vec.n.mtry, n.mtry)
########################
md.rf <- ranger(Response ~.
, data = train_clean_90
, num.trees = 3500
, mtry = 36
, importance = "impurity"
, write.forest = T
, min.node.size = 20
, num.threads = 8
, verbose = T
)
pred.test <- predict(md.rf, test)
pred.test<-predictions(pred.test)
pred.train <- predict(md.rf, train_apca_90)
pred.train<-predictions(pred.train)
trainForOpt <- sample(length(pred.train), length(pred.train) * .8)
pred.train.forOpt <- pred.train[trainForOpt]
SQWKfun <- function(x = seq(1.5, 7.5, by = 1)){
cuts <- c(min(pred.train.forOpt), x[1], x[2], x[3], x[4], x[5], x[6], x[7], max(pred.train.forOpt))
pred <- as.integer(cut2(pred.train.forOpt, cuts))
err <- ScoreQuadraticWeightedKappa(pred, y.train[trainForOpt], 1, 8)
return(-err)
}
optCuts <- optim(seq(1.5, 7.5, by = 1), SQWKfun)
optCuts
require(Hmisc)
library(Hmisc)
cat("applying optCuts on valid ...\n")
cuts.test <- c(min(pred.test), optCuts$par, max(pred.test))
pred.test.op <- as.integer(cut2(pred.test, cuts.test))
View(pred.test)
score <- ScoreQuadraticWeightedKappa(as.numeric(round(pred.test)), as.numeric(y.test))
score
# 0.6359023
b<-ScoreQuadraticWeightedKappa(as.numeric(round(train_clean_30$Prediction)),as.numeric(train_clean_30$Response))
cat("applying optCuts on test ...\n")
cuts.test <- c(min(pred.test), optCuts$par, max(pred.test))
pred.test.op <- as.integer(cut2(pred.test, cuts.test))
#train_clean_30$Prediction <- as.integer(round(predict(md.rf, train_clean_30)))
kappa_rf<-ScoreQuadraticWeightedKappa(train_clean_30$Prediction,as.numeric(train_clean_30$Response))
auc_rf<-multiclass.roc(train_clean_30$Prediction,
as.numeric(train_clean_30$Response),
levels = levels(factor(train_clean_30$Response)))
library(pROC)
accuracy_rf<-classificationMetrics(train_clean_30$Prediction,
as.numeric(train_clean_30$Response))[1]
#The Kappa score for random forest model is
b<-ScoreQuadraticWeightedKappa(as.numeric(train_clean_30$Prediction),as.numeric(train_clean_30$Response))
b
#submit <- data.frame(train_clean)
#write.csv(submit,file="'train1.csv",row.names=F)
#### The Rattel plot (or tree) used for predicting the model is as follows:
fancyRpartPlot(model1)
### 5.3 XGBoost
#XGBoost is short for "Extreme Gradient Boosting", where the term "Gradient Boosting" is proposed in the paper Greedy Function Approximation: A Gradient Boosting Machine, by Friedman. XGBoost is based on this original model. XGBoost is used for supervised learning problems, where we use the training data (with multiple features) $x_i$ to predict a target variable $(y_i)$.
#### Outpout from XGBoost
#Since we are using the same data set for XGBoost as well we will divide it again.
#{r echo=TRUE, warning=FALSE,message=FALSE,error=FALSE,fig.keep='all', include=FALSE}
## dividing data