-
Notifications
You must be signed in to change notification settings - Fork 4
/
AnalysisDiscussion.Rmd
493 lines (395 loc) · 15 KB
/
AnalysisDiscussion.Rmd
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
---
title: "R Notebook"
output:
html_document:
toc: yes
html_notebook: default
---
## Data Reading:
```{r}
library(readr)
Datav2 <- read_delim("Data/finalData.csv", ";", escape_double = FALSE, trim_ws = TRUE)
m2 <- read_delim("Data/cleanData.csv",";", escape_double = FALSE, trim_ws = TRUE)
library(readxl)
#Datav2 <- read_excel("E:/KPMG/KPMG/Data/prepdata.xlsx")
#m2 <- read_excel("E:/KPMG/KPMG/Data/prepdataOrg.xlsx")
```
# Data Stats
```{r}
Datav2$InterestType <- as.factor(Datav2$InterestType)
Datav2$MortgageType <- as.factor(Datav2$MortgageType)
Datav2$NewLoan <- as.factor(Datav2$NewLoan)
Datav2$ProbationaryLoans <- as.factor(Datav2$ProbationaryLoans)
Datav2$LTVCategory <- as.factor(Datav2$LTVCategory)
Datav2$InArrears <- as.factor(Datav2$InArrears)
#Datav2$County <- as.factor(Datav2$County)
Datav2$DefaultedLoans <- as.factor(Datav2$DefaultedLoans)
m2$InterestType <- as.factor(m2$InterestType)
m2$MortgageType <- as.factor(m2$MortgageType)
m2$NewLoan <- as.factor(m2$NewLoan)
m2$ProbationaryLoans <- as.factor(m2$ProbationaryLoans)
m2$LTVCategory <- as.factor(m2$LTVCategory)
m2$InArrears <- as.factor(m2$InArrears)
#m2$County <- as.factor(m2$County)
m2$DefaultedLoans <- as.factor(m2$DefaultedLoans)
m1 <- Datav2
m3 <- Datav2
```
# LR Model with Scaled data (Original Data Set)
## Data prep
##Data Subsetting
```{r}
set.seed(100)
dataPartition = sample(2,nrow(m2),replace=TRUE,prob=c(0.6,0.4))
m2train <- m2[dataPartition ==1,]
m2test <- m2[dataPartition ==2,]
```
##Simple GLM for all variables
```{r}
#m2glm <- glm(m2$DefaultedLoans ~., family = "binomial", data = m2train)
#step(m2glm)
#Simple glm with selected variables
m2glm <- glm(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
family = "binomial", data = m2train)
#step(m2glm)
summary(m2glm)
save(m2glm, file = "Model/m2glm.rda")
```
##Prediction
```{r}
m2test$prediction <- predict(m2glm, newdata=m2test, type="response")
#Confusion Matrix
table(m2test$DefaultedLoans, as.numeric(m2test$prediction >= 0.8))
confusion.glm <- function(data, model) {
prediction <- ifelse(predict(model, data, type='response') > 0.5, TRUE, FALSE)
confusion <- table(prediction, as.logical(model$y))
confusion <- cbind(confusion, c(1 - confusion[1,1]/(confusion[1,1]+confusion[2,1]), 1 - confusion[2,2]/(confusion[2,2]+confusion[1,2])))
confusion <- as.data.frame(confusion)
names(confusion) <- c('FALSE', 'TRUE', 'class.error')
confusion
}
```
##Significant Variables
```{r}
significant.variables <- summary(m2glm)$coeff[-1,4] < 0.05
names(significant.variables)[significant.variables == TRUE]
```
##ROC
```{r}
#score test data set
library(ROCR)
m2test$lr_score <- predict(m2glm,type='response',m2test)
m2predlr <- prediction(m2test$lr_score, m2test$DefaultedLoans)
m2perflr <- performance(m2predlr,"tpr","fpr")
#ROC
plot(m2perflr, lwd=2, colorize=TRUE, main="ROC LR: Logistic Regression Performance")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
##KS, Gini & AUC m2
```{r}
lr_KS <- round(max(attr(m2perflr,'y.values')[[1]]-attr(m2perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m2predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("AUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
#LR without scaling
```{r}
#Data Subsetting
set.seed(100)
dataPartition = sample(2,nrow(m1),replace=TRUE,prob=c(0.6,0.4))
m1train <- m1[dataPartition ==1,]
m1test <- m1[dataPartition ==2,]
summary(m1)
```
##Simple GLM for all variables
```{r}
#m2glm <- glm(m1$DefaultedLoans ~., family = "binomial", data = m1train)
#step(m2glm)
#Simple glm with selected variables
m1glm <- glm(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
family = "binomial", data = m1train)
#step(m1glm)
summary(m1glm)
save(m1glm, file = "Model/m1glm.rda")
```
##Prediction
```{r}
m1test$prediction <- predict(m1glm, newdata=m1test, type="response")
#Confusion Matrix
table(m1test$DefaultedLoans, as.numeric(m1test$prediction >= 0.5))
```
##Significant Variables & ROC
```{r}
significant.variables <- summary(m1glm)$coeff[-1,4] < 0.05
names(significant.variables)[significant.variables == TRUE]
#ROC
#score test data set
library(ROCR)
m1test$lr_score <- predict(m1glm,type='response',m1test)
m1predlr <- prediction(m1test$lr_score, m1test$DefaultedLoans)
m1perflr <- performance(m1predlr,"tpr","fpr")
#ROC
plot(m1perflr, lwd=2, colorize=TRUE, main="ROC LR: Logistic Regression Performance")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
##KS, Gini & AUC m1
```{r}
accuracy <- 89.01
lr_KS <- round(max(attr(m1perflr,'y.values')[[1]]-attr(m1perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m1predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("Accuracy:",accuracy, "\tAUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
# Final Optimized Model
## Data prep
```{r}
m3 <- Datav2
m3$County <- as.factor(m3$County)
m3$LoanBalance <- scale(m3$LoanBalance)
m3$PropertyValue <- scale(m3$PropertyValue)
m3$AnnualPYMT <-scale(m3$AnnualPYMT)
m3$InterestIncome <-scale(m3$InterestIncome)
summary(m3)
```
##Data Subsetting
```{r}
set.seed(100)
dataPartition = sample(2,nrow(m3),replace=TRUE,prob=c(0.6,0.4))
m3train <- m3[dataPartition ==1,]
m3test <- m3[dataPartition ==2,]
```
##Simple GLM for all variables
```{r}
#m3glm <- glm(m3$DefaultedLoans ~., family = "binomial", data = m3train)
#step(m3glm)
#Simple glm with selected variables
m3glm <- glm(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
family = "binomial", data = m3train)
#step(m3glm)
summary(m3glm)
save(m3glm, file = "Model/m3glm.rda")
```
##Prediction
```{r}
m3test$prediction <- predict(m3glm, newdata=m3test, type="response")
#Confusion Matrix
table(m3test$DefaultedLoans, as.numeric(m3test$prediction >= 0.8))
confusion.glm <- function(data, model) {
prediction <- ifelse(predict(model, data, type='response') > 0.5, TRUE, FALSE)
confusion <- table(prediction, as.logical(model$y))
confusion <- cbind(confusion, c(1 - confusion[1,1]/(confusion[1,1]+confusion[2,1]), 1 - confusion[2,2]/(confusion[2,2]+confusion[1,2])))
confusion <- as.data.frame(confusion)
names(confusion) <- c('FALSE', 'TRUE', 'class.error')
confusion
}
```
##Significant Variables
```{r}
significant.variables <- summary(m3glm)$coeff[-1,4] < 0.05
names(significant.variables)[significant.variables == TRUE]
```
##ROC
```{r}
#score test data set
library(ROCR)
m3test$lr_score <- predict(m3glm,type='response',m3test)
m3predlr <- prediction(m3test$lr_score, m3test$DefaultedLoans)
m3perflr <- performance(m3predlr,"tpr","fpr")
#ROC
plot(m3perflr, lwd=2, colorize=TRUE, main="ROC LR: Logistic Regression Performance")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
##KS, Gini & AUC m3
```{r}
lr_KS <- round(max(attr(m3perflr,'y.values')[[1]]-attr(m3perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m3predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("AUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
# ########################################################################################
# #
# # Desicion Tree
```{r}
library(rpart)
library(rattle) # Fancy tree plot
library(rpart.plot) # Enhanced tree plots
library(RColorBrewer) # Color selection for fancy tree plot
library(party) # Alternative decision tree algorithm
library(partykit) # Convert rpart object to BinaryTree
library(caret)
library(tree)
```
#Decision tree with original dataset
```{r}
m2dt <- rpart(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
method = "class",data=m2train, control = rpart.control(minisplit=5,cp = 0.001))
m2test$DT_score <- predict(m2dt,type='prob',m2test)
DT_prediction2 <- prediction(m2test$DT_score[,2],m2test$DefaultedLoans)
DT_performance2 <- performance(DT_prediction2,"tpr","fpr")
# MOdel performance plot
plot(DT_performance2, lwd=2, colorize=TRUE, main="ROC Decision Tree: Traditional Recursive Partitioning")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
# Confusion Matrix
```{r}
defaultPredict <- predict(m2dt,m2test, type = 'class')
matrix <- table(defaultPredict, m2test$DefaultedLoans)
print(matrix)
```
# KS & AUC DT
```{r}
DT_AUROC <- round(performance(DT_prediction2, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance2,'y.values')[[1]]-attr(DT_performance2,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
# Decision tree without scaling variables
```{r}
m1dt <- rpart(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,method = "class",data=m1train, control = rpart.control(minisplit=5,cp = 0.001))
m1test$DT_score <- predict(m1dt,type='prob',m1test)
DT_prediction1 <- prediction(m1test$DT_score[,2],m1test$DefaultedLoans)
DT_performance1 <- performance(DT_prediction1,"tpr","fpr")
# MOdel performance plot
plot(DT_performance1, lwd=2, colorize=TRUE, main="ROC Decision Tree: Traditional Recursive Partitioning")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
# Confusion Matrix
```{r}
defaultPredict <- predict(m1dt,m1test, type = 'class')
matrix <- table(defaultPredict, m1test$DefaultedLoans)
print(matrix)
write.csv(m1test, "Data/m1test.csv")
```
# KS & AUC DT
```{r}
DT_AUROC <- round(performance(DT_prediction1, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance1,'y.values')[[1]]-attr(DT_performance1,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
# Final optimized decision tree
```{r}
m3dt <- rpart(DefaultedLoans ~ CreditRating + InterestIncome +
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,method = "class",data=m3train, control = rpart.control(minisplit=5,cp = 0.001))
m3test$DT_score <- predict(m3dt,type='prob',m3test)
DT_prediction3 <- prediction(m3test$DT_score[,2],m3test$DefaultedLoans)
DT_performance3 <- performance(DT_prediction3,"tpr","fpr")
# MOdel performance plot
plot(DT_performance3, lwd=2, colorize=TRUE, main="ROC Decision Tree: Traditional Recursive Partitioning")
lines(x=c(0, 1), y=c(0, 1), col="red", lwd=1, lty=3);
lines(x=c(1, 0), y=c(0, 1), col="green", lwd=1, lty=4)
```
# Confusion Matrix
```{r}
defaultPredict <- predict(m3dt,m3test, type = 'class')
matrix <- table(defaultPredict, m3test$DefaultedLoans)
print(matrix)
write.csv(m3test, "Data/m3test.csv")
```
# KS & AUC DT
```{r}
DT_AUROC <- round(performance(DT_prediction3, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance3,'y.values')[[1]]-attr(DT_performance3,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
#Results
## Performance of Original Dataset
```{r}
#Original
plot(m2perflr, col='blue', lty=1, main='ROCs: Model Performance Comparision(Original Data') # Modified data(without scaling)
plot(DT_performance2, col='red',lty=2, add=TRUE); # Original Data Set
legend(0.6,0.5,
c('Logistic Regression','Decision tree'),
col=c('blue', "red"),
lwd=3);
lines(c(0,1),c(0,1),col = "gray", lty = 4 ) # random line
```
## Modified Data (Unnormalized data)
```{r}
#Without Scaling
plot(m1perflr, col='blue', lty=1, main='ROCs: Model Performance Comparision (Unnormalized Data)') # Modified data(without scaling)
plot(DT_performance1, col='red',lty=2, add=TRUE); # Original Data Set
legend(0.6,0.5,
c('Logistic Regression','Decision tree'),
col=c('blue',"red"),
lwd=3);
lines(c(0,1),c(0,1),col = "gray", lty = 4 ) # random line
```
## Modified Data (Normalized data)
```{r}
#Final
plot(m3perflr, col='blue', lty=1, main='ROCs: Model Performance Comparision(Normalized Data') # Modified data(without scaling)
plot(DT_performance3, col='red',lty=2, add=TRUE); # Original Data Set
legend(0.6,0.5,
c('Logistic Regression','Decision tree'),
col=c('blue', "red"),
lwd=3);
lines(c(0,1),c(0,1),col = "gray", lty = 4 ) # random line
```
#Logistic Regression
##KS, Gini & AUC m2
```{r}
lr_KS <- round(max(attr(m2perflr,'y.values')[[1]]-attr(m2perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m2predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("AUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
##KS, Gini & AUC m1
```{r}
accuracy <- 89.01
lr_KS <- round(max(attr(m1perflr,'y.values')[[1]]-attr(m1perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m1predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("Accuracy:",accuracy, "\tAUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
##KS, Gini & AUC m3
```{r}
lr_KS <- round(max(attr(m3perflr,'y.values')[[1]]-attr(m3perflr,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(m3predlr, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("AUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
# KS & AUC DT Original
```{r}
DT_AUROC <- round(performance(DT_prediction2, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance2,'y.values')[[1]]-attr(DT_performance2,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
# KS & AUC DT
```{r}
DT_AUROC <- round(performance(DT_prediction1, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance1,'y.values')[[1]]-attr(DT_performance1,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
# KS & AUC DT
```{r}
DT_AUROC <- round(performance(DT_prediction3, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance3,'y.values')[[1]]-attr(DT_performance3,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```