setwd("C:/Users/Ngoc Loi/Downloads")
DT <- read.csv("C:/Users/Ngoc Loi/Downloads/dataset.csv")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.1
## 
## 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

1 Thống kê mô tả Biến purpose

## Bảng tần suất của biến purpose
table(DT$purpose)
## 
## For Rent For Sale 
##    43183   110247
## Bảng tần suất của biến purpose theo tỉ lệ %
table(DT$purpose)/sum(table(DT$purpose))
## 
##  For Rent  For Sale 
## 0.2814508 0.7185492
  • Dựa vào kết quả của bảng tần suất purpose thì tỉ lệ rao bán chiếm cao nhất khoảng 71.8%, còn lại là tỉ lệ cho thuê chiếm khoảng 28.14%

1.1 Đồ thị cột của biến purpose

DT |> ggplot(aes(DT$purpose)) +
  geom_bar(color = 'black', fill = 'black') + theme_classic() + labs(x = '', y = 'Tần số')
## Warning: Use of `DT$purpose` is discouraged.
## ℹ Use `purpose` instead.

Dựa vào biểu đồ cột, cho ta thu được kết quả, For Rent chiếm xấp sỉ 35000 quan sát và For Sale chiếm hơn 90000 quan sát.

2 Ước lượng tỷ lệ

FS<-DT[DT$purpose == "For Sale",]
length(DT$purpose)
## [1] 153430
prop.test(length(FS$purpose),length(DT$purpose),p= 0.75)
## 
##  1-sample proportions test with continuity correction
## 
## data:  length(FS$purpose) out of length(DT$purpose), null probability 0.75
## X-squared = 809.25, df = 1, p-value < 2.2e-16
## alternative hypothesis: true p is not equal to 0.75
## 95 percent confidence interval:
##  0.7162903 0.7207971
## sample estimates:
##         p 
## 0.7185492

Ước lượng tỷ lệ căn hộ với mục đích cho BÁN có phải là 75% hay không

\(H_0\): mục đích cho BÁN chiếm là 75%

\(H_1\): mục đích cho BÁN không chiếm tới 75%

Vì p-value >0,05; chấp nhận \(H_0\) Vì vậy mục đích cho bán căn hộ chiếm đến 75% với mức ý nghĩa 5%

3 Mô hình hồi quy

3.1 Mô hình hồi quy logit

DT$purpose<-as.factor(DT$purpose)
DT$property_type<-as.factor(DT$property_type)

MH1 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type , family = binomial(link = "logit"), data = DT)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(MH1)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "logit"), data = DT)
## 
## 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

Với mô hình hồi quy logit của biến phụ thuộc purpose với các biến độc lập thì có ý nghĩa thống kê ở mức ý nghĩa 5% với price, bedrooms.

3.2 Mô hình hồi quy probit

MH2 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type , family = binomial(link = "probit"), data = DT)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(MH2)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "probit"), data = DT)
## 
## 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

Với mô hình hồi quy probit của biến phụ thuộc purpose với các biến độc lập thì có ý nghĩa thống kê ở mức ý nghĩa 5% với price, bedrooms.

3.3 Mô hình Cloglog

MH3 <- glm(factor (purpose) ~ price + baths + bedrooms + property_type , family = binomial(link = "cloglog"), data = DT)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(MH3)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + property_type, 
##     family = binomial(link = "cloglog"), data = DT)
## 
## 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

Với mô hình hồi quy cloglog của biến phụ thuộc purpose với các biến độc lập thì có ý nghĩa thống kê ở mức ý nghĩa 5% với price, baths, bedrooms.

4 Lựa chọn mô hình

require(tidyverse)
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'purrr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ✔ readr     2.1.4
## ── 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(DescTools)
## Warning: package 'DescTools' was built under R version 4.3.1
BrierScore(MH1)
## [1] 0.00189995
BrierScore(MH2)
## [1] 0.002125317
BrierScore(MH3)
## [1] 0.002385188

Trong đó:

  • Mô hình hồi quy logit: MH1

  • Mô hình hồi quy probit: MH2

  • Mô hình hồi quy cloglog: MH3

Tiến hành so sánh:

  • So sánh AIC:

AIC(MH1) = 3279,2

AIC(MH2) = 3605,2

AIC(MH3) = 4525,8

=> Mô hình logit có AIC nhỏ nhất nên mô hình logit tối ưu nhất.

  • So sánh Deviance:

Deviance(MH1) = 3259,2

Deviance(MH2) = 3585,2

Deviance(MH3) = 4505,8

=> Mô hình logit có Deviance nhỏ nhất nên mô hình logit tối ưu nhất.

  • So sánh BrierScore:

BrierScore(MH1) = 0.00189995

BrierScore(MH2) = 0.002125317

BrierScore(MH3) = 0.002385188

=> Mô hình logit có BrierScore nhỏ nhất nên mô hình logit tối ưu nhất.

Dựa vào ba yếu tố trên, ta thu được kết quả của mô hình logit có giá trị nhỏ nhất vì vậy mô hình hồi quy logit phù hợp với data nhất.