-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCredit_Risk_Analysis.Rmd
529 lines (413 loc) · 12.2 KB
/
Credit_Risk_Analysis.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(readxl)
library(ggplot2)
library(dplyr)
```
```{r}
data <- read_excel("/file/path")
head(data$RESPONSE)
table(data$RESPONSE)
summary(data)
str(data)
head(data)
```
```{r}
# 1 A
sum(is.na(data))
data$CHK_ACCT <- as.factor(data$CHK_ACCT)
ggplot(data, aes(x = RESPONSE)) +
geom_bar() +
ggtitle("Distribution of Credit Risk (Good vs. Bad)") +
xlab("Credit Risk") +
ylab("Count")
print(table(data$RESPONSE))
# 0 1
# 300 700
credit_risk_distribution <- table(data$RESPONSE) /
nrow(data)
print(credit_risk_distribution)
# 0 1
# 0.3 0.7
# good credit (1): 70% , bad credit (0): 30%
```
```{r}
# b
data %>%
group_by(RESPONSE) %>%
summarise(
min_age = min(AGE),
max_age = max(AGE),
avg_age = mean(AGE),
median_age = median(AGE)
)
ggplot(data, aes(x = RESPONSE, y = AGE)) +
geom_boxplot() +
ggtitle("Distribution of Age by Credit Risk") +
xlab("Credit Risk") +
ylab("Age")
ggplot(data, aes(x = AGE, fill = RESPONSE)) +
geom_density(alpha = 0.5) +
ggtitle("Density Distribution of Age by Credit Risk") +
xlab("Age") +
ylab("Density")
# on avg , the younger age has greater risj
```
```{r}
# c
data %>%
group_by(RESPONSE) %>%
summarise(
min_amount = min(AMOUNT),
max_amount = max(AMOUNT),
avg_amount = mean(AMOUNT),
median_amount = median(AMOUNT)
)
avg_good_credit <- mean(data$AMOUNT[data$RESPONSE == "Good"])
avg_bad_credit <- mean(data$AMOUNT[data$RESPONSE == "Bad"])
# Initialize ggplot only once
p <- ggplot(data, aes(x = AMOUNT, fill = RESPONSE))
# Add the first histogram layer
p <- p + geom_histogram(binwidth = 500, alpha = 0.5, position = "identity")
# Add the second histogram layer
# Note: It's not clear why you'd add the same histogram twice, but assuming you have a reason
p <- p + geom_histogram(binwidth = 500, alpha = 0.5, position = "identity")
# Add a vertical line
p <- p + geom_vline(aes(xintercept = avg_good_credit, color = "Good"),
linetype = "dashed", size = 1)
# Add title and labels
p <- p + ggtitle("Distribution of Credit Amounts by Credit Risk") +
xlab("Credit Amount") +
ylab("Frequency")
# Add custom colors
p <- p + scale_color_manual(values = c("Good" = "blue", "Bad" = "red"))
# Print the plot
print(p)
# Initialize ggplot only once
p <- ggplot(data, aes(x = AMOUNT, fill = RESPONSE))
# Add the first histogram layer
p <- p + geom_histogram(binwidth = 500, alpha = 0.5, position = "identity")
# Add the second histogram layer
# Note: It's not clear why you'd add the same histogram twice, but assuming you have a reason
p <- p + geom_histogram(binwidth = 500, alpha = 0.5, position = "identity")
# Add the first vertical line for 'Good' credit
p <- p + geom_vline(aes(xintercept = avg_good_credit, color = "Good"),
linetype = "dashed", size = 1)
# Add the second vertical line for 'Bad' credit
p <- p + geom_vline(aes(xintercept = avg_bad_credit, color = "Bad"),
linetype = "dashed", size = 1)
# Add title and labels
p <- p + ggtitle("Distribution of Credit Amounts by Credit Risk") +
xlab("Credit Amount") +
ylab("Frequency")
# Add custom colors
p <- p + scale_color_manual(values = c("Good" = "blue", "Bad" = "red"))
# Print the plot
print(p)
# credit amounts are higher on avg for bad credit risks
```
```{r}
# d
data %>%
summarise(
min_duration = min(DURATION),
max_duration = max(DURATION),
avg_duration = mean(DURATION),
median_duration = median(DURATION)
)
ggplot(data, aes(x = DURATION)) +
geom_histogram(aes(y = ..density..), binwidth = 3, fill = "blue", alpha = 0.7) +
geom_density(alpha = 0.2, fill = "#FF6666") +
ggtitle("Distribution of Loan Durations") +
xlab("Duration (in months)") +
ylab("Density")
# majority of loan durations are on the shorter end
```
```{r}
# f
ggplot(data, aes(x = DURATION)) +
geom_histogram(binwidth = 5, fill = "blue", alpha = 0.7) +
facet_wrap(~RESPONSE) +
ggtitle("Loan Duration by Credit Risk") +
xlab("Loan Duration (Months)") +
ylab("Frequency")
ggplot(data, aes(x = DURATION, fill = RESPONSE)) +
geom_density(alpha = 0.5) +
facet_wrap(~RESPONSE) +
ggtitle("Loan Duration by Credit Risk") +
xlab("Loan Duration (Months)") +
ylab("Density")
# more loan durations for good credit risk are shorter
```
```{r}
# g
ggplot(data, aes(x = RESPONSE)) +
geom_bar(aes(fill = RESPONSE), position = "dodge") +
ggtitle("Distribution of Credit Risk Categories") +
xlab("Credit Risk Category") +
ylab("Frequency") +
scale_fill_brewer(palette = "Set1")
```
```{r}
# h
ggplot(data, aes(x = AGE)) +
geom_histogram(binwidth = 5, fill = "blue", alpha = 0.7) +
ggtitle("Age Distribution of Credit Applicants") +
xlab("Age") +
ylab("Frequency")
```
```{r}
# i
data$RESPONSE <- as.factor(data$RESPONSE)
ggplot(data, aes(x = AGE, y = AMOUNT)) +
geom_point(aes(color = RESPONSE), alpha = 0.6) +
ggtitle("Scatterplot of Credit Amount vs Age") +
xlab("Age") +
ylab("Credit Amount") +
scale_color_brewer(palette = "Set1")
# j
ggplot(data, aes(x = RESPONSE, y = AMOUNT)) +
geom_boxplot(aes(fill = RESPONSE)) +
ggtitle("Boxplot of Credit Amounts by Credit Risk Category") +
xlab("Credit Risk Category") +
ylab("Credit Amount") +
scale_fill_brewer(palette = "Set1")
```
```{r}
# l
summary_stats <- data %>%
group_by(RESPONSE) %>%
summarise(
mean_age = mean(AGE, na.rm = TRUE),
median_age = median(AGE, na.rm = TRUE)
)
print(summary_stats)
# RESPONSE mean_age median_age
# 0 34.0 31
# 1 36.2 34
```
```{r}
# m
subset_data <- data %>%
filter(OWN_RES == 1)
credit_risk_dist <- subset_data %>%
group_by(RESPONSE) %>%
summarise(count = n())
print(credit_risk_dist)
avg_age <- mean(subset_data$AGE, na.rm = TRUE)
print(paste("Average age:", avg_age))
# avg age is higher by a little more than a year, and more people that own houses have good credit risk
```
```{r}
# n
numerical_vars <- data %>% select(AGE, AMOUNT, DURATION)
cor_matrix <- cor(numerical_vars, use = "pairwise.complete.obs")
library(reshape2)
melted_cor_matrix <- melt(cor_matrix)
ggplot(data = melted_cor_matrix, aes(x = Var1, y = Var2)) +
geom_tile(aes(fill = value), colour = "white") +
scale_fill_gradient2(low = "blue", high = "red", mid = "white", midpoint = 0) +
geom_text(aes(label = sprintf("%.2f", value)), vjust = 1) +
theme_minimal() +
labs(fill = "Correlation")
# correlation for low age and low duration, and med age med duration, and high age high duration is highest correlation at 1
```
```{r}
# o
threshold <- quantile(data$AMOUNT, 0.75) # For example, setting the 75th percentile as the threshold
data$HighCredit <- ifelse(data$AMOUNT > threshold, 1, 0)
data$AgeAmountInteraction <- data$AGE * data$AMOUNT
data$AmountZScore <- (data$AMOUNT - mean(data$AMOUNT)) / sd(data$AMOUNT)
sum(is.na(data))
summary(data)
summary(select(data, AGE, AMOUNT, DURATION))
data %>%
group_by(RESPONSE) %>%
summarise(
Mean_Age = mean(AGE, na.rm = TRUE),
Median_Age = median(AGE, na.rm = TRUE),
Mean_Amount = mean(AMOUNT, na.rm = TRUE),
Median_Amount = median(AMOUNT, na.rm = TRUE)
)
chisq.test(data$RESPONSE, data$AGE)
```
```{r}
# 2
## PROBLEM 2
library(caTools)
set.seed(123)
split <- sample.split(data$RESPONSE, SplitRatio = 0.5) # For a 50/50 split
train_data <- subset(data, split == TRUE)
test_data <- subset(data, split == FALSE)
library(rpart)
fit <- rpart(RESPONSE ~ ., data = train_data, method = "class")
pred <- predict(fit, newdata = test_data, type = "class")
```
```{r}
library(caret)
confusionMatrix(pred, test_data$RESPONSE)
# Accuracy : 0.716
fit <- rpart(RESPONSE ~ ., data = train_data, method = "class", minsplit = 20, cp = 0.01)
pruned_fit <- prune(fit, cp = fit$cptable[which.min(fit$cptable[, "xerror"]), "CP"])
library(C50)
```
```{r}
# C5.0 model
fit_C50 <- C5.0(RESPONSE ~ ., data = train_data)
```
```{r}
fit$variable.importance
# chk acct most important at 30.372
```
```{r}
loaded_model <- readRDS("decision_tree_model.rds")
library(randomForest)
colnames(train_data)[which(colnames(train_data) == "OBS#")] <- "OBS"
colnames(train_data)[which(colnames(train_data) == "RADIO/TV")] <- "RADIO_TV"
```
```{r}
#
fit <- rpart(RESPONSE ~ ., data = train_data, method = "class")
print(fit$variable.importance)
```
```{r}
# 50/50 split
index1 <- createDataPartition(data$RESPONSE, p = 0.5, list = FALSE)
train1 <- data[index1, ]
test1 <- data[-index1, ]
```
```{r}
# 70/30 split
index2 <- createDataPartition(data$RESPONSE, p = 0.7, list = FALSE)
train2 <- data[index2, ]
test2 <- data[-index2, ]
```
```{r}
# 80/20 split
index3 <- createDataPartition(data$RESPONSE, p = 0.8, list = FALSE)
train3 <- data[index3, ]
test3 <- data[-index3, ]
```
```{r}
library(rpart)
fit_CART1 <- rpart(RESPONSE ~ ., data = train1, method = "class")
fit_CART2 <- rpart(RESPONSE ~ ., data = train2, method = "class")
fit_CART3 <- rpart(RESPONSE ~ ., data = train3, method = "class")
```
```{r}
library(C50)
fit_C50_1 <- C5.0(RESPONSE ~ ., data = train1)
fit_C50_2 <- C5.0(RESPONSE ~ ., data = train2)
fit_C50_3 <- C5.0(RESPONSE ~ ., data = train3)
```
```{r}
pred_CART2 <- predict(fit_CART2, newdata = test2, type = "class")
confusionMatrix(pred_CART2, test2$RESPONSE)
# Accuracy : 0.7433
```
```{r}
pred_CART3 <- predict(fit_CART3, newdata = test3, type = "class")
confusionMatrix(pred_CART3, test3$RESPONSE)
# Accuracy : 0.75
```
```{r}
# For C5.0 Models
pred_C50_1 <- predict(fit_C50_1, newdata = test1)
confusionMatrix(pred_C50_1, test1$RESPONSE)
# Accuracy : 0.706
```
```{r}
pred_C50_2 <- predict(fit_C50_2, newdata = test2)
confusionMatrix(pred_C50_2, test2$RESPONSE)
# Accuracy : 0.7067
```
```{r}
pred_C50_3 <- predict(fit_C50_3, newdata = test3)
confusionMatrix(pred_C50_3, test3$RESPONSE)
# Accuracy : 0.725
fit_pruned_CART <- rpart(RESPONSE ~ ., data = train1, method = "class", cp = 0.01)
```
```{r}
## PROBLEM 3
library(rpart)
# misclassification cost matrix
loss_matrix <- matrix(
c(
0, 500,
100, 0
),
ncol = 2
)
colnames(loss_matrix) <- rownames(loss_matrix) <- c("Good", "Bad")
```
```{r}
set.seed(123)
split <- createDataPartition(data$RESPONSE, p = 0.7, list = FALSE)
training <- data[split, ]
testing <- data[-split, ]
```
```{r}
model_with_cost <- rpart(RESPONSE ~ ., data = training, method = "class", parms = list(loss = loss_matrix))
```
```{r}
predictions_with_cost <- predict(model_with_cost, testing, type = "class")
```
```{r}
conf_matrix_with_cost <- table(predictions_with_cost, testing$RESPONSE)
print(conf_matrix_with_cost)
pred <- as.factor(pred)
testing$RESPONSE <- as.factor(testing$RESPONSE)
model_without_cost <- rpart(RESPONSE ~ ., data = training, method = "class")
predictions_without_cost <- predict(model_without_cost, testing, type = "class")
```
```{r}
conf_matrix_without_cost <- table(predictions_without_cost, testing$RESPONSE)
print(conf_matrix_without_cost)
# 0 1
# 0 44 27
# 1 52 177
```
```{r}
library(rpart.plot)
```
```{r}
rpart.plot(fit, type = 4, extra = 101)
good_applicants <- subset(data, AGE > 30 & AMOUNT > 700)
print(good_applicants)
head(good_applicants)
fit <- rpart(RESPONSE ~ ., data = train_data, method = "class")
```
```{r}
printcp(fit)
plot(fit)
text(fit)
```
```{r}
# (4)
# 4 What are the best nodes for classifying “Good” applicants? Output rules corresponding to these. Please explain why you chose these nodes.
printcp(fit)
summary(fit)
print(fit)
pruned_tree <- prune(fit, cp = 0.01)
library(rpart.plot)
rpart.rules(pruned_tree)
```
```{r}
# (5) What are the important variables to estimate the risk to customers?
print(fit$variable.importance)
```
```{r}
# for rpart
fit <- rpart(RESPONSE ~ ., data = train_data, method = "class")
fit$variable.importance
# the most important variables are 1. CHK_ACCT at 30.37, 2. SAV_ACCT at 12.33, and 3. DURATION at 11.70
```
```{r}
# (6) Summarize your findings.
# Checking Account Status (CHK_ACCT) has the highest score at 30.37, thus the status of an applicant's checking account is the most important variable in assessing risk
```