1 Giới thiệu về tập dữ liệu

Tập dữ liệu này ban đầu được zameen.com thu thập dưới dạng dữ iệu giá nhà ở Pakistan và đã được sử dụng một số kỹ thuật làm sạch dữ liệu để cung cấp tập dữ liệu đặc biệt cho Thành phố Islamabad

Mô tả dữ liệu: Bộ dữ liệu ta lấy được gồm có 153430 quan sát và 15 biến:

  1. Property type: là các loại tài sản. Trong phần này, chúng ta có 6 loại khác nhau: House, FarmHouse, Upper Portion, Lower Portion, Flat, Room

  2. Price: là giá của các loại tài sản

  3. Location: về các loại vị trí khác nhau trong mỗi thành phố.

  4. City: thành phố. Trong bộ dữ liệu này có 5 thành phố:Lahore, Karachi, Faisalabad, Rawalpindi, Islamabad

  5. Province_name: tên tỉnh

  6. Latitude: chiều rộng của căn nhà

  7. Longitde: Chiều dài của căn nhà

  8. Baths: số phòng tắm

  9. Purpose: mục đích của căn hộ

  10. Bedrooms: số phòng ngủ

  11. Date_added: Ngày được thêm vào

  12. Agency: hãng

  13. Agent: đại lý

  14. Area in Marla: khu vực ở Marla

  15. Biến số thứ tự

2 Lấy dữ liệu từ file excel

library(readxl)
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(epitools)
# *Găn dữ liệu EDA cho biến home*
home <- read_excel("D:/RStudio/EDA1.xlsx", sheet =1)
## Warning: Expecting numeric in F3081 / R3081C6: got a date
head (home)
## # A tibble: 6 x 14
##   property_type    price location   city  province_name latitude longitude baths
##   <chr>            <dbl> <chr>      <chr> <chr>            <dbl>     <dbl> <dbl>
## 1 Flat          10000000 G-10       Isla~ Islamabad Ca~  3.37e 6   7.30e 6     2
## 2 Flat           6900000 E-11       Isla~ Islamabad Ca~  3.37e 7   7.30e 7     3
## 3 House         16500000 G-15       Isla~ Islamabad Ca~  3.36e16   7.29e 7     6
## 4 House         43500000 Bani Gala  Isla~ Islamabad Ca~  3.37e13   7.32e12     4
## 5 House          7000000 DHA Defen~ Isla~ Islamabad Ca~  3.35e 7   7.33e 7     3
## 6 House         34500000 Ghauri To~ Isla~ Islamabad Ca~  3.36e16   7.31e 7     8
## # i 6 more variables: purpose <chr>, bedrooms <dbl>, date_added <dttm>,
## #   agency <chr>, agent <chr>, Area_in_Marla <dbl>

3 Thống kê mô tả

3.1 Phân tích biến purpose

# Bảng tần số
table(home$purpose)
## 
## For Rent For Sale 
##    43183   110247
## *Hiển thị theo biểu đồ*
library(ggplot2)
home |> ggplot(aes(x = purpose, y = after_stat(count))) +
geom_bar(fill = 'pink') +geom_text(aes(label = scales::percent(after_stat(count/sum(count)))), stat ='count', color = 'blue', vjust = - .5) +labs(x = 'Frequency of purpose', y = 'Number of customers')

Dựa theo kết quả phân tích của phần mềm R tôi thấy mục đích sử dụng căn hộ cho việc buôn bán nhà chiếm tỷ lệ 72% và gấp đến 2,6 lần so với việc sử dụng để cho thuê căn hộ chỉ chiếm khoảng 28% trong tổng số.

3.2 Thống kê mô tả biến phụ thuộc purpose với biến độc lập property_type

3.2.1 Bảng tần số và biểu đồ kết hợp

#1.Thống kê mô tả cho 2 biến purpose và biến property_type. 
gd.how <- table(home$property_type, home$purpose)
addmargins(gd.how)
##                
##                 For Rent For Sale    Sum
##   Farm House          46      580    626
##   Flat              9347    24962  34309
##   House            15466    81177  96643
##   Lower Portion     7339      954   8293
##   Penthouse           93      288    381
##   Room               638       25    663
##   Upper Portion    10254     2261  12515
##   Sum              43183   110247 153430
## *Hiển thị theo biểu đồ*
barplot(gd.how, beside = TRUE, xlab = "Purpose group",main = "Number of property in each purpose group")

home |> ggplot(aes(x= property_type,y=after_stat(count))) + geom_bar(fill="lightblue") + geom_text(aes(label = scales::percent(after_stat(count/sum(count)))), stat = "count", color = "brown", vjust = - .5) + facet_grid(. ~purpose) + labs(x = "purpose")

Theo kết quả tính toán và đồ thị của phần mềm R, chúng ta thấy loại tài sản là nhà (House) sử dụng cho mục đich buôn bán chiếm tỷ lệ rất cao đến 52,908% trong tổng số, trong đó loại tài sản là buồng, phòng (room) sử dụng cho việc buôn bán (25 căn) thấp hơn rất nhiều so với việc sử dụng để cho thuê tầm 638 căn trong tổng số.

3.2.2 Rủi ro tương đối (Relative Risk)

library(epitools)
riskratio(gd.how)
## $data
##                
##                 For Rent For Sale  Total
##   Farm House          46      580    626
##   Flat              9347    24962  34309
##   House            15466    81177  96643
##   Lower Portion     7339      954   8293
##   Penthouse           93      288    381
##   Room               638       25    663
##   Upper Portion    10254     2261  12515
##   Total            43183   110247 153430
## 
## $measure
##                risk ratio with 95% C.I.
##                   estimate      lower      upper
##   Farm House    1.00000000         NA         NA
##   Flat          0.78526756 0.76741895 0.80353129
##   House         0.90658585 0.88665302 0.92696679
##   Lower Portion 0.12416038 0.11650490 0.13231891
##   Penthouse     0.81585664 0.76744171 0.86732588
##   Room          0.04069798 0.02768843 0.05982011
##   Upper Portion 0.19499167 0.18672039 0.20362934
## 
## $p.value
##                two-sided
##                   midp.exact  fisher.exact    chi.square
##   Farm House              NA            NA            NA
##   Flat          0.000000e+00  7.455757e-36  9.363517e-29
##   House         1.144141e-10  1.491812e-10  3.732745e-09
##   Lower Portion 0.000000e+00  0.000000e+00  0.000000e+00
##   Penthouse     8.237855e-14  9.013284e-14  2.695911e-14
##   Room          0.000000e+00 1.926333e-270 4.241531e-224
##   Upper Portion 0.000000e+00  0.000000e+00  0.000000e+00
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "Unconditional MLE & normal approximation (Wald) CI"

3.2.3 Tỷ lệ chênh (Odd Ratio)

oddsratio(gd.how)
## $data
##                
##                 For Rent For Sale  Total
##   Farm House          46      580    626
##   Flat              9347    24962  34309
##   House            15466    81177  96643
##   Lower Portion     7339      954   8293
##   Penthouse           93      288    381
##   Room               638       25    663
##   Upper Portion    10254     2261  12515
##   Total            43183   110247 153430
## 
## $measure
##                odds ratio with 95% C.I.
##                    estimate       lower       upper
##   Farm House    1.000000000          NA          NA
##   Flat          0.212493729 0.155105594 0.283790741
##   House         0.417634271 0.304960883 0.557474922
##   Lower Portion 0.010368176 0.007534767 0.013922136
##   Penthouse     0.246343227 0.167087514 0.358730609
##   Room          0.003179906 0.001884310 0.005120502
##   Upper Portion 0.017533077 0.012798041 0.023502363
## 
## $p.value
##                two-sided
##                   midp.exact  fisher.exact    chi.square
##   Farm House              NA            NA            NA
##   Flat          0.000000e+00  7.455757e-36  9.363517e-29
##   House         1.144141e-10  1.491812e-10  3.732745e-09
##   Lower Portion 0.000000e+00  0.000000e+00  0.000000e+00
##   Penthouse     8.237855e-14  9.013284e-14  2.695911e-14
##   Room          0.000000e+00 1.926333e-270 4.241531e-224
##   Upper Portion 0.000000e+00  0.000000e+00  0.000000e+00
## 
## $correction
## [1] FALSE
## 
## attr(,"method")
## [1] "median-unbiased estimate & mid-p exact CI"

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

Kiểm định chi bình phương (Chi-Square) cho hai biến giới tình và tình trạng có nhà.

Đặt giả thuyết:

Ho: Biến purpose và biến property-type là độc lập

H1: Biến purpose và biến property-type không độc lậP

chisq.test(gd.how)
## 
##  Pearson's Chi-squared test
## 
## data:  gd.how
## X-squared = 41555, df = 6, p-value < 2.2e-16

Với p_value < 5% vậy chưa đủ cơ sở bác bỏ Ho tại mức ý nghĩa 5%. Kết quả này cho chúng ta thấy rằng giữa biến purpose và biến property là độc lập với nhau. Đồng nghĩa với việc cho thấy biến các loại tài sản và mục đích sử dụng căn hộ có mối quan hệ liên kết.

4 Thống kê suy diễn cho khoảng ước lượng cho tỷ lệ

1. Ước lượng tỷ lệ cho biến price của các loại tài sản có mức giá lớn hơn 10,000,000 đồng thời kiểm định xem tỷ lệ (%) các loại tài sản có mức giá lớn hơn 10,000,000 có phải là 65% không ?

a.Đặt giả thuyết:

Ho: p = 0.65

H1: p # 0.65

home1 <- home[home$price > 10000000,]
prop.test(length(home1$price), length(home$price), p = 0.65)
## 
##  1-sample proportions test with continuity correction
## 
## data:  length(home1$price) out of length(home$price), null probability 0.65
## X-squared = 25040, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.65
## 95 percent confidence interval:
##  0.4548146 0.4598065
## sample estimates:
##         p 
## 0.4573095

Trong kết quả phần mềm R, prop.test ước tính tỉ lệ khách hàng có thu nhập trên 40.000đô/tháng 0,4573 (tức 45,73%) vậy ta bác bỏ Ho và với khoảng tin cậy 95% là 0,4548 đến 0,4598. Giá trị khi chi bình phương là 25,040; với trị số < 2.2e-16. Như vậy, nghiên cứu này cho thấy tỷ lệ khách hàng có thu nhập trên 40.000đô/tháng đến mua xe thấp hơn 65%.

Thực hiện bài toán kiểm định giả thuyết sự bằng nhau về tỷ lệ về mục đích của những căn hộ có mức giá lớn hơn 10,000,000 của 2 tổng thể mục đích để bán và cho thuê:

Đặt giả thuyết

Ho: p1 = p2

H1: p1 # p2

homer <- home[home$purpose == 'For Rent',]
homes <- home[home$purpose == 'For Sale',]

homer3 <- homer[homer$price > 10000000,]
homes3 <- homes[homes$price > 10000000,]

a <- c(nrow(homer), nrow(homes))
b <- c(nrow(homer3), nrow(homes3))

prop.test(b,a)
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  b out of a
## X-squared = 50635, df = 1, p-value < 2.2e-16
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.6392583 -0.6335464
## sample estimates:
##       prop 1       prop 2 
## 2.315726e-05 6.364255e-01

Trong kết quả kiểm định trên, prop.test ước tính tỷ lệ những căn sử dụng cho mục đich cho thuê có mức giá lớn hơn 10,000,000 là 2,3157.e-05 và ước tính tỉ lệ tỷ lệ những căn sử dụng cho mục đich buôn bán có mức giá lớn hơn 10,000,000 là 6.3642.e-01. Vậy ta bác bỏ Ho với độ tin cậy 95%. Đồng thời phân tích còn cho thấy với mức ý nghĩa 5%, tỷ lệ nam giới có thu nhập trên 40.000đô/tháng đến mua xe nhỏ hơn 2.2e-16.

5 Hồi quy cho biến purpose

Ước lượng hàm hồi quy cho biến purpose (mục đích sử dụng căn hộ) phụ thuộc vào các biến độc lập bao gồm: biến price, biến bath, biến bedroom, biến Area_in_Marla và biến property_type.

Đối với hàm PUR phạm trù thứ hai của biến nhị phân thể hiện cho “thành công” ở đây là “For Sale”, thể hiện mục đich sử dụng căn hộ.

# Chuyển đổi dữ liệu định tính thành định lượng
home$purpose <- ifelse(home$purpose=="For Sale",1,0)
PUR <- home$purpose
PR <- home$property_type
ce <- home$price
bat<- home$baths
bed <- home$bedrooms
are <- home$Area_in_Marla


# Xác định thứ tự các phạm trù của biến Purchased Bike
levels(factor(PUR))
## [1] "0" "1"
home$purpose<-as.factor(home$purpose)
home$property_type<-as.factor(home$property_type)

5.1 Mô hình hồi quy logit

# Ước lượng
purch1 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type  , family = binomial(link = "logit"), data = home)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(purch1)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "logit"), data = home)
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                -3.294e+00  5.512e-01  -5.977 2.28e-09 ***
## price                       5.445e-06  1.180e-07  46.142  < 2e-16 ***
## baths                      -9.078e-02  5.575e-02  -1.628   0.1035    
## bedrooms                   -3.956e-01  6.054e-02  -6.535 6.36e-11 ***
## property_typeFlat          -1.257e-01  5.520e-01  -0.228   0.8199    
## property_typeHouse         -2.907e+00  5.586e-01  -5.205 1.94e-07 ***
## property_typeLower Portion -2.582e+00  6.158e-01  -4.194 2.74e-05 ***
## property_typePenthouse     -1.377e+00  1.050e+00  -1.311   0.1898    
## property_typeRoom          -1.881e+00  7.841e-01  -2.399   0.0164 *  
## property_typeUpper Portion -2.489e+00  5.946e-01  -4.186 2.84e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 182372.5  on 153429  degrees of freedom
## Residual deviance:   3259.2  on 153420  degrees of freedom
## AIC: 3279.2
## 
## Number of Fisher Scoring iterations: 16

Dựa vào kết quả phân tích ước lượng của phần mềm R, ta thấy Giá trị Pr(>|z|) của các biến các loại tài sản, biến giá của các loại tài sản, biến số phòng tắm, biến số phòng ngủ và biến Area (khu vực ở Marla) đều nhỏ hơn 5% cho thấy mỗi biến trên đều có ý nghĩa thống kê trong mô hình.

5.2 Mô hình hồi quy probit

# Ước lượng
purch2 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type  , family = binomial(link = "probit"), data = home)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(purch2)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "probit"), data = home)
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                -1.741e+00  2.655e-01  -6.560 5.39e-11 ***
## price                       2.429e-06  3.865e-08  62.841  < 2e-16 ***
## baths                      -1.252e-02  2.169e-02  -0.577 0.563782    
## bedrooms                   -1.534e-01  2.336e-02  -6.569 5.08e-11 ***
## property_typeFlat          -2.094e-01  2.656e-01  -0.788 0.430532    
## property_typeHouse         -1.146e+00  2.673e-01  -4.286 1.82e-05 ***
## property_typeLower Portion -1.017e+00  2.784e-01  -3.653 0.000259 ***
## property_typePenthouse     -6.143e-01  4.314e-01  -1.424 0.154458    
## property_typeRoom          -8.047e-01  3.317e-01  -2.426 0.015266 *  
## property_typeUpper Portion -9.890e-01  2.738e-01  -3.612 0.000304 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 182372.5  on 153429  degrees of freedom
## Residual deviance:   3585.2  on 153420  degrees of freedom
## AIC: 3605.2
## 
## Number of Fisher Scoring iterations: 19

Dựa vào kết quả phân tích ước lượng của phần mềm R, ta thấy Giá trị Pr(>|z|) của các biến các loại tài sản, biến giá của các loại tài sản, biến số phòng tắm, biến số phòng ngủ và biến Area (khu vực ở Marla) đều nhỏ hơn 5% cho thấy mỗi biến trên đều có ý nghĩa thống kê trong mô hình.

5.3 Mô hình hồi quy cloglog

# Ước lượng
purch3 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type  , family = binomial(link = "cloglog"), data = home)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(purch3)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "cloglog"), data = home)
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                -6.844e+00  5.164e-01 -13.254  < 2e-16 ***
## price                       4.097e-06  7.441e-08  55.061  < 2e-16 ***
## baths                       6.728e-01  4.637e-02  14.510  < 2e-16 ***
## bedrooms                   -1.023e+00  4.850e-02 -21.091  < 2e-16 ***
## property_typeFlat           3.642e+00  5.184e-01   7.024 2.15e-12 ***
## property_typeHouse          1.516e+00  5.203e-01   2.914 0.003573 ** 
## property_typeLower Portion -6.547e-01  5.970e-01  -1.096 0.272863    
## property_typePenthouse      2.849e+00  7.596e-01   3.750 0.000177 ***
## property_typeRoom           2.127e+00  6.693e-01   3.178 0.001483 ** 
## property_typeUpper Portion  1.471e+00  5.416e-01   2.716 0.006599 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 182372.5  on 153429  degrees of freedom
## Residual deviance:   4505.8  on 153420  degrees of freedom
## AIC: 4525.8
## 
## Number of Fisher Scoring iterations: 25

Dựa vào kết quả phân tích ước lượng của phần mềm R, ta thấy Giá trị Pr(>|z|) của các biến các loại tài sản, biến giá của các loại tài sản, biến số phòng tắm, biến số phòng ngủ và biến Area (khu vực ở Marla) đều nhỏ hơn 5% cho thấy mỗi biến trên đều có ý nghĩa thống kê trong mô hình.

6 Các tiêu chí đánh giá mô hình

Sau khi thực hiện kiểm định sự phù hợp của mô hình logit, probit và cloglog, chúng ta thấy cả 3 mô hình đều phù hợp với dữ liệu.

6.1 AIC - Akaike Information Criterion

Tiêu chí AIC là một số liệu được sử dụng để so sánh sự phù hợp của các mô hình hồi quy khác nhau, ta có:

AIC(logit) = 3279,2

AIC(probit) = 3605,2

AIC(cloglog) = 4525,8

Vậy mô hình hồi quy logit có AIC thấp nhất mang lại sự phù hợp nhất.

6.2 Deviance

# Tiêu chí Deviance
de1 <- deviance(purch1)
de2 <- deviance(purch2)
de3 <- deviance(purch3)
deviance <- cbind(de1,de2,de3)
deviance
##           de1      de2      de3
## [1,] 3259.215 3585.169 4505.815

Ta có: Deviance(logit) = 3259,2

Deviance(probit) = 3585,2

Deviance(cloglog) = 4505,8

Mô hình logit có deviance nhỏ nhất nên ta chọn mh này

6.3 Brier Score

Brier Score là chỉ tiêu dùng để đánh giá mô hình hồi quy logistic, giá trị của Brier Score càng nhỏ nghĩa là chênh lệch giữa xác suất thực tế và xác suất tính từ mô hình càng nhỏ, nghĩa là mô hình càng tốt.

library (DescTools)
BrierScore(purch1)
## [1] 0.00189995
BrierScore(purch2)
## [1] 0.002125317
BrierScore(purch3)
## [1] 0.002385188

Từ tính toán của Giá trị Brier Score của 3 mô hình logit, probit và cloglog, ta thấy BrierScore của logit có giá trị 0.00189995 nhỏ nhất nghĩa là mô hình này là tốt nhất.

6.4 Ma trận nhầm lẫn

6.4.1 Mô hình logit

library(caret)
## Warning: package 'caret' was built under R version 4.3.1
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following objects are masked from 'package:DescTools':
## 
##     MAE, RMSE
# Đánh giá mô hình trên tập kiểm tra
predictions <- predict(purch1, newdata = home, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0")  # Chỉnh ngưỡng phân loại
predictions1<-factor(predicted_classes, levels = c("0","1"))
actual<- factor(home$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  43116    313
##            1     67 109934
##                                           
##                Accuracy : 0.9975          
##                  95% CI : (0.9973, 0.9978)
##     No Information Rate : 0.7185          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9939          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.9984          
##             Specificity : 0.9972          
##          Pos Pred Value : 0.9928          
##          Neg Pred Value : 0.9994          
##              Prevalence : 0.2815          
##          Detection Rate : 0.2810          
##    Detection Prevalence : 0.2831          
##       Balanced Accuracy : 0.9978          
##                                           
##        'Positive' Class : 0               
## 

Mô hình logit có độ chính xác toàn thể là 99,75%, độ nhạy là 78,06% và độ hiệu quả là 99,84%.

6.4.2 Mô hình probit

# Đánh giá mô hình trên tập kiểm tra
predictions <- predict(purch2, newdata = home, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0")  # Chỉnh ngưỡng phân loại
predictions1<-factor(predicted_classes, levels = c("0","1"))
actual<- factor(home$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  43111    322
##            1     72 109925
##                                           
##                Accuracy : 0.9974          
##                  95% CI : (0.9972, 0.9977)
##     No Information Rate : 0.7185          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9937          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.9983          
##             Specificity : 0.9971          
##          Pos Pred Value : 0.9926          
##          Neg Pred Value : 0.9993          
##              Prevalence : 0.2815          
##          Detection Rate : 0.2810          
##    Detection Prevalence : 0.2831          
##       Balanced Accuracy : 0.9977          
##                                           
##        'Positive' Class : 0               
## 

Mô hình probit có độ chính xác toàn thể là 99,74%, độ nhạy là 99,83% và độ hiệu quả là 99,72%.

6.4.3 Mô hình cloglog

# Đánh giá mô hình trên tập kiểm tra
predictions <- predict(purch3, newdata = home, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0")  # Chỉnh ngưỡng phân loại
predictions1<-factor(predicted_classes, levels = c("0","1"))
actual<- factor(home$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  43089    365
##            1     94 109882
##                                           
##                Accuracy : 0.997           
##                  95% CI : (0.9967, 0.9973)
##     No Information Rate : 0.7185          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.9926          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.9978          
##             Specificity : 0.9967          
##          Pos Pred Value : 0.9916          
##          Neg Pred Value : 0.9991          
##              Prevalence : 0.2815          
##          Detection Rate : 0.2808          
##    Detection Prevalence : 0.2832          
##       Balanced Accuracy : 0.9973          
##                                           
##        'Positive' Class : 0               
## 

Mô hình cloglog có độ chính xác toàn thể là 99,7%, độ nhạy là 99,78% và độ hiệu quả là 99,67%

MH logit có độ chính xác toàn thể, độ nhạy, độ hiệu quả cao nhất trong 3 mh nên chọn mh này.

Kết luận: Dựa vào 4 tiêu chuẩn trên ta thấy MH logit là mô hình được chọn. Do đó mh logit là mh phù hợp nhất trong 3 mh.