Bộ Dữ Liệu Tiếp Thị Điện Thoại Của Ngân Hàng Bồ Đào Nha

library(readxl)
d<- read_excel("C:/Users/pc/Downloads/dldt.xlsx")
## New names:
## • `` -> `...1`
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
library(epitools)
library(DescTools)
## Warning: package 'DescTools' was built under R version 4.3.1
library(DT)
## Warning: package 'DT' was built under R version 4.3.1
library(energy)
## Warning: package 'energy' was built under R version 4.3.1
options(digits = 4)

5 Bài tập tuần 6

Hồi quy

Ước lượng hàm hồi qui cho dữ liệu nhị phân

NP <- glm(data = d, formula = factor(loan) ~ education + marital + balance + job + housing, family = binomial(link = 'logit'))
summary(NP)
## 
## Call:
## glm(formula = factor(loan) ~ education + marital + balance + 
##     job + housing, family = binomial(link = "logit"), data = d)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)   
## (Intercept)        -1.71e+00   6.42e-01   -2.66   0.0078 **
## educationsecondary -1.21e-01   3.67e-01   -0.33   0.7418   
## educationtertiary  -4.55e-01   5.52e-01   -0.82   0.4099   
## maritalmarried     -2.37e-02   3.51e-01   -0.07   0.9463   
## maritalsingle      -1.29e-01   4.30e-01   -0.30   0.7640   
## balance            -1.24e-04   1.55e-04   -0.80   0.4263   
## jobblue-collar      1.02e-02   4.43e-01    0.02   0.9817   
## jobentrepreneur     9.63e-01   6.88e-01    1.40   0.1613   
## jobhousemaid       -1.58e+01   1.61e+03   -0.01   0.9922   
## jobmanagement       4.26e-01   5.61e-01    0.76   0.4482   
## jobretired          3.45e-01   6.15e-01    0.56   0.5743   
## jobself-employed    4.62e-01   8.77e-01    0.53   0.5987   
## jobservices         3.89e-01   4.70e-01    0.83   0.4078   
## jobstudent         -1.55e+01   1.61e+03   -0.01   0.9923   
## jobtechnician       2.41e-01   4.66e-01    0.52   0.6050   
## jobunemployed      -1.56e+01   1.14e+03   -0.01   0.9891   
## housingyes         -7.97e-03   3.77e-01   -0.02   0.9831   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 436.98  on 533  degrees of freedom
## Residual deviance: 425.22  on 517  degrees of freedom
## AIC: 459.2
## 
## Number of Fisher Scoring iterations: 16
BrierScore (NP)
## [1] 0.12

Ước lượng hàm hồi qui nhị phân log

LG <- glm(data = d, formula = factor(loan) ~ education + marital + balance + job + housing, family = binomial(link = 'log'))
summary(LG)
## 
## Call:
## glm(formula = factor(loan) ~ education + marital + balance + 
##     job + housing, family = binomial(link = "log"), data = d)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        -1.92e+00   5.45e-01   -3.52  0.00044 ***
## educationsecondary -8.73e-02   3.08e-01   -0.28  0.77658    
## educationtertiary  -3.61e-01   4.65e-01   -0.78  0.43817    
## maritalmarried     -4.04e-03   2.93e-01   -0.01  0.98898    
## maritalsingle      -1.05e-01   3.65e-01   -0.29  0.77368    
## balance            -1.19e-04   1.41e-04   -0.84  0.40085    
## jobblue-collar      2.75e-03   3.84e-01    0.01  0.99429    
## jobentrepreneur     7.70e-01   5.29e-01    1.46  0.14549    
## jobhousemaid       -1.54e+01   1.48e+03   -0.01  0.99166    
## jobmanagement       3.54e-01   4.79e-01    0.74  0.45916    
## jobretired          2.93e-01   5.17e-01    0.57  0.57073    
## jobself-employed    3.93e-01   7.26e-01    0.54  0.58797    
## jobservices         3.34e-01   3.98e-01    0.84  0.40170    
## jobstudent         -1.52e+01   1.47e+03   -0.01  0.99177    
## jobtechnician       2.02e-01   4.01e-01    0.50  0.61461    
## jobunemployed      -1.53e+01   1.04e+03   -0.01  0.98827    
## housingyes          1.79e-02   3.19e-01    0.06  0.95521    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 436.98  on 533  degrees of freedom
## Residual deviance: 425.18  on 517  degrees of freedom
## AIC: 459.2
## 
## Number of Fisher Scoring iterations: 16
BrierScore (LG)
## [1] 0.12

Ước lương mô hình cloglog

CG <- glm(data = d, formula = factor(loan) ~ education + marital + balance + job + housing, family = binomial(link = "cloglog"))
levels(factor(d$loan))
## [1] "no"  "yes"
summary(CG)
## 
## Call:
## glm(formula = factor(loan) ~ education + marital + balance + 
##     job + housing, family = binomial(link = "cloglog"), data = d)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)   
## (Intercept)        -1.81e+00   5.92e-01   -3.06   0.0022 **
## educationsecondary -1.04e-01   3.37e-01   -0.31   0.7582   
## educationtertiary  -4.06e-01   5.08e-01   -0.80   0.4243   
## maritalmarried     -1.36e-02   3.21e-01   -0.04   0.9661   
## maritalsingle      -1.16e-01   3.97e-01   -0.29   0.7693   
## balance            -1.21e-04   1.48e-04   -0.82   0.4139   
## jobblue-collar      6.60e-03   4.13e-01    0.02   0.9872   
## jobentrepreneur     8.62e-01   6.04e-01    1.43   0.1538   
## jobhousemaid       -1.56e+01   1.54e+03   -0.01   0.9919   
## jobmanagement       3.88e-01   5.19e-01    0.75   0.4545   
## jobretired          3.19e-01   5.64e-01    0.56   0.5722   
## jobself-employed    4.26e-01   8.00e-01    0.53   0.5944   
## jobservices         3.61e-01   4.33e-01    0.83   0.4048   
## jobstudent         -1.53e+01   1.54e+03   -0.01   0.9921   
## jobtechnician       2.21e-01   4.33e-01    0.51   0.6099   
## jobunemployed      -1.54e+01   1.08e+03   -0.01   0.9887   
## housingyes          5.17e-03   3.47e-01    0.01   0.9881   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 436.98  on 533  degrees of freedom
## Residual deviance: 425.20  on 517  degrees of freedom
## AIC: 459.2
## 
## Number of Fisher Scoring iterations: 16

Đánh giá mô hình

Tiêu chí AIC

aic1 <- AIC(NP)
aic2 <- AIC(LG)
aic3 <- AIC(CG)
AIC <-cbind(aic1,aic2,aic3)
AIC
##       aic1  aic2  aic3
## [1,] 459.2 459.2 459.2

Tiêu chí Deviance

de1 <- deviance(NP)
de2 <- deviance(LG)
de3 <- deviance(CG)
deviance <- cbind(de1,de2,de3)
deviance
##        de1   de2   de3
## [1,] 425.2 425.2 425.2

Tiêu chí Brier Score

bs1 <- BrierScore(NP)
bs2 <- BrierScore(LG)
bs3 <- BrierScore(CG)
BrierScore <- cbind(bs1,bs2,bs3)
BrierScore
##       bs1  bs2  bs3
## [1,] 0.12 0.12 0.12

-> Giá trị của 3 tiêu chí trên càng nhỏ nghĩa là mô hình càng tốt.Vì vậy ta lựa chọn mô hình hồi quy nhị phân.

summary(NP)
## 
## Call:
## glm(formula = factor(loan) ~ education + marital + balance + 
##     job + housing, family = binomial(link = "logit"), data = d)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)   
## (Intercept)        -1.71e+00   6.42e-01   -2.66   0.0078 **
## educationsecondary -1.21e-01   3.67e-01   -0.33   0.7418   
## educationtertiary  -4.55e-01   5.52e-01   -0.82   0.4099   
## maritalmarried     -2.37e-02   3.51e-01   -0.07   0.9463   
## maritalsingle      -1.29e-01   4.30e-01   -0.30   0.7640   
## balance            -1.24e-04   1.55e-04   -0.80   0.4263   
## jobblue-collar      1.02e-02   4.43e-01    0.02   0.9817   
## jobentrepreneur     9.63e-01   6.88e-01    1.40   0.1613   
## jobhousemaid       -1.58e+01   1.61e+03   -0.01   0.9922   
## jobmanagement       4.26e-01   5.61e-01    0.76   0.4482   
## jobretired          3.45e-01   6.15e-01    0.56   0.5743   
## jobself-employed    4.62e-01   8.77e-01    0.53   0.5987   
## jobservices         3.89e-01   4.70e-01    0.83   0.4078   
## jobstudent         -1.55e+01   1.61e+03   -0.01   0.9923   
## jobtechnician       2.41e-01   4.66e-01    0.52   0.6050   
## jobunemployed      -1.56e+01   1.14e+03   -0.01   0.9891   
## housingyes         -7.97e-03   3.77e-01   -0.02   0.9831   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 436.98  on 533  degrees of freedom
## Residual deviance: 425.22  on 517  degrees of freedom
## AIC: 459.2
## 
## Number of Fisher Scoring iterations: 16

4 Bài tập tuần 5

Kiểm định tính độc lập

Kiểm định tính độc lập cho 2 biến định tính là số dư tài khoản và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

s<-table(d$balance,d$loan)
chisq.test(s)
## Warning in chisq.test(s): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  s
## X-squared = 356, df = 372, p-value = 0.7

Kết quả cho thấy, p-value=0.7 > 0.05 -> Ta chấp nhận H0 Vậy biến số dư tài khoản và biến khả năng vay là độc lập với nhau.

Kiểm định tính độc lập cho 2 biến định tính là tình trạng học vấn và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

u<- table(d$education,d$loan)
chisq.test(u)
## 
##  Pearson's Chi-squared test
## 
## data:  u
## X-squared = 0.32, df = 2, p-value = 0.9

Kết quả cho thấy, p-value=0.8 > 0.05 -> Ta chấp nhận H0 Vậy biến tình trạng học vấn và biến khả năng vay là độc lập với nhau.

Phân tích khách hàng có khoản vay theo tình trạng hôn nhân

Đồ thị

q<-table(d$marital,d$loan)
ggplot(d, aes(loan, fill = marital)) + geom_bar(position = 'dodge')

Bảng tần số

addmargins(q)
##           
##             no yes Sum
##   divorced  69  13  82
##   married  290  49 339
##   single    99  14 113
##   Sum      458  76 534
  • Trong 82 người đã ly hôn có 69 người không có khoản vay và 13 người có khoản vay.
  • Trong 339 người đã kết hôn có 290 người không có khoản vay và 49 người có khoản vay.
  • Trong 113 người chưa kết hôn có 99 người không có khoản vay và 14 người có khoản vay.

Tính rủi ro tương đối

riskratio(q)
## $data
##           
##             no yes Total
##   divorced  69  13    82
##   married  290  49   339
##   single    99  14   113
##   Total    458  76   534
## 
## $measure
##           risk ratio with 95% C.I.
##            estimate  lower upper
##   divorced   1.0000     NA    NA
##   married    0.9117 0.5198 1.599
##   single     0.7815 0.3883 1.573
## 
## $p.value
##           two-sided
##            midp.exact fisher.exact chi.square
##   divorced         NA           NA         NA
##   married      0.7358       0.7303     0.7483
##   single       0.4966       0.5325     0.4893
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"
  • Tỷ lệ những người có khoản vay trong tình trạng đã kết hôn bằng 0.92 lần so với tỷ lệ những người có khoản vay trong tình trạng đã ly hôn.

  • Tỷ lệ những người có khoản vay trong tình trạng chưa kết hôn bằng 0.78 lần so với tỷ lệ những người có khoản vay trong tình trạng đã ly hôn.

riskratio(q, rev = 'c')
## $data
##           
##            yes  no Total
##   divorced  13  69    82
##   married   49 290   339
##   single    14  99   113
##   Total     76 458   534
## 
## $measure
##           risk ratio with 95% C.I.
##            estimate  lower upper
##   divorced    1.000     NA    NA
##   married     1.017 0.9165 1.128
##   single      1.041 0.9264 1.170
## 
## $p.value
##           two-sided
##            midp.exact fisher.exact chi.square
##   divorced         NA           NA         NA
##   married      0.7358       0.7303     0.7483
##   single       0.4966       0.5325     0.4893
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

*Tỷ lệ những người không có khoản vay trong tình trạng đã kết hôn bằng 1.02 lần so với tỷ lệ những người không có khoản vay trong tình trạng đã ly hôn.

*Tỷ lệ những người không có khoản vay trong tình trạng chưa kết hôn bằng 1.04 lần so với tỷ lệ những người không có khoản vay trong tình trạng đã ly hôn.

Tỷ lệ chênh

oddsratio(q)
## $data
##           
##             no yes Total
##   divorced  69  13    82
##   married  290  49   339
##   single    99  14   113
##   Total    458  76   534
## 
## $measure
##           odds ratio with 95% C.I.
##            estimate  lower upper
##   divorced   1.0000     NA    NA
##   married    0.8904 0.4678 1.802
##   single     0.7512 0.3285 1.728
## 
## $p.value
##           two-sided
##            midp.exact fisher.exact chi.square
##   divorced         NA           NA         NA
##   married      0.7358       0.7303     0.7483
##   single       0.4966       0.5325     0.4893
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"
  • Tỷ lệ những người có và không có khoản vay trong tình trạng đã kết hôn bằng 0.89 lần tỷ lệ những người có và không có khoản vay trong tình trạng đã ly hôn.

  • Tỷ lệ những người có và không có khoản vay trong tình trạng chưa kết hôn bằng 0.75 lần tỷ lệ những người có và không có khoản vay trong tình trạng đã ly hôn.

oddsratio(q, rev = 'c')
## $data
##           
##            yes  no Total
##   divorced  13  69    82
##   married   49 290   339
##   single    14  99   113
##   Total     76 458   534
## 
## $measure
##           odds ratio with 95% C.I.
##            estimate  lower upper
##   divorced    1.000     NA    NA
##   married     1.123 0.5550 2.137
##   single      1.331 0.5787 3.044
## 
## $p.value
##           two-sided
##            midp.exact fisher.exact chi.square
##   divorced         NA           NA         NA
##   married      0.7358       0.7303     0.7483
##   single       0.4966       0.5325     0.4893
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"

Phân tích khách hàng có khoản vay theo trình độ học vấn

Đồ thị

u<-table(d$education,d$loan)
ggplot(d, aes(loan, fill = education)) + geom_bar(position = 'dodge')

### Bảng tần số

addmargins(u)
##            
##              no yes Sum
##   primary    75  13  88
##   secondary 292  50 342
##   tertiary   91  13 104
##   Sum       458  76 534
  • Trong 88 người có trình độ cấp 1 có 75 người không có khoản vay và 13 người có khoản vay.
  • Trong 342 người có trình độ cấp 2 có 292 người không có khoản vay và 50 người có khoản vay.
  • Trong 104 người có trình độ cấp 33 có 91 người không có khoản vay và 13 người có khoản vay.

Tính rủi ro tương đối

riskratio(u)
## $data
##            
##              no yes Total
##   primary    75  13    88
##   secondary 292  50   342
##   tertiary   91  13   104
##   Total     458  76   534
## 
## $measure
##            risk ratio with 95% C.I.
##             estimate  lower upper
##   primary     1.0000     NA    NA
##   secondary   0.9897 0.5634 1.738
##   tertiary    0.8462 0.4142 1.729
## 
## $p.value
##            two-sided
##             midp.exact fisher.exact chi.square
##   primary           NA           NA         NA
##   secondary     0.9534       1.0000     0.9712
##   tertiary      0.6520       0.6768     0.6465
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

*Tỷ lệ những người có khoản vay khi có trình độ cấp 2 bằng 0.99 lần so với tỷ lệ những người có khoản vay khi có trình độ cấp 1.

*Tỷ lệ những người có khoản vay khi có trình độ cấp 2 bằng 0.84 lần so với tỷ lệ những người có khoản vay khi có trình độ cấp 3.

riskratio(u, rev = 'c')
## $data
##            
##             yes  no Total
##   primary    13  75    88
##   secondary  50 292   342
##   tertiary   13  91   104
##   Total      76 458   534
## 
## $measure
##            risk ratio with 95% C.I.
##             estimate  lower upper
##   primary      1.000     NA    NA
##   secondary    1.002 0.9088 1.104
##   tertiary     1.027 0.9167 1.150
## 
## $p.value
##            two-sided
##             midp.exact fisher.exact chi.square
##   primary           NA           NA         NA
##   secondary     0.9534       1.0000     0.9712
##   tertiary      0.6520       0.6768     0.6465
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

*Tỷ lệ những người không có khoản vay khi có trình độ cấp 2 bằng 1.002 lần so với tỷ lệ những người không có khoản vay khi có trình độ cấp 1.

*Tỷ lệ những người không có khoản vay khi có trình độ cấp 3 bằng 1.03 lần so với tỷ lệ những người không có khoản vay khi có trình độ cấp 1.

Tỷ lệ chênh

oddsratio(u)
## $data
##            
##              no yes Total
##   primary    75  13    88
##   secondary 292  50   342
##   tertiary   91  13   104
##   Total     458  76   534
## 
## $measure
##            odds ratio with 95% C.I.
##             estimate lower upper
##   primary     1.0000    NA    NA
##   secondary   0.9804 0.518 1.976
##   tertiary    0.8251 0.355 1.917
## 
## $p.value
##            two-sided
##             midp.exact fisher.exact chi.square
##   primary           NA           NA         NA
##   secondary     0.9534       1.0000     0.9712
##   tertiary      0.6520       0.6768     0.6465
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"
  • Tỷ lệ những người có và không có khoản vay khi có trình độ cấp 2 bằng 0.95 lần tỷ lệ những người có và không có khoản vay khi có trình độ cấp 1.

  • Tỷ lệ những người có và không có khoản vay khi có trình độ cấp 3 bằng 0.65 lần tỷ lệ những người có và không có khoản vay khi có trình độ cấp 1.

Phân tích khách hàng có khoản vay theo sở hữu nhà

Đồ thị

i<-table(d$housing,d$loan)
ggplot(d, aes(loan, fill = housing)) + geom_bar(position = 'dodge')

Bảng tần số

addmargins(i)
##      
##        no yes Sum
##   no   59  10  69
##   yes 399  66 465
##   Sum 458  76 534

Tính rủi ro tương đối

riskratio(i)
## $data
##        
##          no yes Total
##   no     59  10    69
##   yes   399  66   465
##   Total 458  76   534
## 
## $measure
##      risk ratio with 95% C.I.
##       estimate  lower upper
##   no    1.0000     NA    NA
##   yes   0.9794 0.5294 1.812
## 
## $p.value
##      two-sided
##       midp.exact fisher.exact chi.square
##   no          NA           NA         NA
##   yes     0.9231            1     0.9471
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"
riskratio(i, rev = 'c')
## $data
##        
##         yes  no Total
##   no     10  59    69
##   yes    66 399   465
##   Total  76 458   534
## 
## $measure
##      risk ratio with 95% C.I.
##       estimate  lower upper
##   no     1.000     NA    NA
##   yes    1.003 0.9044 1.113
## 
## $p.value
##      two-sided
##       midp.exact fisher.exact chi.square
##   no          NA           NA         NA
##   yes     0.9231            1     0.9471
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

Tỷ lệ chênh

oddsratio(i)
## $data
##        
##          no yes Total
##   no     59  10    69
##   yes   399  66   465
##   Total 458  76   534
## 
## $measure
##      odds ratio with 95% C.I.
##       estimate  lower upper
##   no    1.0000     NA    NA
##   yes   0.9649 0.4871 2.101
## 
## $p.value
##      two-sided
##       midp.exact fisher.exact chi.square
##   no          NA           NA         NA
##   yes     0.9231            1     0.9471
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"
oddsratio(i, rev = 'c')
## $data
##        
##         yes  no Total
##   no     10  59    69
##   yes    66 399   465
##   Total  76 458   534
## 
## $measure
##      odds ratio with 95% C.I.
##       estimate  lower upper
##   no     1.000     NA    NA
##   yes    1.036 0.4759 2.053
## 
## $p.value
##      two-sided
##       midp.exact fisher.exact chi.square
##   no          NA           NA         NA
##   yes     0.9231            1     0.9471
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"

Phân tích khách hàng có khoản vay theo nghề nghiệp

Đồ thị

k<-table(d$job,d$loan)
ggplot(d, aes(loan, fill = job)) + geom_bar(position = 'dodge')

Bảng tần số

addmargins(k)
##                
##                  no yes Sum
##   admin.         70  10  80
##   blue-collar   128  20 148
##   entrepreneur   11   4  15
##   housemaid       6   0   6
##   management     66  11  77
##   retired        24   5  29
##   self-employed  10   2  12
##   services       56  12  68
##   student         6   0   6
##   technician     69  12  81
##   unemployed     12   0  12
##   Sum           458  76 534

Tính rủi ro tương đối

riskratio(k)
## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect
## $data
##                
##                  no yes Total
##   admin.         70  10    80
##   blue-collar   128  20   148
##   entrepreneur   11   4    15
##   housemaid       6   0     6
##   management     66  11    77
##   retired        24   5    29
##   self-employed  10   2    12
##   services       56  12    68
##   student         6   0     6
##   technician     69  12    81
##   unemployed     12   0    12
##   Total         458  76   534
## 
## $measure
##                risk ratio with 95% C.I.
##                 estimate  lower upper
##   admin.           1.000     NA    NA
##   blue-collar      1.081 0.5322 2.196
##   entrepreneur     2.133 0.7693 5.916
##   housemaid        0.000 0.0000   NaN
##   management       1.143 0.5150 2.536
##   retired          1.379 0.5146 3.697
##   self-employed    1.333 0.3315 5.362
##   services         1.412 0.6508 3.063
##   student          0.000 0.0000   NaN
##   technician       1.185 0.5431 2.586
##   unemployed       0.000 0.0000   NaN
## 
## $p.value
##                two-sided
##                 midp.exact fisher.exact chi.square
##   admin.                NA           NA         NA
##   blue-collar       0.8435       1.0000     0.8289
##   entrepreneur      0.1945       0.2260     0.1555
##   housemaid         0.4650       1.0000     0.3569
##   management        0.7491       0.8168     0.7425
##   retired           0.5325       0.5381     0.5254
##   self-employed     0.6753       0.6535     0.6894
##   services          0.3927       0.4878     0.3804
##   student           0.4650       1.0000     0.3569
##   technician        0.6779       0.8191     0.6690
##   unemployed        0.2283       0.3487     0.1945
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"
riskratio(k, rev = 'c')
## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect

## Warning in chisq.test(xx, correct = correction): Chi-squared approximation may
## be incorrect
## $data
##                
##                 yes  no Total
##   admin.         10  70    80
##   blue-collar    20 128   148
##   entrepreneur    4  11    15
##   housemaid       0   6     6
##   management     11  66    77
##   retired         5  24    29
##   self-employed   2  10    12
##   services       12  56    68
##   student         0   6     6
##   technician     12  69    81
##   unemployed      0  12    12
##   Total          76 458   534
## 
## $measure
##                risk ratio with 95% C.I.
##                 estimate  lower upper
##   admin.          1.0000     NA    NA
##   blue-collar     0.9884 0.8904 1.097
##   entrepreneur    0.8381 0.6109 1.150
##   housemaid       1.1429 1.0520 1.242
##   management      0.9796 0.8661 1.108
##   retired         0.9458 0.7856 1.139
##   self-employed   0.9524 0.7298 1.243
##   services        0.9412 0.8201 1.080
##   student         1.1429 1.0520 1.242
##   technician      0.9735 0.8609 1.101
##   unemployed      1.1429 1.0520 1.242
## 
## $p.value
##                two-sided
##                 midp.exact fisher.exact chi.square
##   admin.                NA           NA         NA
##   blue-collar       0.8435       1.0000     0.8289
##   entrepreneur      0.1945       0.2260     0.1555
##   housemaid         0.4650       1.0000     0.3569
##   management        0.7491       0.8168     0.7425
##   retired           0.5325       0.5381     0.5254
##   self-employed     0.6753       0.6535     0.6894
##   services          0.3927       0.4878     0.3804
##   student           0.4650       1.0000     0.3569
##   technician        0.6779       0.8191     0.6690
##   unemployed        0.2283       0.3487     0.1945
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

Kiểm định tính độc lập

Tình trạng hôn nhân và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

s<-table(d$marital,d$loan)
chisq.test(s)
## 
##  Pearson's Chi-squared test
## 
## data:  s
## X-squared = 0.5, df = 2, p-value = 0.8

Kết quả cho thấy, p-value=0.7 > 0.05 -> Ta chấp nhận H0 Vậy biến số dư tài khoản và biến khả năng vay là độc lập với nhau.

Tình trạng học vấn và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

u<- table(d$education,d$loan)
chisq.test(u)
## 
##  Pearson's Chi-squared test
## 
## data:  u
## X-squared = 0.32, df = 2, p-value = 0.9

Kết quả cho thấy, p-value=0.8 > 0.05 -> Ta chấp nhận H0

Vậy biến tình trạng học vấn và biến khả năng vay là độc lập với nhau.

Nghề nghiệp và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

y<- table(d$job,d$loan)
chisq.test(y)
## Warning in chisq.test(y): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  y
## X-squared = 7.1, df = 10, p-value = 0.7

Kết quả cho thấy, p-value=0.7 > 0.05 -> Ta chấp nhận H0

Vậy biến nghề nghiệp và biến khả năng vay là độc lập với nhau.

Nhà và khả năng vay.

H0: Hai biến độc lập với nhau H1: Không có cơ sở kết luận hai biến có liên quan nhau

i<- table(d$housing,d$loan)
chisq.test(i)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  i
## X-squared = 1.5e-29, df = 1, p-value = 1

Kết quả cho thấy, p-value > 0.05 -> Ta chấp nhận H0

Vậy biến nhà ở và biến khả năng vay là độc lập với nhau.

3 Bài Tập Tuần 3,4

3.1 Thống kê mô tả

Biến Default

Khách hàng có nợ tín dụng không?

table(d$default)
## 
##  no yes 
## 525   9
ggplot(data = d) + geom_bar(mapping = aes(x = default, fill= default))

Số liệu cho thấy có 525 khách hàng không nợ tín dụng và 9 khách hàng nợ tín dụng.

Biến Loan

Khách hàng có khoản vay ngân hàng không?

table(d$loan)
## 
##  no yes 
## 458  76
ggplot(data = d) + geom_bar(mapping = aes(x = loan, fill= loan))

Số liệu cho thấy có 458 khách hàng không có khoản vay và 76 khách hàng có khoản vay.

Biến Job

Nghề nghiệp của khách hàng

table(d$job)
## 
##        admin.   blue-collar  entrepreneur     housemaid    management 
##            80           148            15             6            77 
##       retired self-employed      services       student    technician 
##            29            12            68             6            81 
##    unemployed 
##            12
table(d$job)/sum(table(d$job))
## 
##        admin.   blue-collar  entrepreneur     housemaid    management 
##       0.14981       0.27715       0.02809       0.01124       0.14419 
##       retired self-employed      services       student    technician 
##       0.05431       0.02247       0.12734       0.01124       0.15169 
##    unemployed 
##       0.02247
ggplot(data = d) + geom_bar(mapping = aes(x = job, fill= job))

  • Có 80 người làm nghề admin.
  • Có 148 người làm nghề blue-collar.
  • Có 15 người làm nghề entrepreneur.
  • Có 6 người làm nghề housemaid.
  • Có 77 người làm nghề management.
  • Có 29 người đã nghỉ hưu.(retired)
  • Có 12 người tự chủ (self-employed).
  • Có 68 người làm nghề services.
  • Có 6 người là student.
  • Có 81 người làm nghề technician.
  • Có 12 người thất nghiệp.

Biến Marital

Tình trạng hôn nhân

table(d$marital)
## 
## divorced  married   single 
##       82      339      113
table(d$marital)/sum(table(d$marital))
## 
## divorced  married   single 
##   0.1536   0.6348   0.2116
ggplot(data = d) + geom_bar(mapping = aes(x = marital, fill= marital))

  • Có 113 người đang độc thân.
  • Có 339 người đã kết hôn.
  • Có 82 người đã ly hôn.

Biến Housing

Khách hàng có nhà hay không?

table(d$housing)
## 
##  no yes 
##  69 465
ggplot(data = d) + geom_bar(mapping = aes(x = housing, fill= housing))

  • Số người có nhà là 465 người.
  • Số người không có nhà là 69 người.

Biến Balance

summary(d$balance)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    -674      17     157     454     457    8730

Số dư trong tài khoản lớn nhất là 8730$

Số dư trong tài khoản nhỏ nhất là -674$

Biến Age

summary(d$age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    22.0    36.0    43.0    43.4    51.0    61.0
  • Trong 1000 khách hàng của ngân hàng:

Tuổi của khách hàng cao nhất là : 61 tuổi

Tuổi của khách hàng thấp nhất là 22 tuổi

Biến education

table(d$education)
## 
##   primary secondary  tertiary 
##        88       342       104
table(d$education)/sum(table(d$education))
## 
##   primary secondary  tertiary 
##    0.1648    0.6404    0.1948
ggplot(data = d) + geom_bar(mapping = aes(x = education, fill= education))

* Có 88 người có tình độ tiểu học. * Có 342 người có trình độ cấp 2. * Có 104 người có trình độ cao đẳng hoặc đại học.

2 Bài Tập Tuần 2

2.1 Chọn 2 Biến Phụ Thuộc

  • Biến phụ thuộc là biến số chịu ảnh hưởng từ một biế n số khác trong mô hình.

Ta chọn 1 biến định tính và 1 biến định lượng bao gồm:

  • Loan (Khách hàng có khoản vay không?)

  • Balance (Số dư tại ngân hàng)

Vì 2 biến này có thể sẽ chịu ảnh hưởng từ nghề nghiệp,học vấn, tuổi,….nên ta chọn 2 biến này là biến phụ thuộc của mô hình.

2.2 Phân Tích Biến

library(readxl)
d<- read_excel("C:/Users/pc/Downloads/dldt.xlsx")
## New names:
## • `` -> `...1`
table(d$loan)
## 
##  no yes 
## 458  76
  • Số liệu cho thấy có 458 khách hàng không có khoản vay và 76 khách hàng có khoản vay.
table(d$balance)
## 
## -674 -509 -457 -390 -372 -364 -358 -349 -333 -331 -325 -246 -244 -209 -185 -171 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## -144 -137 -125 -109 -106 -103 -100  -99  -76  -69  -67  -66  -62  -60  -41  -37 
##    1    1    1    1    1    1    1    1    2    1    1    2    1    1    1    1 
##  -34  -25  -16  -15  -12  -11  -10   -9   -7   -4   -3   -2    0    1    2    3 
##    1    1    1    1    1    1    1    1    1    1    1    1   60    6    2    3 
##    4    6    7    8    9   10   11   12   13   15   16   17   18   19   20   22 
##    1    1    2    2    2    1    1    1    1    1    2    4    1    3    2    2 
##   23   25   26   28   29   30   31   34   37   39   41   42   44   45   46   47 
##    2    4    1    1    1    2    1    3    2    2    3    2    2    1    4    2 
##   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   66 
##    1    2    1    2    1    1    1    2    1    2    2    1    3    1    1    2 
##   67   70   71   72   73   74   76   77   79   81   82   83   86   89   90   91 
##    1    1    3    1    1    1    1    1    1    2    3    2    2    3    1    1 
##   92   94   96   98   99  100  101  105  107  109  110  113  115  121  122  126 
##    1    1    1    2    1    1    1    1    1    1    1    2    1    2    1    1 
##  128  129  131  136  137  139  140  141  143  144  148  151  152  155  157  161 
##    1    1    2    1    1    2    2    2    2    1    1    2    1    2    2    1 
##  162  163  164  167  173  176  179  182  183  184  187  190  193  195  203  204 
##    1    1    1    1    2    2    2    1    2    1    1    2    2    2    1    1 
##  206  207  209  211  213  214  216  217  219  224  228  229  230  231  235  236 
##    3    1    1    3    1    1    2    2    2    1    1    2    3    2    1    1 
##  242  243  245  249  253  255  258  262  263  265  270  271  274  275  276  278 
##    1    2    1    2    1    1    1    1    2    2    2    1    1    1    1    1 
##  279  282  283  284  288  290  292  293  294  297  301  307  308  310  313  315 
##    1    1    1    1    1    2    1    1    1    1    2    1    1    2    1    2 
##  318  319  320  324  330  348  349  351  352  353  358  367  368  372  375  377 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    2 
##  378  383  384  387  388  390  393  398  400  406  408  423  424  428  434  436 
##    2    1    1    1    1    1    1    1    1    1    1    1    2    1    2    1 
##  445  447  448  450  459  469  471  473  475  477  483  484  486  490  505  506 
##    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1 
##  507  510  517  518  523  529  530  531  539  545  560  570  575  581  582  593 
##    1    1    3    1    1    1    1    1    1    1    2    1    1    1    1    1 
##  600  627  639  668  672  685  690  693  712  723  742  744  756  757  767  778 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  779  782  787  790  792  797  802  818  830  831  839  840  841  842  869  871 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  901  904  946  947  964  965  983  989 1011 1022 1068 1078 1127 1142 1151 1161 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1169 1205 1225 1234 1236 1263 1270 1291 1293 1297 1331 1335 1357 1378 1387 1428 
##    1    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 1467 1533 1602 1640 1660 1667 1680 1685 1716 1877 1927 1937 2102 2127 2143 2248 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 2298 2343 2476 2558 2573 2586 3165 3237 3877 4070 4080 4325 5090 5345 5431 5435 
##    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
## 5699 5935 6530 8486 8730 
##    1    1    1    1    1

*Số liệu cho thấy có 428 khách hàng có số dư trong tài khoản ngân hàng lớn hơn 0.

*Số liệu cho thấy có 106 khách hàng có số dư trong tài khoản ngân hàng nhỏ hơn 0.

1 Bài Tập Tuần 1

Bộ Dữ Liệu Tiếp Thị Điện Thoại Của Ngân Hàng Bồ Đào Nha

Dữ liệu bao gồm 534 quan sát và 9 biến. Trong 9 biến có 6 biến định tính và 3 biến định lượng.

*Age : Tuổi

*Job : Nghề nghiệp

*Marital: Tình trạng hôn nhân, giá trị 1 là đã kết hôn (married),giá trị 2 là độc thân(single),giá trị 3 là đã ly hôn(divorced)

*Education: Học vấn

*Default: Có nợ tín dụng không?, giá trị 1 là có (Yes),giá trị 2 là không (No)

*balance:Số dư tại ngân hàng

*Housing: Có nhà hay không?,giá trị 1 là có (Yes),giá trị 2 là không (No)

*Loan: Có Nợ Không? ,giá trị 1 là có (Yes),giá trị 2 là không (No)

*Duration: Thời gian đáo hạn

library(readxl)
data<- read_excel("C:/Users/pc/Downloads/dldt.xlsx")
## New names:
## • `` -> `...1`
attach(data)
str(data)
## tibble [534 × 18] (S3: tbl_df/tbl/data.frame)
##  $ ...1           : num [1:534] 1 2 3 4 5 6 7 8 9 10 ...
##  $ age            : num [1:534] 58 44 33 35 28 42 58 43 41 29 ...
##  $ job            : chr [1:534] "management" "technician" "entrepreneur" "management" ...
##  $ marital        : chr [1:534] "married" "single" "married" "married" ...
##  $ education      : chr [1:534] "tertiary" "secondary" "secondary" "tertiary" ...
##  $ default        : chr [1:534] "no" "no" "no" "no" ...
##  $ balance        : num [1:534] 2143 29 2 231 447 ...
##  $ housing        : chr [1:534] "yes" "yes" "yes" "yes" ...
##  $ loan           : chr [1:534] "no" "no" "yes" "no" ...
##  $ day            : num [1:534] 5 5 5 5 5 5 5 5 5 5 ...
##  $ month          : chr [1:534] "may" "may" "may" "may" ...
##  $ duration       : num [1:534] 4.35 2.52 1.27 2.32 3.62 6.33 0.83 0.92 3.7 2.28 ...
##  $ campaign       : num [1:534] 1 1 1 1 1 1 1 1 1 1 ...
##  $ pdays          : num [1:534] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ previous       : num [1:534] 0 0 0 0 0 0 0 0 0 0 ...
##  $ poutcome       : chr [1:534] "unknown" "unknown" "unknown" "unknown" ...
##  $ response       : chr [1:534] "no" "no" "no" "no" ...
##  $ response_binary: num [1:534] 0 0 0 0 0 0 0 0 0 0 ...
summary(data)
##       ...1          age           job              marital         
##  Min.   :  1   Min.   :22.0   Length:534         Length:534        
##  1st Qu.:134   1st Qu.:36.0   Class :character   Class :character  
##  Median :268   Median :43.0   Mode  :character   Mode  :character  
##  Mean   :268   Mean   :43.4                                        
##  3rd Qu.:401   3rd Qu.:51.0                                        
##  Max.   :534   Max.   :61.0                                        
##   education           default             balance       housing         
##  Length:534         Length:534         Min.   :-674   Length:534        
##  Class :character   Class :character   1st Qu.:  17   Class :character  
##  Mode  :character   Mode  :character   Median : 157   Mode  :character  
##                                        Mean   : 454                     
##                                        3rd Qu.: 457                     
##                                        Max.   :8730                     
##      loan                day          month              duration    
##  Length:534         Min.   :5.00   Length:534         Min.   : 0.17  
##  Class :character   1st Qu.:5.00   Class :character   1st Qu.: 2.27  
##  Mode  :character   Median :5.00   Mode  :character   Median : 3.29  
##                     Mean   :5.41                      Mean   : 4.50  
##                     3rd Qu.:6.00                      3rd Qu.: 5.21  
##                     Max.   :6.00                      Max.   :33.88  
##     campaign        pdays       previous   poutcome           response        
##  Min.   :1.00   Min.   :-1   Min.   :0   Length:534         Length:534        
##  1st Qu.:1.00   1st Qu.:-1   1st Qu.:0   Class :character   Class :character  
##  Median :1.00   Median :-1   Median :0   Mode  :character   Mode  :character  
##  Mean   :1.51   Mean   :-1   Mean   :0                                        
##  3rd Qu.:2.00   3rd Qu.:-1   3rd Qu.:0                                        
##  Max.   :5.00   Max.   :-1   Max.   :0                                        
##  response_binary 
##  Min.   :0.0000  
##  1st Qu.:0.0000  
##  Median :0.0000  
##  Mean   :0.0225  
##  3rd Qu.:0.0000  
##  Max.   :1.0000