-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathModelling 1.Rmd
243 lines (200 loc) · 6.58 KB
/
Modelling 1.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
---
title: "KPMG Thesis Project"
output: html_document
---
#Work on Original Dataset
## Data Reading:
```{r}
library(readr)
#uncomment when you want to read the data from fresh
Data <- read_delim("Data/KPMGDT.csv",",", escape_double = FALSE, trim_ws = TRUE)
```
## Data Stats
```{r}
dim(Data)
names(Data)
summary(Data)
```
##Subsetting Train and Test Datasets
```{r}
set.seed(100)
dataPartition = sample(2,nrow(Data),replace=TRUE,prob=c(0.7,0.3))
trainData <- Data[dataPartition ==1,]
testData <- Data[dataPartition ==2,]
```
##Data Prepration
```{r}
Data$CreditRating <- as.factor(Data$CreditRating)
Data$InterestType <- as.factor(Data$InterestType)
Data$MortgageType <- as.factor(Data$MortgageType)
Data$NewLoan <- as.factor(Data$NewLoan)
Data$ProbationaryLoans <- as.factor(Data$ProbationaryLoans)
Data$LTVCategory <- as.factor(Data$LTVCategory)
Data$InArrears <- as.factor(Data$InArrears)
Data$County <- as.factor(Data$County)
Data$DefaultedLoans <- as.factor(Data$DefaultedLoans)
str(Data)
```
##GLM
###Building Simple GLM Model
```{r}
simpleglm <- glm(DefaultedLoans ~ CreditRating + InterestIncome +
log(PropertyValue) + log(LoanBalance) + AnnualPYMT + LTV +
InterestType + NewLoan + ProbationaryLoans + MortgageYears +
MortgageType + InArrears + County + AddressLatitude + AddressLongitude,
family = "binomial", data = trainData)
summary(simpleglm)
```
###Running simple GLM Model on test data of predict loan default
```{r}
testData$prediction = predict(simpleglm, newdata=testData, type="response")
```
###Testing Accuracy of glm() model
```{r}
table(testData$DefaultedLoans, as.numeric(testData$prediction >= 0.5))
```
### Accuracy
```{r}
(67566+3154)/nrow(testData)
```
### ROC Curve
```{r}
library(ROCR)
#roc_prediction = prediction(testData$prediction, testData$DefaultedLoans)
#as.numeric(performance(pred, "auc")@y.values)
# Make predictions on training set
train_pred = predict(simpleglm, type="response")
# Prediction function
ROCT_Prediction = prediction(train_pred, trainData$DefaultedLoans)
# Performance function
ROCR_Performance = performance(ROCT_Prediction, "tpr", "fpr")
# Plot ROC curve
plot(ROCR_Performance)
# Add colors
plot(ROCR_Performance, colorize=TRUE)
# Add threshold labels
plot(ROCR_Performance, colorize=TRUE, print.cutoffs.at=seq(0,1.5,by=0.1), text.adj=c(-0.2,1.7))
```
## Vizualization of Ireland property price market
```{r}
## Load the library
library(dplyr)
library(maps)
library(reshape2)
library(leaflet)
library(ggplot2)
library(ggmap)
library(gridExtra)
library(htmlwidgets)
library(readr)
weatherIcon <- makeIcon(
iconUrl = "./fig/weather.png",
iconWidth = 30,
iconHeight = 30
)
popupInfo <- paste(testData[['ContractRef']],
", ",
testData[['LoanBalance']],
"<br>",
"Average January Temp in F: ",
testData[['DefaultedLoans']],
"<br>",
"Credit Rating: ",
testData[['CreditRating']],
"<br>",
"LTV: ",
testData[['LTV']],
"<br>",
"Property Value: ",
testData[['PropertyValue']],
sep='')
MapDisplay <- leaflet(testData) %>%
setView(-6.24420, 53.30867, zoom = 12) %>%
addTiles() %>%
addMarkers(testData$AddressLongitude, testData$AddressLatitude, popup= ~ popupInfo,
options = popupOptions(closeButton = TRUE),
clusterOptions = markerClusterOptions(),
icon = weatherIcon)
#MapDisplay
saveWidget(MapDisplay, file="E:/KPMG/KPMG/Output/MapDisplay.html")
```
####################################
#
#Update this part
#
#####################################
```{r}
library(lubridate)
library(ggplot2)
library(dplyr)
library(stringr)
library(caret)
library(rpart)
library(rattle)
library(ROSE)
library(ROCR)
library(MASS)
library(ipred)
library(plyr)
library(rpart.plot)
library(readr)
levels.default(testData$DefaultedLoans)
table(testData$DefaultedLoans, testData$CreditRating)
ggplot(testData, aes(x = testData$LoanBalance)) +geom_histogram(aes(fill = CreditRating)) +facet_wrap(~DefaultedLoans, ncol=1)
```
```{r}
index = createDataPartition(y = testData$DefaultedLoans, p = 0.90)[[1]]
loans.sample <- testData[-index,]
ggplot(loans.sample, aes(x = testData$CreditRating, y = testData$CreditRatingMovement)) + geom_point(aes(color = testData$MortgageYears))
```
```{r}
predictions.1 <- (predict(mydata.rpart.1, mydata.test, type = "class"))
confusionMatrix(predictions.1, mydata.test$DefaultedLoans)
```
#Model for Tableau
```{r}
#mydata <- read.csv("E:/KPMG/Data/Prep/Sample1_3.csv")
lrmodel <- glm(DefaultedLoans ~ CreditRating + LoanBalance + PropertyValue, data = trainData, family = "binomial");
save(lrmodel, file = "Model/mymodel.rda")
```
```{r}
load("mymodel.rda")
prob <- predict(lrmodel, newdata = testData, type = "response")
plot(prob,testData$CreditRating)
```
#Decision Tree for Tableu
```{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)
fit <- rpart(DefaultedLoans ~ NewLoan+County+LoanBalance + PropertyValue+ CreditRating+ AnnualPYMT,method = "class",data=testData, control = rpart.control(minisplit=5,cp = 0.001))
save(fit, file = "Output/myclassificationtree.rda")
print(fit)
prp(fit)
tree.1 <- fit
fancyRpartPlot(tree.1)
```
## Confusion Matrix for traindata
```{r}
defaultPredict <- predict(fit, trainData, type = 'class')
matrix <- table(defaultPredict, trainData$DefaultedLoans)
print(matrix)
print((matrix[4]+matrix[1])/nrow(trainData))
```
## Classification Tree Model for Tableau
```{r}
library(rpart)
library(rpart.plot) # Enhanced tree plots
library(RColorBrewer)
library(rattle) # Fancy tree plot) # Color selection for fancy tree plot
library(party) # Alternative decision tree algorithm
library(partykit) # Convert rpart object to BinaryTree
library(caret)
fit <- rpart(DefaultedLoans ~ CreditRating+ County+ LoanBalance +PropertyValue+ NewLoan + ProbationaryLoans+LTV + LTVCategory +InterestIncome+ AddressLatitude, data=testData, control=rpart.control(minsplit =5,cp=0.001),method="class")
prp(fit)
```