Gói dữ liệu
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library (readxl)
library (DescTools)
library (ggplot2)
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following objects are masked from 'package:DescTools':
##
## MAE, RMSE
##
## The following object is masked from 'package:purrr':
##
## lift
library(epitools)
library(DT)
loan <- read_excel("C:/Users/sylev/Documents/PTDL.xlsx")
table(loan$Gender)
##
## Female Male
## 114 486
loan |> ggplot(aes(Gender)) +
geom_bar()
Nhận xét: đồ thị cho thấy giới tính nam đi vay nhiều hơn giới tính nữ 62%
table(loan$Married)
##
## No Yes
## 210 390
loan |> ggplot(aes(Married)) +
geom_bar()
Nhận xét: đồ thị cho thấy những người đã kết hôn đi vay nhiều hơn độc thân 30%
table(loan$Education)
##
## Graduate Not Graduate
## 469 131
loan |> ggplot(aes(Education)) +
geom_bar()
Nhận xét: những người tốt nghiệp có quyết định vay nhiều hơn người chưa tốt nghiệp 56,3333367%
table(loan$SelfEmployed)
##
## No Yes
## 506 94
loan |> ggplot(aes(SelfEmployed)) +
geom_bar()
Nhận xét: những người không kinh doanh làm chủ quyết định vay nhiều hơn những người kinh doanh làm chủ 68,6666666%
table(loan$PropertyArea)
##
## Rural Semiurban Urban
## 173 230 197
loan |> ggplot(aes(PropertyArea)) +
geom_bar()
Nhận xét: người dân sống ở khu vực bán thành thị có tyt lệ vay cao hơn 2 khu vực khác
table(loan$LoanStatus)
##
## N Y
## 189 411
loan |> ggplot(aes(LoanStatus)) +
geom_bar()
Nhận xét: quyết định cho vay nhiều hơn quyết định không cho vay
table(loan$ApplicantIncome)
##
## 1 to 1000 1001 to 2000 2001 to 3000 3001 to 4000
## 5 35 133 153
## 4001 to 5000 5001 to 6000 greater than 6000
## 87 53 134
loan |> ggplot(aes(ApplicantIncome)) +
geom_bar()
Nhận xét: những người có mức thu nhập từ 3001 đến 4000 có tỷ lệ đi vay cao nhất và những người có mức thu nhập từ 1 đến 1000 là thấp nhất
table(loan$LoanAmount)
##
## 1 to 100 101 to 200 201 to 300 301 to 400 401 to 500 501 to 600 601 to 700
## 164 357 52 14 8 3 2
loan |> ggplot(aes(LoanAmount)) +
geom_bar()
Nhận xét: số tiền từ 101 đến 200 là mức cho vay nhiều người mong muốn
nhất và từ 601 đến 700 là mức thấp nhất
table(loan$Dependents)
##
## 1 2 3 4 5 6
## 342 101 104 33 15 5
loan |> ggplot(aes(Dependents)) +
geom_bar()
Nhận xét: số người phụ thuộc 1 chiếm tỷ lệ nhiều nhất
table(loan$LoanTerm)
##
## 12 36 48 60 84 120 180 240 300 360 480
## 1 2 1 2 4 12 43 4 13 503 15
loan |> ggplot(aes(LoanTerm)) +
geom_bar()
Nhận xét:
table(loan$CreditHistory)
##
## 0 1
## 107 493
loan |> ggplot(aes(CreditHistory)) +
geom_bar()
Nhận xét:
rủi ro tương đối là một tỷ lệ 2 xác suất: xác suất của quyết định cho vay và xác suất để người vay là nam hoặc nữ
loan1 <- table(loan$LoanStatus, loan$Gender)
addmargins(loan1)
##
## Female Male Sum
## N 36 153 189
## Y 78 333 411
## Sum 114 486 600
nhận xét: tỷ lệ quyết định không cho vay của nữ cao hơn tỷ lệ quyết định cho vay của nữ là 0.3663%
RelRisk(loan1)
## [1] 1.003663
loan2 <- table(loan$LoanStatus, loan$Married)
addmargins(loan2)
##
## No Yes Sum
## N 77 112 189
## Y 133 278 411
## Sum 210 390 600
nhận xét: tỷ lệ quyết định không cho vay của nữ chưa kết hôn cao hơn tỷ lệ quyết định không cho vay của nữ đã kết hôn 25.8981%
RelRisk(loan2)
## [1] 1.258981
loan1 <- table(loan$LoanStatus, loan$Gender)
loan1
##
## Female Male
## N 36 153
## Y 78 333
OddsRatio(loan1)
## [1] 1.004525
nhận xét:
loan2 <- table(loan$LoanStatus, loan$Married)
loan2
##
## No Yes
## N 77 112
## Y 133 278
OddsRatio(loan2)
## [1] 1.43703
nhận xét: tỷ lệ chênh lệch so với tỷ lệ khách hàng chưa kết hôn không được quyết định cho vay cao hơn tỷ lệ khách hàng đã kết hôn được quyết định cho vay 43.703%
loan3 <- table(loan$Gender, loan$Married)
loan3
##
## No Yes
## Female 77 37
## Male 133 353
chisq.test(loan3)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: loan3
## X-squared = 63.766, df = 1, p-value = 1.401e-15
Nhận xét: kết quả kiểm định cho ta p-value < 1.401e-15 < 0.05, nên bác bỏ H0, nghĩa là giữa giới tính và tình trạng hôn nhân là có liên quan với nhau.
loan4 <- table(loan$Gender, loan$Education)
loan4
##
## Graduate Not Graduate
## Female 93 21
## Male 376 110
chisq.test(loan4)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: loan4
## X-squared = 0.72924, df = 1, p-value = 0.3931
Nhận xét: Kết quả p−value = 0.3931 nên chưa đủ chứng cứ/thông tin/cơ sở để nói rằng tình trạng học vấn độc lập với giới tính.
Ước lượng tỷ lệ người phụ thuộc nhiều hơn 3 người (UnitsSold) đồng thời kiểm định xem tỷ lệ (%) người phụ thuộc nhiều hơn 3 người có phải là 20% không (nghĩa là chúng ta kiểm định giả thuyết: H0:p=0.2 ).
loan5 <- loan[loan$Dependents > 3,]
prop.test(length(loan5$Dependents), length(loan5$Dependents), p = 0.2)
##
## 1-sample proportions test with continuity correction
##
## data: length(loan5$Dependents) out of length(loan5$Dependents), null probability 0.2
## X-squared = 207.03, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.2
## 95 percent confidence interval:
## 0.9158109 1.0000000
## sample estimates:
## p
## 1
loanm <- loan[loan$Gender == 'Male',]
loanf <- loan[loan$Gender == 'Female',]
loanm3 <- loanm[loanm$Dependents > 3,]
loanf3 <- loanf[loanf$Dependents > 3,]
a <- c(nrow(loanm), nrow(loanf))
b <- c(nrow(loanm3), nrow(loanf3))
prop.test(b,a)
##
## 2-sample test for equality of proportions with continuity correction
##
## data: b out of a
## X-squared = 2.8085, df = 1, p-value = 0.09376
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## 0.003483625 0.106327941
## sample estimates:
## prop 1 prop 2
## 0.09876543 0.04385965
các yếu tố ảnh hưởng đến việc phê duyệt hoặc từ chối các đơn xin vay
Biến phụ thuộc: LoanStatus
Biến độc lập: Gender, Dependents, Education, SelfEmployed, ApplicantIncome, LoanAmount, LoanTerm, CreditHistory, PropertyArea
loan <- read_excel("C:/Users/sylev/Documents/PTDL.xlsx")
str(loan)
## tibble [600 × 11] (S3: tbl_df/tbl/data.frame)
## $ Gender : chr [1:600] "Male" "Male" "Male" "Male" ...
## $ Married : chr [1:600] "No" "Yes" "Yes" "Yes" ...
## $ Dependents : num [1:600] 1 2 1 1 1 3 1 4 3 2 ...
## $ Education : chr [1:600] "Graduate" "Graduate" "Graduate" "Not Graduate" ...
## $ SelfEmployed : chr [1:600] "No" "No" "Yes" "No" ...
## $ ApplicantIncome: chr [1:600] "5001 to 6000" "4001 to 5000" "2001 to 3000" "2001 to 3000" ...
## $ LoanAmount : chr [1:600] "1 to 100" "101 to 200" "1 to 100" "101 to 200" ...
## $ LoanTerm : num [1:600] 360 360 360 360 360 360 360 360 360 360 ...
## $ CreditHistory : num [1:600] 1 1 1 1 1 1 1 0 1 1 ...
## $ PropertyArea : chr [1:600] "Urban" "Rural" "Urban" "Urban" ...
## $ LoanStatus : chr [1:600] "Y" "N" "Y" "Y" ...
Bộ dữ liệu Phê duyệt Số tiền Khoản vay chứa thông tin về các đơn xin vay và liệu chúng có được phê duyệt hay không. Nó cung cấp cái nhìn sâu sắc về các yếu tố ảnh hưởng đến việc phê duyệt hoặc từ chối các đơn xin vay. Bộ dữ liệu với 600 quan sát với 11 biến (trong đó có 8 biến định tính và 3 biến định lượng):
Gender: giới tính
Married: tính trạng hôn nhân
Dependents: số người phụ thuộc
Education: tình trạng học vấn
SelfEmployed: tự làm chủ
Applicantlncome: thu nhập người vay
LoanAmount: số tiền vay
LoanTerm: thời hạn cho vay
CreditHistory: lịch sử tín dụng
PropertyArea: khu vực sinh sống
LoanStatus: quyết định khoản vay
Dữ liệu lấy từ https://www.kaggle.com/datasets/ashishkumarjayswal/loanamount-approval?resource=download
datatable(loan)