L3_1

Logit Models

Context/ Introduction

Trong chủ đề này chúng ta phân tích 1 khía cạnh khác trong phân tích kinh tế, được ứng dụng rất nhiều trong các vấn đề các mô hình tín dụng, hành vi khách hàng và nghiên cứu hành vi lựa chọn của các đối tượng kinh tế khác nhau. Trong ví dụ này, chúng ta xem xét mô hình đánh giá các yếu tố ảnh hưởng đến lựa chọn mua bảo hiểm sức khỏe. Dựa trên các mô hình lý thuyết và nghiên cứu trước, các tác giả đã xác định (Giáo trình số 2) đã xác định 3 nhóm biến tác động chính: Trạng thái sức khỏe, các đặc trưng nhân khẩu học và thông tin về người ở chung (vợ/chồng)

Self-assessed health-status information is used to generate a dummy variable (hstatusg) that measures whether health status is good, very good, or excellent. Other measures of health status are the number of limitations (up to five) on activities of daily living (adl) and the total number of chronic conditions (chronic).

Socioeconomic variables used are age, gender, race, ethnicity, marital status, years of education, and retirement status (respectively, age, f emale, white, hisp, married, educyear, retire); household income (hhincome); and log household income if positive (line).

Spouse retirement status (sretire) is an indicator variable equal to 1 if a retired spouse is present.

Data

Data <-haven::read_dta("C:\\Users\\Huynh Chuong\\Desktop\\University\\UEL\\Class_QuantMethods\\Giáo trình\\Data\\mus\\mus14data.dta")
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.1
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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

Khám phá dữ liệu

Data %>%
  variable.names()
 [1] "personid"      "private"       "eprhi"         "age"          
 [5] "hisp"          "white"         "female"        "educyear"     
 [9] "married"       "excel"         "vegood"        "good"         
[13] "fair"          "poor"          "chronic"       "adl"          
[17] "retire"        "seprhi"        "sretire"       "hhincome"     
[21] "ins"           "age2"          "hstatusg"      "agefem"       
[25] "agechr"        "agewhi"        "_est_blogit"   "_est_bprobit" 
[29] "_est_bOLS"     "_est_bloghet"  "_est_bprobhet" "_est_bOLShet" 
[33] "_est_PROBIT"   "_est_IVPROBML" "_est_IVPROB2S"
Data %>% 
  mutate(linc=log(hhincome)) %>%
  select(ins, retire,age, hstatusg, hhincome, educyear, married, hisp,linc, female, white, chronic, adl, sretire) %>%
  head()
# A tibble: 6 × 14
    ins retire   age hstatusg hhincome educyear married  hisp  linc female white
  <dbl>  <dbl> <dbl>    <dbl>    <dbl>    <dbl>   <dbl> <dbl> <dbl>  <dbl> <dbl>
1     0      0    62        0        0       12       0     0  -Inf      1     0
2     0      0    59        0        0       12       0     0  -Inf      1     1
3     0      1    60        1        0       13       0     0  -Inf      0     0
4     0      0    62        0        0       10       0     0  -Inf      1     1
5     0      0    54        0        0        9       0     0  -Inf      1     1
6     0      1    62        1        0       12       1     0  -Inf      1     1
# ℹ 3 more variables: chronic <dbl>, adl <dbl>, sretire <dbl>
Data%>%
  select(ins) %>%
  table() %>%
  prop.table() %>%
  round(digits = 4)*100
ins
    0     1 
61.29 38.71 

Xem bảng tỷ lệ giữa sức khỏe và mua bảo hiểm

Data%>%
  select(ins, hstatusg) %>%
  table() %>%
  prop.table() %>% # tỷ lệ % trong toàn bộ quan sát
  round(digits = 4)*100
   hstatusg
ins     0     1
  0 21.21 40.08
  1  8.33 30.38

Xem tỷ lệ trạng thái sức khỏe và hành vi mua bảo hiểm (cố định theo hàng)

Data%>%
  select(ins, hstatusg) %>%
  table() %>%
  prop.table(1) %>% # tỷ lệ % theo hàng
  round(digits = 3)*100
   hstatusg
ins    0    1
  0 34.6 65.4
  1 21.5 78.5
Data%>%
  select(ins, retire) %>%
  table() %>%
  prop.table(1) %>%
  round(digits = 4)*100
   retire
ins     0     1
  0 40.61 59.39
  1 32.63 67.37
Data%>%
  select(ins, retire) %>%
  table() %>%
  prop.table(2) %>% # tỷ lệ % theo cột
  round(digits = 3)*100
   retire
ins    0    1
  0 66.3 58.3
  1 33.7 41.7

Bảng tần số 3 chiều

Data%>%
  select(ins, retire, female) %>%
  table() %>%
  prop.table(2) %>%
  round(digits = 3)*100
, , female = 0

   retire
ins    0    1
  0 22.9 34.4
  1 13.5 27.4

, , female = 1

   retire
ins    0    1
  0 43.5 23.9
  1 20.2 14.4

Thống kê định lượng theo từng nhóm định tính

Data %>%
  group_by(ins) %>%
  summarise(TuoiTB = mean(age), 
            ThunhapTB=mean(hhincome),
            HocvanTB=mean(educyear))
# A tibble: 2 × 4
    ins TuoiTB ThunhapTB HocvanTB
  <dbl>  <dbl>     <dbl>    <dbl>
1     0   66.8      37.7     11.3
2     1   67.1      57.3     12.9

Xem chỉ số theo từng nhóm giới tính và đặc điểm mua bảo hiểm

Data %>%
  group_by(female, ins) %>%
  summarise(TuoiTB = mean(age), 
            ThunhapTB=mean(hhincome),
            HocvanTB=mean(educyear))
`summarise()` has grouped output by 'female'. You can override using the
`.groups` argument.
# A tibble: 4 × 5
# Groups:   female [2]
  female   ins TuoiTB ThunhapTB HocvanTB
   <dbl> <dbl>  <dbl>     <dbl>    <dbl>
1      0     0   67.8      43.8     11.3
2      0     1   67.8      62.7     12.8
3      1     0   65.9      31.7     11.3
4      1     1   66.1      50.1     12.9
Data %>%
  group_by(female,married) %>%
  summarise(TuoiTB = mean(age), 
            ThunhapTB=mean(hhincome),
            HocvanTB=mean(educyear))
`summarise()` has grouped output by 'female'. You can override using the
`.groups` argument.
# A tibble: 4 × 5
# Groups:   female [2]
  female married TuoiTB ThunhapTB HocvanTB
   <dbl>   <dbl>  <dbl>     <dbl>    <dbl>
1      0       0   66.6      30.5     11.6
2      0       1   67.9      54.9     12.0
3      1       0   65.7      20.7     11.5
4      1       1   66.2      50.9     12.1

Model

Các biến đánh giá trạng thái sức khỏe: hstatusg: đánh giá chung về sức khỏe Ngoài ra các biến chỉ báo sức khỏe khác: adl, chronic

Các biến nhân khẩu học: age, female, white, hisp,married, educyear, retire, hhincome( linc), sretire

Mô hình 0: Mô hình với các biến đánh giá sức khỏe Mô hình 1: Thêm vào các biến kiểm soát về nhân khẩu học

*** Lưu ý: Với các biến định tính (định danh): khi đưa vào mô hình ta cần phải làm gì? Về nguyên tắc ta phải chuyển thành biến dummy Cần phải khai báo biến định tính trong R. as.factor(tên biến)

Data %>%
  select(hstatusg) %>%
  table()
hstatusg
   0    1 
 947 2259 

Cách 1: Khai báo ngay trong dữ liệu

Data %>%
  mutate(hstatusg=as.factor(hstatusg),
         retire=as.factor(retire)) -> Data

Cách 2: Khai báo ngay tại mô hình

logit0<- glm(ins~ as.factor(hstatusg)+adl+chronic ,  family=binomial(link="logit"), data = Data )
summary(logit0)

Call:
glm(formula = ins ~ as.factor(hstatusg) + adl + chronic, family = binomial(link = "logit"), 
    data = Data)

Coefficients:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)          -0.85207    0.11487  -7.418 1.19e-13 ***
as.factor(hstatusg)1  0.53613    0.09593   5.589 2.28e-08 ***
adl                  -0.26590    0.05917  -4.494 7.00e-06 ***
chronic               0.03642    0.02919   1.248    0.212    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 4279.5  on 3205  degrees of freedom
Residual deviance: 4192.3  on 3202  degrees of freedom
AIC: 4200.3

Number of Fisher Scoring iterations: 4

Ước lượng mô hình.

logit0<- glm(ins~hstatusg+adl+chronic ,  family=binomial(link="logit"), data = Data )
summary(logit0)

Call:
glm(formula = ins ~ hstatusg + adl + chronic, family = binomial(link = "logit"), 
    data = Data)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -0.85207    0.11487  -7.418 1.19e-13 ***
hstatusg1    0.53613    0.09593   5.589 2.28e-08 ***
adl         -0.26590    0.05917  -4.494 7.00e-06 ***
chronic      0.03642    0.02919   1.248    0.212    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 4279.5  on 3205  degrees of freedom
Residual deviance: 4192.3  on 3202  degrees of freedom
AIC: 4200.3

Number of Fisher Scoring iterations: 4
probit0<- glm(ins~hstatusg+adl+chronic ,  family=binomial(link="probit"), data = Data )
summary(probit0)

Call:
glm(formula = ins ~ hstatusg + adl + chronic, family = binomial(link = "probit"), 
    data = Data)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -0.52657    0.07000  -7.522 5.38e-14 ***
hstatusg1    0.33019    0.05845   5.650 1.61e-08 ***
adl         -0.15333    0.03385  -4.529 5.91e-06 ***
chronic      0.02139    0.01793   1.193    0.233    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 4279.5  on 3205  degrees of freedom
Residual deviance: 4192.9  on 3202  degrees of freedom
AIC: 4200.9

Number of Fisher Scoring iterations: 4
logit<- glm(ins~ hstatusg+adl+chronic + age+ hhincome+ educyear+ married+ hisp,  family=binomial(link="logit"), data = Data )
summary(logit)

Call:
glm(formula = ins ~ hstatusg + adl + chronic + age + hhincome + 
    educyear + married + hisp, family = binomial(link = "logit"), 
    data = Data)

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -2.0467858  0.7424471  -2.757 0.005837 ** 
hstatusg1    0.2601605  0.1025363   2.537 0.011173 *  
adl         -0.2126602  0.0607973  -3.498 0.000469 ***
chronic      0.0534146  0.0303700   1.759 0.078612 .  
age         -0.0087753  0.0108950  -0.805 0.420564    
hhincome     0.0020309  0.0007443   2.729 0.006359 ** 
educyear     0.1187003  0.0142057   8.356  < 2e-16 ***
married      0.5882746  0.0931003   6.319 2.64e-10 ***
hisp        -0.7777764  0.1967244  -3.954 7.70e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 4279.5  on 3205  degrees of freedom
Residual deviance: 3980.4  on 3197  degrees of freedom
AIC: 3998.4

Number of Fisher Scoring iterations: 4
probit<- glm(ins~hstatusg+adl+chronic + age+ hhincome+ educyear+ married+ hisp,  family=binomial(link="probit"), data = Data )
summary(probit)

Call:
glm(formula = ins ~ hstatusg + adl + chronic + age + hhincome + 
    educyear + married + hisp, family = binomial(link = "probit"), 
    data = Data)

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.280830   0.450674  -2.842 0.004483 ** 
hstatusg1    0.163869   0.062173   2.636 0.008396 ** 
adl         -0.125010   0.035113  -3.560 0.000371 ***
chronic      0.031673   0.018487   1.713 0.086673 .  
age         -0.005140   0.006612  -0.777 0.436898    
hhincome     0.001113   0.000429   2.594 0.009480 ** 
educyear     0.073273   0.008488   8.632  < 2e-16 ***
married      0.367515   0.055939   6.570 5.03e-11 ***
hisp        -0.454956   0.111016  -4.098 4.17e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 4279.5  on 3205  degrees of freedom
Residual deviance: 3977.8  on 3197  degrees of freedom
AIC: 3995.8

Number of Fisher Scoring iterations: 6

So sánh 3 mô hình

Models <- list(logit0, logit, probit0, probit)
modelsummary::modelsummary(Models,
                           statistic = c('{statistic} '),
                           stars = c("*"=0.1, "**"=0.05, "***"=0.01))
tinytable_vxt0hs06ohleihudfo89
(1) (2) (3) (4)
* p < 0.1, ** p < 0.05, *** p < 0.01
(Intercept) -0.852*** -2.047*** -0.527*** -1.281***
-7.418 -2.757 -7.522 -2.842
hstatusg1 0.536*** 0.260** 0.330*** 0.164***
5.589 2.537 5.650 2.636
adl -0.266*** -0.213*** -0.153*** -0.125***
-4.494 -3.498 -4.529 -3.560
chronic 0.036 0.053* 0.021 0.032*
1.248 1.759 1.193 1.713
age -0.009 -0.005
-0.805 -0.777
hhincome 0.002*** 0.001***
2.729 2.594
educyear 0.119*** 0.073***
8.356 8.632
married 0.588*** 0.368***
6.319 6.570
hisp -0.778*** -0.455***
-3.954 -4.098
Num.Obs. 3206 3206 3206 3206
AIC 4200.3 3998.4 4200.9 3995.8
BIC 4224.6 4053.1 4225.2 4050.4
Log.Lik. -2096.146 -1990.201 -2096.444 -1988.884
F 25.799 30.305 27.067 33.375
RMSE 0.48 0.47 0.48 0.47

Assessing model fit for a logistic regression

Comparing the crude model (logit0) to the adjusted model (logit)

  • Kiểm định này không quan trọng vì mô hình lý thuyết được xác định từ đầu.
lmtest:: lrtest(logit0, logit)
Likelihood ratio test

Model 1: ins ~ hstatusg + adl + chronic
Model 2: ins ~ hstatusg + adl + chronic + age + hhincome + educyear + 
    married + hisp
  #Df  LogLik Df  Chisq Pr(>Chisq)    
1   4 -2096.2                         
2   9 -1990.2  5 211.89  < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The Hosmer-Lemeshow GOF test

Đây là kiểm định quan trọng của mô hình. Nếu kiểm định này không có ý nghĩa thông kê ==> tốt

Nếu có ý nghĩa thống kê thì mô hình chưa tốt (cần xem xét lại: đặc trưng dữ liệu, các biến trong mô hình, mô hình lý thuyết)

performance::performance_hosmer(logit0, n_bins=2)
# Hosmer-Lemeshow Goodness-of-Fit Test

  Chi-squared: 2.364
           df: 0    
      p-value: 0.000
Summary: model does not fit well.
performance::performance_hosmer(logit, n_bins=10)
# Hosmer-Lemeshow Goodness-of-Fit Test

  Chi-squared: 22.591
           df:  8    
      p-value:  0.004
Summary: model does not fit well.
performance::performance_hosmer(probit, n_bins=10)
# Hosmer-Lemeshow Goodness-of-Fit Test

  Chi-squared: 18.129
           df:  8    
      p-value:  0.020
Summary: model does not fit well.
logit.res <- rms::lrm(logit,  data = Data, y = TRUE, x = TRUE)
residuals(logit.res, type = "gof")
Sum of squared errors     Expected value|H0                    SD 
         6.968213e+02          6.944541e+02          8.521374e-01 
                    Z                     P 
         2.778008e+00          5.469335e-03 

OR

Chỉ số ODDsRatio: Chỉ số này các em đọc trong giáo trình. Hãy giải thích ý nghĩa của từng biến tác động tới biến phụ thuộc theo chỉ số OR

cbind(Estimate=round(coef(logit),4),
 OR=round(exp(coef(logit)),4))
            Estimate     OR
(Intercept)  -2.0468 0.1291
hstatusg1     0.2602 1.2971
adl          -0.2127 0.8084
chronic       0.0534 1.0549
age          -0.0088 0.9913
hhincome      0.0020 1.0020
educyear      0.1187 1.1260
married       0.5883 1.8009
hisp         -0.7778 0.4594

Chỉ số OR giải thích như thế nào?

exp(cbind(OR = coef(logit), confint(logit)))
Waiting for profiling to be done...
                   OR      2.5 %    97.5 %
(Intercept) 0.1291494 0.03008966 0.5533042
hstatusg1   1.2971383 1.06153569 1.5869199
adl         0.8084308 0.71535935 0.9082222
chronic     1.0548670 0.99389527 1.1195957
age         0.9912631 0.97027386 1.0126310
hhincome    1.0020330 1.00065171 1.0035725
educyear    1.1260324 1.09530962 1.1580509
married     1.8008784 1.50197594 2.1637878
hisp        0.4594264 0.30815159 0.6678051

Hypothesis and specification tests

Khi so sánh các mô hình cần xem xét các chỉ số AIC, BIC, RMSE và khả năng ước tính của mô hình, đặc biệt khi xem xét lựa chọn tiếp cận logit hay probit.

Theo đó, AIC, BIC, RMSE càng thấp càng tốt, khả năng dự báo đúng càng cao càng tốt. Goodness of fit and prediction

invlogit = function (x) {1/(1+exp(-x))}
invlogit(coef(logit)[1]+
 coef(logit)[2]*mean(Data$retire)+
 coef(logit)[3]*mean(Data$age)+
 coef(logit)[4]*mean(Data$hstatusg)+
   coef(logit)[4]*mean(Data$hhincome)+
   coef(logit)[4]*mean(Data$educyear)+
   coef(logit)[4]*mean(Data$married)+
   coef(logit)[4]*mean(Data$hisp))
Warning in mean.default(Data$retire): argument is not numeric or logical:
returning NA
Warning in mean.default(Data$hstatusg): argument is not numeric or logical:
returning NA
(Intercept) 
         NA 
models <- list(logit,probit)
modelsummary::modelsummary(models,
                           statistic = c('{statistic} '),
                           stars = c("*"=0.1, "**"=0.05, "***"=0.01))
tinytable_e807c080l0wbhjc1z3n3
(1) (2)
* p < 0.1, ** p < 0.05, *** p < 0.01
(Intercept) -2.047*** -1.281***
-2.757 -2.842
hstatusg1 0.260** 0.164***
2.537 2.636
adl -0.213*** -0.125***
-3.498 -3.560
chronic 0.053* 0.032*
1.759 1.713
age -0.009 -0.005
-0.805 -0.777
hhincome 0.002*** 0.001***
2.729 2.594
educyear 0.119*** 0.073***
8.356 8.632
married 0.588*** 0.368***
6.319 6.570
hisp -0.778*** -0.455***
-3.954 -4.098
Num.Obs. 3206 3206
AIC 3998.4 3995.8
BIC 4053.1 4050.4
Log.Lik. -1990.201 -1988.884
F 30.305 33.375
RMSE 0.47 0.47

Marginal effects

Đánh giá ảnh hưởng biên: Đây là phần phân tích quan trọng trong mô hình logit, các em cần đọc trong giáo trình để giải thích các con số trong đánh giá ảnh hưởng biên tế.

Theo bảng ảnh hưởng biên dưới đây, Khi tuổi tăng 1 tuổi thì ……….

mfx::logitmfx(ins~retire + age+ hstatusg+ hhincome+ educyear+ married+ hisp,   data = Data)
Call:
mfx::logitmfx(formula = ins ~ retire + age + hstatusg + hhincome + 
    educyear + married + hisp, data = Data)

Marginal Effects:
                dF/dx   Std. Err.       z     P>|z|    
retire1    0.04572549  0.01939558  2.3575 0.0183974 *  
age       -0.00341286  0.00263888 -1.2933 0.1959072    
hstatusg1  0.07166126  0.02056944  3.4839 0.0004942 ***
hhincome   0.00053865  0.00017849  3.0179 0.0025456 ** 
educyear   0.02671792  0.00330249  8.0902 5.955e-16 ***
married    0.12956012  0.01974445  6.5618 5.314e-11 ***
hisp      -0.16770282  0.03417763 -4.9068 9.257e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

dF/dx is for discrete change for the following variables:

[1] "retire1"   "hstatusg1" "married"   "hisp"     
mfx::probitmfx(ins~retire + age+ hstatusg+ hhincome+ educyear+ married+ hisp,   data = Data)
Call:
mfx::probitmfx(formula = ins ~ retire + age + hstatusg + hhincome + 
    educyear + married + hisp, data = Data)

Marginal Effects:
                dF/dx   Std. Err.       z     P>|z|    
retire1    0.04460886  0.01923347  2.3193  0.020377 *  
age       -0.00336099  0.00260051 -1.2924  0.196206    
hstatusg1  0.07380110  0.02033914  3.6285  0.000285 ***
hhincome   0.00046711  0.00016576  2.8180  0.004833 ** 
educyear   0.02680980  0.00320948  8.3533 < 2.2e-16 ***
married    0.13255492  0.01959506  6.7647 1.336e-11 ***
hisp      -0.16351772  0.03351731 -4.8786 1.068e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

dF/dx is for discrete change for the following variables:

[1] "retire1"   "hstatusg1" "married"   "hisp"     

Classification

Xem xét khả năng dự báo của mô hình 1- Xác định xác suất theo từng quan sát từ mô hình

# Run the model on the test set
test.probs <-predict(logit, Data, type='response')

2- Tạo biến dự báo (đặt giá trị 0)

Data$pred.logit <- rep(0,length(test.probs))

3- Chọn mức xác suất để xác định dự báo sang 1, mặc định là 0.5. Tùy theo lĩnh vực và hiểu biết trong lĩnh vực đó có thể điều chỉnh cho phù hợp.

Data$pred.logit[test.probs>=0.5] <- 1 # hễ xác suất của quan sát là 0.5 trở lên thì biến dự báo thành 1.

5- So sánh với giá trị thực: so sánh giữa biến mục tiêu ins và biến dự báo pred.logit

Data%>%
  select(ins, pred.logit) %>%
  table() %>%
  prop.table()* 100
   pred.logit
ins         0         1
  0 51.653150  9.638178
  1 27.885215 10.823456

5- Dùng lệnh test chi tiết Chuyển dữ liệu của các biến sang dạng factor (hiện ở dạng numeric 0,1)

Data %>% 
  mutate(ins=as.factor(ins),
         pred.logit=as.factor(pred.logit))-> Data

Dùng lệnh để kiểm tra

caret::confusionMatrix(Data$ins, Data$pred.logit)
Confusion Matrix and Statistics

          Reference
Prediction    0    1
         0 1656  309
         1  894  347
                                          
               Accuracy : 0.6248          
                 95% CI : (0.6077, 0.6416)
    No Information Rate : 0.7954          
    P-Value [Acc > NIR] : 1               
                                          
                  Kappa : 0.134           
                                          
 Mcnemar's Test P-Value : <2e-16          
                                          
            Sensitivity : 0.6494          
            Specificity : 0.5290          
         Pos Pred Value : 0.8427          
         Neg Pred Value : 0.2796          
             Prevalence : 0.7954          
         Detection Rate : 0.5165          
   Detection Prevalence : 0.6129          
      Balanced Accuracy : 0.5892          
                                          
       'Positive' Class : 0               
                                          

Advance

Trên thực tế cần điều chỉnh cách làm để mô hình tốt hơn bằng cách tách dữ liệu ban đầu thành 2 nhóm, 1 nhóm dùng để xác định các mô hình tiềm năng, phù hợp (training models), phần dữ liệu còn lại dùng để kiểm tra thực chứng mô hình với các phần dữ liệu chưa có trong mô hình (testing).

Thông thường ngta thường chia thành 80% dữ liệu để training, 20% dữ liệu để testing. Nếu thực hiện, thực hiện chia tách dữ liệu ban đầu như sau

set.seed(123) # thiết lập để kết quả có thể lặp lại/ kiểm tra lại sau mỗi lần thực hiện
Dat.train <- caret::createDataPartition(Data$ins, p = 0.8, list = FALSE) # cắt dữ liệu thành 80%
train_data <- Data[Dat.train, ] # Mẫu 80%
test_data <- Data[-Dat.train, ]  # Mẫu 20%

Các bước tiếp theo thực hiện tương tự.