-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathResults.Rmd
177 lines (152 loc) · 5.52 KB
/
Results.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
---
title: "Results & Analysis"
output:
html_document: default
html_notebook: default
---
#Logistic Regression Model (Stepwise)
```{r}
lr <- glm(DefaultedLoans ~ InterestIncome + CreditRating+
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
family = "binomial", data = trainDatav2)
summary(lr)
```
```{r}
lr <- step(lr)
```
```{r}
summary(lr)
save(lr, file = "Model/lr.rda")
```
```{r}
significant.variables <- summary(lr)$coeff[-1,4] < 0.05
names(significant.variables)[significant.variables == TRUE]
```
```{r}
prob <- predict(lr, type = "response")
res <- residuals(lr, type = "deviance")
#Plot Residuals
plot(predict(lr), res,
xlab="Fitted values", ylab = "Residuals",
ylim = max(abs(res)) * c(-1,1))
```
```{r}
#score test data set
library(ROCR)
testDatav2$lr_score <- predict(lr,type='response',testDatav2)
lr_prediction <- prediction(testDatav2$lr_score, testDatav2$DefaultedLoans)
lr_performance <- performance(lr_prediction,"tpr","fpr")
#ROC
plot(lr_performance, 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)
```
```{r}
defaultPredict <- predict(lr, trainDatav2, type = 'response')
matrix <- table(defaultPredict, trainDatav2$DefaultedLoans)
```
```{r}
# Plot precision/recall curve
lr_performance_precision <- performance(lr_prediction, measure = "prec", x.measure = "rec")
plot(lr_performance_precision, main="LR Logistic:Precision vs Recall curve")
```
```{r}
# Plot accuracy as function of threshold
lr_performance_accuracy <- performance(lr_prediction, measure = "acc")
plot(lr_performance_accuracy, main="LR Logistic:Accuracy as function of threshold")
```
```{r}
#KS, Gini & AUC m1
lr_KS <- round(max(attr(lr_performance,'y.values')[[1]]-attr(lr_performance,'x.values')[[1]])*100, 2)
lr_AUROC <- round(performance(lr_prediction, measure = "auc")@y.values[[1]]*100, 2)
lr_Gini <- (2*lr_AUROC - 100)
cat("AUROC: ",lr_AUROC,"\tKS: ", lr_KS, "\tGini:", lr_Gini, "\n")
```
```{r}
```
# Decision Tree Results
```{r}
#Decision Tree for Tableu
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)
DT <- rpart(DefaultedLoans ~ InterestIncome +CreditRating +ValuationAgeYears+
PropertyValue + LoanBalance + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude
,method = "class",data=trainDatav2,
control = rpart.control(minisplit=50,cp = 0.001215))
save(fit, file = "Model/DT.rda")
prp(DT,type=0,extra = 101,box.palette = "auto")
rpart.plot(DT, branch.lty=2,extra = 104,box.palette = "GnBu")
```
```{r}
# score test data
testDatav2$DT_score <- predict(DT,type='prob',testDatav2)
DT_prediction <- prediction(testDatav2$DT_score[,2],testDatav2$DefaultedLoans)
DT_performance <- performance(DT_prediction,"tpr","fpr")
# MOdel performance plot
plot(DT_performance, 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)
```
```{r}
# Plot precision/recall curve
DT_performance_precision <- performance(DT_prediction, measure = "prec", x.measure = "rec")
plot(DT_performance_precision, main="Decision Tree: Precision vs Recall curve")
```
```{r}
# Plot accuracy as function of threshold
DT_perf_acc <- performance(DT_prediction, measure = "acc")
plot(DT_perf_acc, main="m2 Recursive Partitioning:Accuracy as function of threshold")
```
```{r}
# KS & AUC DT
DT_AUROC <- round(performance(DT_prediction, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance,'y.values')[[1]]-attr(DT_performance,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
```{r}
defaultPredict <- predict(DT, trainDatav2, type = 'class')
matrix <- table(defaultPredict, trainDatav2$DefaultedLoans)
print(matrix)
cm<- confusionMatrix(defaultPredict,trainDatav2$DefaultedLoans)
fourfoldplot(cm$table)
print((matrix[4]+matrix[1])/nrow(trainDatav2))
```
```{r}
plot(lr_performance, col='blue', lty=1, main='ROCs: Model Performance Comparision') # logistic regression
plot(DT_performance, col='red',lty=2, add=TRUE); # simple
legend(0.6,0.5,
c('LR :Logistic regression','DT: Decision tree'),
col=c('blue','red'),
lwd=3);
lines(c(0,1),c(0,1),col = "gray", lty = 4 ) # random line
```
```{r}
# KS & AUC DT
DT_AUROC <- round(performance(DT_prediction, measure = "auc")@y.values[[1]]*100, 2)
DT_KS <- round(max(attr(DT_performance,'y.values')[[1]]-attr(DT_performance,'x.values')[[1]])*100, 2)
DT_Gini <- (2*DT_AUROC - 100)
cat("AUROC: ",DT_AUROC,"\tKS: ", DT_KS, "\tGini:", DT_Gini, "\n")
```
```{r}
plot(lr_performance, col='blue', lty=1, main='ROCs: Model Performance Comparision') # logistic regression
plot(DT_performance, col='red',lty=2, add=TRUE); # simple
legend(0.6,0.5,
c('LR :Logistic regression','DT: Decision tree'),
col=c('blue','red'),
lwd=3);
lines(c(0,1),c(0,1),col = "gray", lty = 4 ) # random line
```
```{r}
write.csv(testDatav2, "Data/test_results.csv")
```