-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit_Decision_tree.Rmd
294 lines (232 loc) · 7 KB
/
credit_Decision_tree.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
---
output:
html_document: default
pdf_document: default
word_document: default
---
```{r}
#read the dataset
file=readRDS("loan_data_1.rds")
```
```{r}
#create frame for dataset
credit_frame=data.frame(file)
```
```{r}
str(credit_frame)
```
```{r}
head(credit_frame)
```
```{r}
sapply(credit_frame,function(x) sum(is.na(x)))
```
```{r}
library(gmodels)
CrossTable(credit_frame$loan_status)
```
```{r}
CrossTable(credit_frame$home_ownership)
```
```{r}
CrossTable(credit_frame$grade)
```
```{r}
print(summary(credit_frame$loan_amnt))
print(summary(credit_frame$int_rate))
print(summary(credit_frame$emp_length))
```
```{r}
plot(credit_frame$annual_inc,ylab="Annual Income")
```
```{r}
hist(credit_frame$annual_inc,sqrt(nrow(credit_frame)),xlab = "Annual Income",main = "histogram of annual income")
```
From scatter plot extrem value is removed
```{r}
index_outlier_annua_inc<-which(credit_frame$annual_inc>3000000)
index_outlier_annua_inc
nrow(credit_frame)
credit_frame<-credit_frame[-index_outlier_annua_inc,]
nrow(credit_frame)
```
```{r}
plot(credit_frame$annual_inc,ylab="Annual Income")
```
```{r}
hist(credit_frame$annual_inc,sqrt(nrow(credit_frame)),xlab = "Annual Income",main = "histogram of annual income")
```
still not look normanl
```{r}
boxplot(credit_frame$annual_inc)
```
```{r}
summary(credit_frame$annual_inc)
```
```{r}
temp_vector<-c("log_annual_income")
credit_frame[,temp_vector]<-NA
```
```{r}
credit_frame$log_annual_income<-log(credit_frame$annual_inc)
```
```{r}
hist(credit_frame$log_annual_inc,sqrt(nrow(credit_frame)),xlab = "log Annual Income",main = "histogram of log annual income")
```
```{r}
summary(credit_frame$log_annual_income)
```
Looks normal distribution acepted
```{r}
boxplot(credit_frame$age,ylab="age")
```
```{r}
hist(credit_frame$age,sqrt(nrow(credit_frame)),ylab = "frequency",xlab = "age",main = "Histogram of age")
```
```{r}
summary(credit_frame$age)
```
Looks constant mean and median So distribution is quet normal
Interest rate has some missing values so fill it with mean
```{r}
summary(credit_frame$int_rate)
index_int_rate_NA<-which(is.na(credit_frame$int_rate))
credit_frame$int_rate[index_int_rate_NA]<-mean(credit_frame$int_rate,na.rm = TRUE)
summary(credit_frame$int_rate)
```
```{r}
plot(credit_frame$int_rate,xlab = "frequency",ylab = "interest rate",main = "Interest rate")
```
```{r}
hist(credit_frame$int_rate,sqrt(nrow(credit_frame)),ylab = "frequency",xlab = "interest rate",main = "Histogram of Interest rate")
```
```{r}
boxplot(credit_frame$int_rate,ylab="Interst rate")
```
```{r}
summary(credit_frame$int_rate)
```
From distribution and summary it looks normal and acepted
```{r}
summary(credit_frame$emp_length)
index_emp_length_NA<-which(is.na(credit_frame$emp_length))
credit_frame$emp_length[index_emp_length_NA]<-mean(credit_frame$emp_length,na.rm = TRUE)
summary(credit_frame$emp_length)
```
```{r}
boxplot(credit_frame$emp_length,main="Boxplot of log emp length")
```
```{r}
hist(credit_frame$emp_length,sqrt(nrow(credit_frame)),ylab = "Frequency",xlab = "Emp length",main = "Histogram of Emp length")
```
Emp length has extrem values so preprocess it
```{r}
credit_frame$emp_length<-credit_frame$emp_length+1
```
```{r}
temp_vector<-c("log_emp_length")
credit_frame[,temp_vector]<-NA
```
```{r}
credit_frame$log_emp_length<-log(credit_frame$emp_length)
```
```{r}
boxplot(credit_frame$log_emp_length)
```
```{r}
hist(credit_frame$log_emp_length,ylab = "frequency",xlab ="Log emp length ", main = "Histogram of Log of emp length")
```
```{r}
boxplot(credit_frame$log_emp_length,ylab="Log of emp length")
```
```{r}
summary(credit_frame$log_emp_length)
```
Looks normal because normal mean and median
```{r}
boxplot(credit_frame$loan_amnt)
```
```{r}
summary(credit_frame$loan_amnt)
```
```{r}
hist(credit_frame$loan_amnt,sqrt(nrow(credit_frame)),ylab = "Frequency",xlab = "Loan Amount", main = "Histogram of Loan Amount")
```
```{r}
temp_vector<-c("log_loan_amnt")
credit_frame[,temp_vector]<-NA
```
```{r}
credit_frame$log_loan_amnt<-log(credit_frame$loan_amnt)
```
```{r}
boxplot(credit_frame$log_loan_amnt,main="Boxplot of log loan amnt")
```
```{r}
hist(credit_frame$log_loan_amnt,sqrt(nrow(credit_frame)),ylab = "frequency",xlab = "Log loan amnt",main = "Histogram of Log loan amnt")
```
```{r}
summary(credit_frame$log_loan_amnt)
```
lets create some new features
```{r}
CrossTable(credit_frame$grade,credit_frame$loan_status,prop.r = TRUE,prop.c = FALSE,prop.t = FALSE,prop.chisq = FALSE)
```
Accordingly from above observation the loan status depends on the grade of the customer
```{r}
temp_vector<-c("grade_multiplier")
credit_frame[,temp_vector]<-NA
```
```{r}
credit_frame<-within(credit_frame,grade_multiplier[grade=='A']<-(0.941))
credit_frame<-within(credit_frame,grade_multiplier[grade=='B']<-(0.894))
credit_frame<-within(credit_frame,grade_multiplier[grade=='C']<-(0.853))
credit_frame<-within(credit_frame,grade_multiplier[grade=='D']<-(0.820))
credit_frame<-within(credit_frame,grade_multiplier[grade=='E']<-(0.797))
credit_frame<-within(credit_frame,grade_multiplier[grade=='F']<-(0.735))
credit_frame<-within(credit_frame,grade_multiplier[grade=='G']<-(0.625))
```
```{r}
temp_vector<-c("loan_amnt_int_rate_multiplier")
credit_frame[,temp_vector]<-NA
```
```{r}
credit_frame$loan_amnt_int_rate_multiplier<-((credit_frame$int_rate)*(credit_frame$log_loan_amnt)*(credit_frame$grade_multiplier))
```
```{r}
hist(credit_frame$loan_amnt_int_rate_multiplier,sqrt(nrow(credit_frame)),ylab = "frequency",xlab = "loan_amnt_int_rate_multiplier",main = "Histogram of loan_amnt_int_rate_multiplier ")
```
```{r}
boxplot(credit_frame$loan_amnt_int_rate_multiplier,main="loan_amnt_int_rate_multiplier")
```
```{r}
summary(credit_frame$loan_amnt_int_rate_multiplier)
```
```{r}
index_train <- sample(1:nrow(credit_frame), 2 / 3 * nrow(credit_frame))
```
```{r}
training_set<-credit_frame[index_train,]
test_set<-credit_frame[-index_train,]
```
```{r}
x_test=test_set[,2:13]
y_test=test_set[,1]
```
```{r}
library(rpart)
dt_model<-rpart(loan_status~loan_amnt_int_rate_multiplier+grade+log_loan_amnt+int_rate+log_emp_length+log_annual_income+age+annual_inc+home_ownership+emp_length+loan_amnt, method = 'class',data = training_set, parms = list(prior = c(0.7, 0.3)),control = rpart.control(cp = 0.001) )
```
```{r}
summary(dt_model)
```
```{r}
predicted1<-predict(dt_model,x_test,type = 'class')
```
```{r}
mean(predicted1==y_test)
```
```{r}
library(caTools)
table(test_set$loan_status,predicted1)
```