-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 31.2 KB
/
.Rhistory
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
c7_threshold_upp = as.numeric(quantile(cluster_7$DIST.FACTOR)[4]) + IQR(cluster_7$DIST.FACTOR) * 1.5
c8_threshold_upp = as.numeric(quantile(cluster_8$DIST.FACTOR)[4]) + IQR(cluster_8$DIST.FACTOR) * 1.5
#Lower limit
c1_threshold_low = as.numeric(quantile(cluster_1$DIST.FACTOR)[2]) - IQR(cluster_1$DIST.FACTOR) * 1.5
c2_threshold_low = as.numeric(quantile(cluster_2$DIST.FACTOR)[2]) - IQR(cluster_2$DIST.FACTOR) * 1.5
c3_threshold_low = as.numeric(quantile(cluster_3$DIST.FACTOR)[2]) - IQR(cluster_3$DIST.FACTOR) * 1.5
c4_threshold_low = as.numeric(quantile(cluster_4$DIST.FACTOR)[2]) - IQR(cluster_4$DIST.FACTOR) * 1.5
c5_threshold_low = as.numeric(quantile(cluster_5$DIST.FACTOR)[2]) - IQR(cluster_5$DIST.FACTOR) * 1.5
c6_threshold_low = as.numeric(quantile(cluster_6$DIST.FACTOR)[2]) - IQR(cluster_6$DIST.FACTOR) * 1.5
c7_threshold_low = as.numeric(quantile(cluster_7$DIST.FACTOR)[2]) - IQR(cluster_7$DIST.FACTOR) * 1.5
c8_threshold_low = as.numeric(quantile(cluster_8$DIST.FACTOR)[2]) - IQR(cluster_8$DIST.FACTOR) * 1.5
#Selecting the outliers
c1_outliers = cluster_1[cluster_1$DIST.FACTOR>c1_threshold_upp | cluster_1$DIST.FACTOR<c1_threshold_low, ]
c2_outliers = cluster_2[cluster_2$DIST.FACTOR>c2_threshold_upp | cluster_2$DIST.FACTOR<c2_threshold_low, ]
c3_outliers = cluster_3[cluster_3$DIST.FACTOR>c3_threshold_upp | cluster_3$DIST.FACTOR<c3_threshold_low, ]
c4_outliers = cluster_4[cluster_4$DIST.FACTOR>c4_threshold_upp | cluster_4$DIST.FACTOR<c4_threshold_low, ]
c5_outliers = cluster_5[cluster_5$DIST.FACTOR>c5_threshold_upp | cluster_5$DIST.FACTOR<c5_threshold_low, ]
c6_outliers = cluster_6[cluster_6$DIST.FACTOR>c6_threshold_upp | cluster_6$DIST.FACTOR<c6_threshold_low, ]
c7_outliers = cluster_7[cluster_7$DIST.FACTOR>c7_threshold_upp | cluster_7$DIST.FACTOR<c7_threshold_low, ]
c8_outliers = cluster_8[cluster_8$DIST.FACTOR>c8_threshold_upp | cluster_8$DIST.FACTOR<c8_threshold_low, ]
#Combinding the outliers from different clusters into a single data frame
outlier = rbind(c1_outliers,c2_outliers,c3_outliers,c4_outliers,c5_outliers,c6_outliers,c7_outliers,c8_outliers)
#Mark the records that are outliers as 1 in the column "RISK_P" and the rest 0
luxe.cmp = luxe.root[,"CLM_REF"]
luxe.cmp$RISK_P = 0
luxe.cmp[luxe.cmp$CLM_REF %in% outlier$CLM_REF,]$RISK_P = 1
#Now that we managed to label the outliers, we can now compare this against the verified
#dataset to see if our methodology of finding outliers (Risky claims) are representative #of the way the company does it.
#Compute accuracy scores
luxe.cmp$RISK_P = as.factor(luxe.cmp$RISK_P)
quartile_cm = confusionMatrix(luxe.cmp$RISK_P,luxe_val.root$RISK, positive="1")
quartile_cm
#From the results, we can see that while we managed identify profiles from the clusters, the way we identified our outliers is not effective. In this case we will attempt to try another way of detecting outliers. Since the values with huge difference to the cluster centroid did not yield good results, we shall make use of the top N approach
#Considering how we have 7406 records in the validated dataset that are risky, We shall set the same number and see whether we are able to detect the risky claims
N= 7406
km.distorder <- order(luxe.clustdn$DIST.FACTOR, decreasing = T)
luxe.clustdn.order <- luxe.clustdn[km.distorder,]
luxe.clustdn.order$rank <- rank(-luxe.clustdn.order$DIST.FACTOR)
luxe.topN.outl <- data.frame(luxe.clustdn.order[luxe.clustdn.order$rank<=N,])
luxe.cmp2 = luxe.root[,"CLM_REF"]
luxe.cmp2$RISK_P = 0
luxe.cmp2[luxe.cmp2$CLM_REF %in% luxe.topN.outl$CLM_REF,]$RISK_P = 1
luxe.cmp2$RISK_P = as.factor(luxe.cmp2$RISK_P)
topn_cm = confusionMatrix(luxe.cmp2$RISK_P,luxe_val.root$RISK, positive="1")
topn_cm
#From the results we can see that sensitivity is extremely low. From this we can roughly estimate that the way the organization detect outliers is very different from using the clustering approach. We will not recommend using this. As such we will need to find an alternative way to do prediction.
#Here we begin with our values prediction for supervised learning.
#When it comes to supervised learning, the usage of variables does not matter as
#much as long as prediction is accurate. The explanatory power of the model does not
#really matter.
#To begin with the whole process, we will first do a bit of data preparation. Initially,
#we have kept for ourselves luxe.all_data, that has all the variables. We will use this
#as a naive approach to create our baseline models
#We shall first attach the labels to luxe.all_data
luxe.all_data.label = cbind(luxe.all_data,luxe_val.root$RISK)
luxe.all_data.label = rename(luxe.all_data.label,RISK = `luxe_val.root$RISK`)
#We will remove variables with only 1 class in the variable (All the same value), which are
#CLM_SYS.EN and CLM_STAT.EN since they won't be helpful in predicting values
#We will also remove CLM_REF which only consist of distinct values for the same reason.
luxe.pm.all_data = luxe.all_data.label[,-c(12:14)]
#So then, we will also attempt to do variable selection to create a few dataset with different variables for testing. To do so, we will make use of Chi-square goodness of fit and stepwise analysis.
#We will start with Chi-square goodness of fit test for categorical variables and
#correlation test for continuous variables. We are not using stepwise because of the nature of our algorithm we hae chose to solve our business problem, which is classifcation tree.
empid_stat_test_result = chisq.test(luxe.all_data$EMP_ID, luxe_val.root$RISK, correct=FALSE)
flexbentype_stat_test_result =chisq.test(luxe.all_data$FLEXBEN_TYPE, luxe_val.root$RISK, correct=FALSE)
clmyr_stat_test_result =chisq.test(as.factor(luxe.all_data$CLM_YR), luxe_val.root$RISK, correct=FALSE)
clmamt_stat_test_result =cor.test(luxe.all_data$CLM_AMT, as.numeric(luxe_val.root$RISK),method = c("pearson", "kendall", "spearman"))
reimbyr_stat_test_result =chisq.test(as.factor(luxe.all_data$REIMB_YR), luxe_val.root$RISK, correct=FALSE)
sublapseint_stat_test_result =cor.test(luxe.all_data$SUB_LAPSE_INT, as.numeric(luxe_val.root$RISK), method = c("pearson", "kendall", "spearman"))
clmlapseint_stat_test_result =cor.test(luxe.all_data$CLM_LAPSE_INT, as.numeric(luxe_val.root$RISK), method = c("pearson", "kendall", "spearman"))
audlapseint_stat_test_result =cor.test(luxe.all_data$AUD_LAPSE_INT, as.numeric(luxe_val.root$RISK), method = c("pearson", "kendall", "spearman"))
rcptdayen_stat_test_result =chisq.test(luxe.all_data$RCPT_DAY.EN, luxe_val.root$RISK, correct=FALSE)
daytagen_stat_test_result =chisq.test(luxe.all_data$DAY.TAG.EN, luxe_val.root$RISK, correct=FALSE)
fsatypeen_stat_test_result =chisq.test(luxe.all_data$FSA_TYPE.EN, luxe_val.root$RISK, correct=FALSE)
stat_test_var = c("EMP_ID","FLEXBEN_TYPE","CLM_YR","CLM_AMT","REIMB_YR","SUB_LAPSE_INT","CLM_LAPSE_INT","AUD_LAPSE_INT","RCPT_DAY.EN","DAY_TAG.EN","FSA_TYPE.EN")
stat_test_p_value = c(empid_stat_test_result$p.value,
flexbentype_stat_test_result$p.value,
clmyr_stat_test_result$p.value,
clmamt_stat_test_result$p.value,
reimbyr_stat_test_result$p.value,
sublapseint_stat_test_result$p.value,
clmlapseint_stat_test_result$p.value,
audlapseint_stat_test_result$p.value,
rcptdayen_stat_test_result$p.value,
daytagen_stat_test_result$p.value,
daytagen_stat_test_result$p.value)
stat_test_results = data.frame(stat_test_var,stat_test_p_value)
stat_test_results[stat_test_results$stat_test_p_value<0.05,]
sig_cols=stat_test_results[stat_test_results$stat_test_p_value<0.05,]$stat_test_var
#Given the results, we will put all these variables into a dataset
luxe.sig_col = cbind(luxe.all_data[,c(sig_cols)],luxe_val.root$RISK)
luxe.sig_col = rename(luxe.sig_col,RISK = `luxe_val.root$RISK`)
#Based on the EDA we have done previously, we saw that there are potential pitfalls
#when creating the model because risky claims are rarer than non-risky claims.
#As such, we will engage is sampling techniques to balance out the bias. In this case we will be using Randomly Over Sampling Examples (ROSE). We will conduct ROSE on both luxe.all_data and luxe.sig_val
#We will only be oversampling the train set so we are Splitting dataset into 60-40
set.seed(1)
luxe.train.index <- sample(c(1:dim(luxe.pm.all_data)[1]),dim(luxe.pm.all_data)[1]*.6)
luxe.pm.all_data.train <- luxe.pm.all_data[luxe.train.index,]
luxe.pm.all_data.test <- luxe.pm.all_data[-luxe.train.index,]
luxe.sig_col.train <- luxe.sig_col[luxe.train.index,]
luxe.sig_col.test <- luxe.sig_col[-luxe.train.index,]
# Apply ROSE to both train
luxe.pm.all_data.train.ROSE <- ROSE(RISK~ ., data = luxe.pm.all_data, seed = 1)$data
luxe.sig_col.train.ROSE <- ROSE(RISK~ ., data = luxe.sig_col.train, seed = 1)$data
#So far we have the following datasets that can be used for training the model:
#luxe.pm.all_data.train
#luxe.sig_val.train
#luxe.pm.all_data.train.ROSE
#luxe.sig_val.train.ROSE
#Then using these 4 models we are going to train 3 different models:
#Classification tree (C5.0) - single model
#Random Forest - Ensemble model using bootstrapping concept
#XGBoost - Random Forest with boosting technique
#By doing these 3 model, our team believe that we will get a good sensing of
#the nature of the data and which algorithm will create the best model for future
#predictions
# Conduct of C5.0 Decision Tree
set.seed(1)
ladl.c5<-C5.0(RISK~., data = luxe.pm.all_data.train,rules=TRUE)
lsc.c5<-C5.0(RISK~., data = luxe.sig_col.train,rules=TRUE)
ladlr.c5<-C5.0(RISK~., data = luxe.pm.all_data.train.ROSE,rules=TRUE)
lscr.c5<-C5.0(RISK~., data = luxe.sig_col.train.ROSE,rules=TRUE)
ladl.c5.pred <- predict(ladl.c5,luxe.pm.all_data.test,type="class")
lsc.c5.pred <- predict(lsc.c5,luxe.sig_col.test,type="class")
ladlr.c5.pred <- predict(ladlr.c5,luxe.pm.all_data.test,type="class")
lscr.c5.pred <- predict(lscr.c5,luxe.sig_col.test,type="class")
ladl.c5_cm = confusionMatrix(table(ladl.c5.pred, luxe.pm.all_data.test$RISK),
positive = "1")
lsc.c5_cm = confusionMatrix(table(lsc.c5.pred, luxe.sig_col.test$RISK),
positive = "1")
ladlr.c5_cm = confusionMatrix(table(ladlr.c5.pred, luxe.pm.all_data.test$RISK),
positive = "1")
lscr.c5_cm = confusionMatrix(table(lscr.c5.pred, luxe.sig_col.test$RISK),
positive = "1")
ladl.c5_cm
lsc.c5_cm
ladlr.c5_cm
lscr.c5_cm
# Conduct of XGboost
#Preparation of data
#The XGBoost function requires labels and miscellaneous variables to be separated
#Here we separate them accordingly.
set.seed(100)
ladl_labels = as.numeric(paste(luxe.pm.all_data.train$RISK))
ladlr_labels = as.numeric(paste(luxe.pm.all_data.train.ROSE$RISK))
lsc_labels = as.numeric(paste(luxe.sig_col.train$RISK))
lscr_labels = as.numeric(paste(luxe.sig_col.train.ROSE$RISK))
luxe.pm.all_data.train.nl = luxe.pm.all_data.train[,-c(length(luxe.pm.all_data.train))]
luxe.sig_col.train.nl = luxe.sig_col.train[,-c(length(luxe.sig_col.train))]
luxe.pm.all_data.train.ROSE.nl = luxe.pm.all_data.train.ROSE[,-c(length(luxe.pm.all_data.train.ROSE))]
luxe.sig_col.train.ROSE.nl = luxe.sig_col.train.ROSE[,-c(length(luxe.sig_col.train.ROSE))]
ladl_test_label = luxe.pm.all_data.test$RISK
lsc_test_label = luxe.sig_col.test$RISK
luxe.pm.all_data.test.nl =luxe.pm.all_data.test[,-c(length(luxe.pm.all_data.test))]
luxe.sig_col.test.nl = luxe.sig_col.test[,-c(length(luxe.sig_col.test))]
#Parameters
p_eta = 0.1
p_max_depth = 15
p_nround=25
p_subsample = 0.5
p_colsample_bytree = 0.5
p_seed = 1
p_eval_metric = "merror"
p_objective = "multi:softmax"
p_num_class =2
p_nthread = 3
#Model for luxe.pm.all_data
xgb_ladl <- xgboost(data = data.matrix(luxe.pm.all_data.train.nl),
label = ladl_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.sig_col
xgb_lsc <- xgboost(data = data.matrix(luxe.sig_col.train.nl),
label = lsc_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.pm.all_data.ROSE
xgb_ladlr <- xgboost(data = data.matrix(luxe.pm.all_data.train.ROSE.nl),
label = ladlr_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.sig_col.ROSE
xgb_lscr <- xgboost(data = data.matrix(luxe.sig_col.train.ROSE.nl),
label = lscr_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Predicting the results using the 4 models
#We are using the same test data for testing because ROSE is only applied to the
#training set
xgb_ladl_y_pred <- predict(xgb_ladl, data.matrix(luxe.pm.all_data.test.nl))
xgb_lsc_y_pred <- predict(xgb_lsc, data.matrix(luxe.sig_col.test.nl))
xgb_ladlr_y_pred <- predict(xgb_ladlr, data.matrix(luxe.pm.all_data.test.nl))
xgb_lscr_y_pred <- predict(xgb_lscr, data.matrix(luxe.sig_col.test.nl))
#plotting of the confusion matrix
xgb_ladl_y_pred <- as.factor(xgb_ladl_y_pred)
xgb_lsc_y_pred <- as.factor(xgb_lsc_y_pred)
xgb_ladlr_y_pred <- as.factor(xgb_ladlr_y_pred)
xgb_lscr_y_pred <- as.factor(xgb_lscr_y_pred)
xgb_ladl_cm = confusionMatrix(xgb_ladl_y_pred,ladl_test_label, positive="1")
xgb_lsc_cm = confusionMatrix(xgb_lsc_y_pred,lsc_test_label, positive="1")
xgb_ladlr_cm = confusionMatrix(xgb_ladlr_y_pred,ladl_test_label, positive="1")
xgb_lscr_cm = confusionMatrix(xgb_lscr_y_pred,lsc_test_label, positive="1")
xgb_ladl_cm
xgb_lsc_cm
xgb_ladlr_cm
xgb_lscr_cm
# Conduct of Random Forest
# Random forest cannot handle variables that has more than 53 levels (EMP_ID), so we will remove
# it to run the model.
luxe.pm.less_EMPID.train = luxe.pm.all_data.train[,-c(1)]
luxe.sig_col.less_EMPID.train = luxe.sig_col.train[,-c(7)]
luxe.pm.less_EMPID.train.ROSE <- ROSE(RISK~ ., data = luxe.pm.less_EMPID.train, seed = 1)$data
luxe.sig_col.less_EMPID.train.ROSE <- ROSE(RISK~ ., data = luxe.sig_col.less_EMPID.train, seed = 1)$data
luxe.pm.less_EMPID.test = luxe.pm.all_data.test[,-c(1)]
luxe.sig_col.less_EMPID.test = luxe.sig_col.test[,-c(7)]
ladl_no_empid.randf <- randomForest(RISK ~ ., data = luxe.pm.less_EMPID.train,
ntree=500, importance = TRUE)
lsc_no_empid.randf <- randomForest(RISK ~ ., data = luxe.sig_col.less_EMPID.train,
ntree=500, importance = TRUE)
ladlr_no_empid.randf <- randomForest(RISK ~ ., data = luxe.pm.less_EMPID.train.ROSE,
ntree=500, importance = TRUE)
lscr_no_empid.randf <- randomForest(RISK ~ ., data = luxe.sig_col.less_EMPID.train.ROSE,
ntree=500, importance = TRUE)
ladl_no_empid.randf.pred <- predict(ladl_no_empid.randf,luxe.pm.less_EMPID.test,type="class",
main="Variable Importance Plot")
lsc_no_empid.randf.pred <- predict(lsc_no_empid.randf,luxe.sig_col.less_EMPID.test,type="class",
main="Variable Importance Plot")
ladlr_no_empid.randf.pred <- predict(ladlr_no_empid.randf,luxe.pm.less_EMPID.test,type="class",
main="Variable Importance Plot")
lscr_no_empid.randf.pred <- predict(lscr_no_empid.randf,luxe.sig_col.less_EMPID.test,type="class",
main="Variable Importance Plot")
# generate confusion matrix
ladl_no_empid.randf_cm = confusionMatrix(table(ladl_no_empid.randf.pred, luxe.pm.less_EMPID.test$RISK),
positive = "1")
lsc_no_empid.randf_cm = confusionMatrix(table(lsc_no_empid.randf.pred, luxe.sig_col.less_EMPID.test$RISK),
positive = "1")
ladlr_no_empid.randf_cm = confusionMatrix(table(ladlr_no_empid.randf.pred, luxe.pm.less_EMPID.test$RISK),
positive = "1")
lscr_no_empid.randf_cm = confusionMatrix(table(lscr_no_empid.randf.pred, luxe.sig_col.less_EMPID.test$RISK),
positive = "1")
ladl_no_empid.randf_cm
lsc_no_empid.randf_cm
ladlr_no_empid.randf_cm
lscr_no_empid.randf_cm
# Considering how EMP_ID is removed from the dataset We should use the dataset to
# run the two other models so that we can have a comparison between models
# Conduct of C5.0 Decision Tree without EMP_ID in dataset
set.seed(1)
ladl_no_empid.c5<-C5.0(RISK~., data = luxe.pm.less_EMPID.train, rules=TRUE)
lsc_no_empid.c5<-C5.0(RISK~., data = luxe.sig_col.less_EMPID.train, rules=TRUE)
ladlr_no_empid.c5<-C5.0(RISK~., data = luxe.pm.less_EMPID.train.ROSE, rules=TRUE)
lscr_no_empid.c5<-C5.0(RISK~., data = luxe.sig_col.less_EMPID.train.ROSE, rules=TRUE)
ladl_no_empid.c5.pred <- predict(ladl_no_empid.c5,luxe.pm.less_EMPID.test,type="class")
lsc_no_empid.c5.pred <- predict(lsc_no_empid.c5,luxe.sig_col.less_EMPID.test,type="class")
ladlr_no_empid.c5.pred <- predict(ladlr_no_empid.c5,luxe.pm.less_EMPID.test,type="class")
lscr_no_empid.c5.pred <- predict(lscr_no_empid.c5,luxe.sig_col.less_EMPID.test,type="class")
ladl_no_empid.c5_cm = confusionMatrix(table(ladl_no_empid.c5.pred, luxe.pm.less_EMPID.test$RISK),
positive = "1")
lsc_no_empid.c5_cm = confusionMatrix(table(lsc_no_empid.c5.pred, luxe.sig_col.less_EMPID.test$RISK),
positive = "1")
ladlr_no_empid.c5_cm = confusionMatrix(table(ladlr_no_empid.c5.pred, luxe.pm.less_EMPID.test$RISK),
positive = "1")
lscr_no_empid.c5_cm = confusionMatrix(table(lscr_no_empid.c5.pred, luxe.sig_col.less_EMPID.test$RISK),
positive = "1")
# Conduct of XGboost without EMP_ID in dataset
set.seed(100)
ladl_no_empid_labels = as.numeric(paste(luxe.pm.less_EMPID.train$RISK))
ladlr_no_empid_labels = as.numeric(paste(luxe.pm.less_EMPID.train.ROSE$RISK))
lsc_no_empid_labels = as.numeric(paste(luxe.sig_col.less_EMPID.train$RISK))
lscr_no_empid_labels = as.numeric(paste(luxe.sig_col.less_EMPID.train.ROSE$RISK))
luxe.pm.less_EMPID.train.nl = luxe.pm.less_EMPID.train[,-c(length(luxe.pm.less_EMPID.train))]
luxe.sig_col.less_EMPID.train.nl = luxe.sig_col.less_EMPID.train[,-c(length(luxe.sig_col.less_EMPID.train))]
luxe.pm.less_EMPID.train.ROSE.nl = luxe.pm.less_EMPID.train.ROSE[,-c(length(luxe.pm.less_EMPID.train.ROSE))]
luxe.sig_col.less_EMPID.train.ROSE.nl = luxe.sig_col.less_EMPID.train.ROSE[,-c(length(luxe.sig_col.less_EMPID.train.ROSE))]
ladl_no_empid_test_label = luxe.pm.less_EMPID.test$RISK
lsc_no_empid_test_label = luxe.sig_col.less_EMPID.test$RISK
luxe.pm.less_EMPID.test.nl =luxe.pm.less_EMPID.test[,-c(length(luxe.pm.less_EMPID.test))]
luxe.sig_col.less_EMPID.test.nl = luxe.sig_col.less_EMPID.test[,-c(length(luxe.sig_col.less_EMPID.test))]
#Parameters
p_eta = 0.1
p_max_depth = 15
p_nround=25
p_subsample = 0.5
p_colsample_bytree = 0.5
p_seed = 1
p_eval_metric = "merror"
p_objective = "multi:softmax"
p_num_class =2
p_nthread = 3
#Model for luxe.pm.all_data
xgb_ladl_no_empid <- xgboost(data = data.matrix(luxe.pm.less_EMPID.train.nl),
label = ladl_no_empid_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.sig_col
xgb_lsc_no_empid <- xgboost(data = data.matrix(luxe.sig_col.less_EMPID.train.nl),
label = lsc_no_empid_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.pm.all_data.ROSE
xgb_ladlr_no_empid <- xgboost(data = data.matrix(luxe.pm.less_EMPID.train.ROSE.nl),
label = ladlr_no_empid_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Model for luxe.sig_col.ROSE
xgb_lscr_no_empid <- xgboost(data = data.matrix(luxe.sig_col.less_EMPID.train.ROSE.nl),
label = lscr_no_empid_labels,
eta = p_eta,
max_depth = p_max_depth,
nround= p_nround,
subsample = p_subsample,
colsample_bytree = p_colsample_bytree,
seed = p_seed,
eval_metric = p_eval_metric,
objective = p_objective,
num_class =p_num_class,
nthread = p_nthread
)
#Predicting the results using the 4 models
#We are using the same test data for testing because ROSE is only applied to the
#training set
xgb_ladl_no_empid_y_pred <- predict(xgb_ladl_no_empid, data.matrix(luxe.pm.less_EMPID.test.nl))
xgb_lsc_no_empid_y_pred <- predict(xgb_lsc_no_empid, data.matrix(luxe.sig_col.less_EMPID.test.nl))
xgb_ladlr_no_empid_y_pred <- predict(xgb_ladlr_no_empid, data.matrix(luxe.pm.less_EMPID.test.nl))
xgb_lscr_no_empid_y_pred <- predict(xgb_lscr_no_empid, data.matrix(luxe.sig_col.less_EMPID.test.nl))
#plotting of the confusion matrix
xgb_ladl_no_empid_y_pred <- as.factor(xgb_ladl_no_empid_y_pred)
xgb_lsc_no_empid_y_pred <- as.factor(xgb_lsc_no_empid_y_pred)
xgb_ladlr_no_empid_y_pred <- as.factor(xgb_ladlr_no_empid_y_pred)
xgb_lscr_no_empid_y_pred <- as.factor(xgb_lscr_no_empid_y_pred)
xgb_ladl_no_empid_cm = confusionMatrix(xgb_ladl_no_empid_y_pred,ladl_no_empid_test_label, positive="1")
xgb_lsc_no_empid_cm = confusionMatrix(xgb_lsc_no_empid_y_pred,lsc_no_empid_test_label, positive="1")
xgb_ladlr_no_empid_cm = confusionMatrix(xgb_ladlr_no_empid_y_pred,ladl_no_empid_test_label, positive="1")
xgb_lscr_no_empid_cm = confusionMatrix(xgb_lscr_no_empid_y_pred,lsc_no_empid_test_label, positive="1")
model_names = c("C5.0","Random Forest","XGBoost")
#All Data less EMP_ID
ladl_acc_no_emp_id_results_cmp = c(ladl_no_empid.c5_cm$overall[1],ladl_no_empid.randf_cm$overall[1],xgb_ladl_no_empid_cm$overall[1])
ladl_sen_no_emp_id_results_cmp = c(ladl_no_empid.c5_cm$byClass[1],ladl_no_empid.randf_cm$byClass[1],xgb_ladl_no_empid_cm$byClass[1])
ladl_spec_no_emp_id_results_cmp = c(ladl_no_empid.c5_cm$byClass[2],ladl_no_empid.randf_cm$byClass[2],xgb_ladl_no_empid_cm$byClass[2])
ladl_prec_no_emp_id_results_cmp = c(ladl_no_empid.c5_cm$byClass[5],ladl_no_empid.randf_cm$byClass[5],xgb_ladl_no_empid_cm$byClass[5])
ladl_no_emp_id_results_cmp = data.frame(model_names,ladl_acc_no_emp_id_results_cmp,ladl_sen_no_emp_id_results_cmp,ladl_spec_no_emp_id_results_cmp,ladl_prec_no_emp_id_results_cmp)
colnames(ladl_no_emp_id_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Significant variable less EMP_ID
lsc_acc_no_emp_id_results_cmp = c(lsc_no_empid.c5_cm$overall[1],lsc_no_empid.randf_cm$overall[1],xgb_lsc_no_empid_cm$overall[1])
lsc_sen_no_emp_id_results_cmp = c(lsc_no_empid.c5_cm$byClass[1],lsc_no_empid.randf_cm$byClass[1],xgb_lsc_no_empid_cm$byClass[1])
lsc_spec_no_emp_id_results_cmp = c(lsc_no_empid.c5_cm$byClass[2],lsc_no_empid.randf_cm$byClass[2],xgb_lsc_no_empid_cm$byClass[2])
lsc_prec_no_emp_id_results_cmp = c(lsc_no_empid.c5_cm$byClass[5],lsc_no_empid.randf_cm$byClass[5],xgb_lsc_no_empid_cm$byClass[5])
lsc_no_emp_id_results_cmp = data.frame(model_names,lsc_acc_no_emp_id_results_cmp,lsc_sen_no_emp_id_results_cmp,lsc_spec_no_emp_id_results_cmp,lsc_prec_no_emp_id_results_cmp)
colnames(lsc_no_emp_id_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#All Data less EMP_ID with ROSE
ladlr_acc_no_emp_id_results_cmp = c(ladlr_no_empid.c5_cm$overall[1],ladlr_no_empid.randf_cm$overall[1],xgb_ladlr_no_empid_cm$overall[1])
ladlr_sen_no_emp_id_results_cmp = c(ladlr_no_empid.c5_cm$byClass[1],ladlr_no_empid.randf_cm$byClass[1],xgb_ladlr_no_empid_cm$byClass[1])
ladlr_spec_no_emp_id_results_cmp = c(ladlr_no_empid.c5_cm$byClass[2],ladlr_no_empid.randf_cm$byClass[2],xgb_ladlr_no_empid_cm$byClass[2])
ladlr_prec_no_emp_id_results_cmp = c(ladlr_no_empid.c5_cm$byClass[5],ladlr_no_empid.randf_cm$byClass[5],xgb_ladlr_no_empid_cm$byClass[5])
ladlr_no_emp_id_results_cmp = data.frame(model_names,ladlr_acc_no_emp_id_results_cmp,ladlr_sen_no_emp_id_results_cmp,ladlr_spec_no_emp_id_results_cmp,ladlr_prec_no_emp_id_results_cmp)
colnames(ladlr_no_emp_id_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Significant variable less EMP_ID
lscr_acc_no_emp_id_results_cmp = c(lscr_no_empid.c5_cm$overall[1],lscr_no_empid.randf_cm$overall[1],xgb_lscr_no_empid_cm$overall[1])
lscr_sen_no_emp_id_results_cmp = c(lscr_no_empid.c5_cm$byClass[1],lscr_no_empid.randf_cm$byClass[1],xgb_lscr_no_empid_cm$byClass[1])
lscr_spec_no_emp_id_results_cmp = c(lscr_no_empid.c5_cm$byClass[2],lscr_no_empid.randf_cm$byClass[2],xgb_lscr_no_empid_cm$byClass[2])
lscr_prec_no_emp_id_results_cmp = c(lscr_no_empid.c5_cm$byClass[5],lscr_no_empid.randf_cm$byClass[5],xgb_lscr_no_empid_cm$byClass[5])
lscr_no_emp_id_results_cmp = data.frame(model_names,lscr_acc_no_emp_id_results_cmp,lscr_sen_no_emp_id_results_cmp,lscr_spec_no_emp_id_results_cmp,lscr_prec_no_emp_id_results_cmp)
colnames(lscr_no_emp_id_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Compare C5.0 and XGBoost with EMP_ID and no EMP_ID for All Data less EMP_ID
model_names2 = c("C5.0","C5.0 w/o EMP_ID","XGBoost","XGBoost w/o EMP_ID")
ladl_acc_results_cmp = c(ladl.c5_cm$overall[1],ladl_no_empid.c5_cm$overall[1],xgb_ladl_cm$overall[1],xgb_ladl_no_empid_cm$overall[1])
ladl_sen_results_cmp = c(ladl.c5_cm$byClass[1],ladl_no_empid.c5_cm$byClass[1],xgb_ladl_cm$byClass[1],xgb_ladl_no_empid_cm$byClass[1])
ladl_spec_results_cmp = c(ladl.c5_cm$byClass[2],ladl_no_empid.c5_cm$byClass[2],xgb_ladl_cm$byClass[2],xgb_ladl_no_empid_cm$byClass[2])
ladl_prec_results_cmp = c(ladl.c5_cm$byClass[5],ladl_no_empid.c5_cm$byClass[5],xgb_ladl_cm$byClass[5],xgb_ladl_no_empid_cm$byClass[5])
ladl_results_cmp = data.frame(model_names2,ladl_acc_results_cmp,ladl_sen_results_cmp,ladl_spec_results_cmp,ladl_prec_results_cmp)
colnames(ladl_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Compare C5.0 and XGBoost with EMP_ID and no EMP_ID for Significant Variables
lsc_acc_results_cmp = c(lsc.c5_cm$overall[1],lsc_no_empid.c5_cm$overall[1],xgb_lsc_cm$overall[1],xgb_lsc_no_empid_cm$overall[1])
lsc_sen_results_cmp = c(lsc.c5_cm$byClass[1],lsc_no_empid.c5_cm$byClass[1],xgb_lsc_cm$byClass[1],xgb_lsc_no_empid_cm$byClass[1])
lsc_spec_results_cmp = c(lsc.c5_cm$byClass[2],lsc_no_empid.c5_cm$byClass[2],xgb_lsc_cm$byClass[2],xgb_lsc_no_empid_cm$byClass[2])
lsc_prec_results_cmp = c(lsc.c5_cm$byClass[5],lsc_no_empid.c5_cm$byClass[5],xgb_lsc_cm$byClass[5],xgb_lsc_no_empid_cm$byClass[5])
lsc_results_cmp = data.frame(model_names2,lsc_acc_results_cmp,lsc_sen_results_cmp,lsc_spec_results_cmp,lsc_prec_results_cmp)
colnames(lsc_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Compare C5.0 and XGBoost with EMP_ID and no EMP_ID for All Data less EMP_ID, as well as with ROSE
ladlr_acc_results_cmp = c(ladlr.c5_cm$overall[1],ladlr_no_empid.c5_cm$overall[1],xgb_ladlr_cm$overall[1],xgb_ladlr_no_empid_cm$overall[1])
ladlr_sen_results_cmp = c(ladlr.c5_cm$byClass[1],ladlr_no_empid.c5_cm$byClass[1],xgb_ladlr_cm$byClass[1],xgb_ladlr_no_empid_cm$byClass[1])
ladlr_spec_results_cmp = c(ladlr.c5_cm$byClass[2],ladlr_no_empid.c5_cm$byClass[2],xgb_ladlr_cm$byClass[2],xgb_ladlr_no_empid_cm$byClass[2])
ladlr_prec_results_cmp = c(ladlr.c5_cm$byClass[5],ladlr_no_empid.c5_cm$byClass[5],xgb_ladlr_cm$byClass[5],xgb_ladlr_no_empid_cm$byClass[5])
ladlr_results_cmp = data.frame(model_names2,ladlr_acc_results_cmp,ladlr_sen_results_cmp,ladlr_spec_results_cmp,ladlr_prec_results_cmp)
colnames(ladlr_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Compare C5.0 and XGBoost with EMP_ID and no EMP_ID for Significant Variables, as well as ROSE
lscr_acc_results_cmp = c(lscr.c5_cm$overall[1],lscr_no_empid.c5_cm$overall[1],xgb_lscr_cm$overall[1],xgb_lscr_no_empid_cm$overall[1])
lscr_sen_results_cmp = c(lscr.c5_cm$byClass[1],lscr_no_empid.c5_cm$byClass[1],xgb_lscr_cm$byClass[1],xgb_lscr_no_empid_cm$byClass[1])
lscr_spec_results_cmp = c(lscr.c5_cm$byClass[2],lscr_no_empid.c5_cm$byClass[2],xgb_lscr_cm$byClass[2],xgb_lscr_no_empid_cm$byClass[2])
lscr_prec_results_cmp = c(lscr.c5_cm$byClass[5],lscr_no_empid.c5_cm$byClass[5],xgb_lscr_cm$byClass[5],xgb_lscr_no_empid_cm$byClass[5])
lscr_results_cmp = data.frame(model_names2,lscr_acc_results_cmp,lscr_sen_results_cmp,lscr_spec_results_cmp,lscr_prec_results_cmp)
colnames(lscr_results_cmp) = c("Model","Accuracy","Sensitivity","Specificity","Precision")
#Compare against 3 models using datasets without EMP_ID
ladl_no_emp_id_results_cmp
lsc_no_emp_id_results_cmp
ladlr_no_emp_id_results_cmp
lscr_no_emp_id_results_cmp
#Compare C5.0 and XGBoost models that make use of the with EMP_ID and without EMP_ID datasets
ladl_results_cmp
lsc_results_cmp
ladlr_results_cmp
lscr_results_cmp
#The results shows that even though EMP_ID was considered to be significant, it was not very helpful in
#Developing a model. As things are right now, C5.0 with all prediction model data less EMP_ID fare the best
#in accuracy. However, considering how It is more important to detect risky transaction than making
#a mistake in classifying non-risky transaction as risky, we should consider the sensitivity more.
#The model that fares the best in sensitivity is C5.0 with all prediction model data (including EMP_ID)
#It has also been consistent that results without EMP_ID fares better as well. This is with exception to
#when ROSE has been applied to the dataset with all predictive model variables. While XGboost seems to be
#underperforming,it is to be noted that XGboost may do a lot better with proper tuning of the values
summary(ladl.c5)
summary(lsc.c5)
summary(ladlr.c5)
summary(lscr.c5)
summary(ladl_no_empid.c5)
summary(lsc_no_empid.c5)
summary(ladlr_no_empid.c5)
summary(lscr_no_empid.c5)
xgb_ladl_no_empid_cm
xgb_lsc_no_empid_cm
xgb_ladlr_no_empid_cm
xgb_lscr_no_empid_cm
shiny::runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
dengue_pt_range_fil
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
ppp_list
spatpoint_list
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
length(plot_list)
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
print(NULL)
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')
runApp('~/SMU/Current Semester/IS415-Geospatial Analytics and Application/Geospatial Project/dangy')