library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
Loan_default=read.csv("D://rstudio/code/Loan_default.csv")
df <- Loan_default %>%
select(Default, CreditScore, Income, LoanAmount, LoanTerm, LoanPurpose)
df$Default <- as.factor(df$Default)
df$LoanPurpose <- as.factor(df$LoanPurpose)
Bộ dữ liệu Loan Default Prediction Dataset được cung cấp trên Kaggle, gồm 255.347 quan sát và 18 biến, mô phỏng dữ liệu tín dụng tiêu dùng. Các biến bao gồm đặc điểm cá nhân (tuổi, thu nhập, nghề nghiệp), lịch sử tín dụng, thông tin khoản vay, và biến kết quả
dim(Loan_default)
## [1] 255347 18
colSums(is.na(Loan_default))
## LoanID Age Income LoanAmount CreditScore
## 0 0 0 0 0
## MonthsEmployed NumCreditLines InterestRate LoanTerm DTIRatio
## 0 0 0 0 0
## Education EmploymentType MaritalStatus HasMortgage HasDependents
## 0 0 0 0 0
## LoanPurpose HasCoSigner Default
## 0 0 0
sum(duplicated(Loan_default))
## [1] 0
variable_description <- data.frame(
Variable = c("LoanID", "Age", "Income", "LoanAmount", "CreditScore",
"MonthsEmployed", "NumCreditLines", "InterestRate", "LoanTerm", "DTIRatio",
"Education", "EmploymentType", "MaritalStatus", "HasMortgage",
"HasDependents", "LoanPurpose", "HasCoSigner", "Default"),
Description = c("Mã khoản vay", "Tuổi khách hàng", "Thu nhập hàng năm",
"Số tiền vay", "Điểm tín dụng", "Số tháng đã làm việc",
"Số lượng dòng tín dụng hiện tại/trước đây", "Lãi suất vay (%)",
"Thời hạn vay", "Tỷ lệ nợ trên thu nhập", "Trình độ học vấn",
"Loại hình công việc", "Tình trạng hôn nhân",
"Có vay thế chấp hay không", "Có người phụ thuộc hay không",
"Mục đích vay", "Có người đồng ký vay hay không", "Khả năng vỡ nợ")
)
variable_description
## Variable Description
## 1 LoanID Mã khoản vay
## 2 Age Tuổi khách hàng
## 3 Income Thu nhập hàng năm
## 4 LoanAmount Số tiền vay
## 5 CreditScore Điểm tín dụng
## 6 MonthsEmployed Số tháng đã làm việc
## 7 NumCreditLines Số lượng dòng tín dụng hiện tại/trước đây
## 8 InterestRate Lãi suất vay (%)
## 9 LoanTerm Thời hạn vay
## 10 DTIRatio Tỷ lệ nợ trên thu nhập
## 11 Education Trình độ học vấn
## 12 EmploymentType Loại hình công việc
## 13 MaritalStatus Tình trạng hôn nhân
## 14 HasMortgage Có vay thế chấp hay không
## 15 HasDependents Có người phụ thuộc hay không
## 16 LoanPurpose Mục đích vay
## 17 HasCoSigner Có người đồng ký vay hay không
## 18 Default Khả năng vỡ nợ
table(df$Default)
##
## 0 1
## 225694 29653
prop.table(table(df$Default))
##
## 0 1
## 0.8838718 0.1161282
df %>%
count(Default) %>%
mutate(Percent = n / sum(n) * 100,
Label = paste0(Default, " (", round(Percent, 1), "%)")) %>%
ggplot(aes(x = "", y = Percent, fill = Default)) +
geom_col(width = 1, color = "white") +
coord_polar("y", start = 0) +
geom_text(aes(label = Label), position = position_stack(vjust = 0.5),
color = "white", size = 5) +
labs(title = "Biểu đồ tròn: Tỷ lệ vỡ nợ") +
scale_fill_manual(values = c("#377eb8", "#e41a1c")) +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
Phần này sẽ trình bày thống kê mô tả cho các biến độc lập chính.
summary(df[, c("CreditScore", "Income", "LoanAmount", "LoanTerm")])
## CreditScore Income LoanAmount LoanTerm
## Min. :300.0 Min. : 15000 Min. : 5000 Min. :12.00
## 1st Qu.:437.0 1st Qu.: 48826 1st Qu.: 66156 1st Qu.:24.00
## Median :574.0 Median : 82466 Median :127556 Median :36.00
## Mean :574.3 Mean : 82499 Mean :127579 Mean :36.03
## 3rd Qu.:712.0 3rd Qu.:116219 3rd Qu.:188985 3rd Qu.:48.00
## Max. :849.0 Max. :149999 Max. :249999 Max. :60.00
boxplot(df$LoanAmount, main = "Boxplot LoanAmount", ylab = "LoanAmount", col = "lightblue")
boxplot(df$CreditScore, main = "Boxplot CreditScore", ylab = "CreditScore", col = "lightgray")
boxplot(df$Income, main = "Boxplot Income", ylab = "Income", col = "lightgreen")
boxplot(df$LoanTerm, main = "Boxplot LoanTerm", ylab = "LoanTerm (tháng)", col = "lightpink")
df %>%
group_by(Default) %>%
summarise(Mean = mean(CreditScore), Median = median(CreditScore),
SD = sd(CreditScore), Min = min(CreditScore), Max = max(CreditScore))
## # A tibble: 2 × 6
## Default Mean Median SD Min Max
## <fct> <dbl> <dbl> <dbl> <int> <int>
## 1 0 576. 577 159. 300 849
## 2 1 559. 553 159. 300 849
df %>%
group_by(Default) %>%
summarise(Mean_CreditScore = mean(CreditScore, na.rm = TRUE)) %>%
ggplot(aes(x = Default, y = Mean_CreditScore, fill = Default)) +
geom_col(width = 0.5) +
geom_text(aes(label = round(Mean_CreditScore, 1)), vjust = -0.5, size = 5) +
labs(title = "Điểm tín dụng trung bình theo trạng thái vỡ nợ",
x = "Default (0 = Không vỡ nợ, 1 = Vỡ nợ)",
y = "CreditScore (Mean)") +
theme_minimal()
df %>%
group_by(Default) %>%
summarise(Mean = mean(Income), Median = median(Income),
SD = sd(Income), Min = min(Income), Max = max(Income))
## # A tibble: 2 × 6
## Default Mean Median SD Min Max
## <fct> <dbl> <dbl> <dbl> <int> <int>
## 1 0 83899. 84238. 38499. 15000 149999
## 2 1 71845. 66566 40785. 15004 149995
df %>%
group_by(Default) %>%
summarise(Mean_Income = mean(Income)) %>%
ggplot(aes(x = Default, y = Mean_Income, fill = Default)) +
geom_col(width = 0.5) +
geom_text(aes(label = round(Mean_Income, 1)), vjust = -0.5, size = 5) +
labs(title = "Thu nhập trung bình theo trạng thái vỡ nợ",
x = "Default (0 = Không vỡ nợ, 1 = Vỡ nợ)",
y = "Income (Mean)") +
theme_minimal()
df %>%
group_by(Default) %>%
summarise(Mean = mean(LoanAmount), Median = median(LoanAmount),
SD = sd(LoanAmount), Min = min(LoanAmount), Max = max(LoanAmount))
## # A tibble: 2 × 6
## Default Mean Median SD Min Max
## <fct> <dbl> <dbl> <dbl> <int> <int>
## 1 0 125354. 124236 70708. 5001 249999
## 2 1 144515. 152672 69548. 5000 249993
df %>%
group_by(Default) %>%
summarise(Mean_LoanAmount = mean(LoanAmount)) %>%
ggplot(aes(x = Default, y = Mean_LoanAmount, fill = Default)) +
geom_col(width = 0.5) +
geom_text(aes(label = round(Mean_LoanAmount, 1)), vjust = -0.5, size = 5) +
labs(title = "Số tiền vay trung bình theo trạng thái vỡ nợ",
x = "Default (0 = Không vỡ nợ, 1 = Vỡ nợ)",
y = "LoanAmount (Mean)") +
theme_minimal()
df %>%
group_by(Default) %>%
summarise(Mean = mean(LoanTerm), Median = median(LoanTerm),
SD = sd(LoanTerm), Min = min(LoanTerm), Max = max(LoanTerm))
## # A tibble: 2 × 6
## Default Mean Median SD Min Max
## <fct> <dbl> <dbl> <dbl> <int> <int>
## 1 0 36.0 36 17.0 12 60
## 2 1 36.1 36 17.0 12 60
df %>%
group_by(Default) %>%
summarise(Mean_LoanTerm = mean(LoanTerm)) %>%
ggplot(aes(x = Default, y = Mean_LoanTerm, fill = Default)) +
geom_col(width = 0.5) +
geom_text(aes(label = round(Mean_LoanTerm, 1)), vjust = -0.5, size = 5) +
labs(title = "Thời hạn vay trung bình theo trạng thái vỡ nợ",
x = "Default (0 = Không vỡ nợ, 1 = Vỡ nợ)",
y = "LoanTerm (Mean)") +
theme_minimal()