require(tidyverse)
## Loading required package: tidyverse
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(DescTools)
## Warning: package 'DescTools' was built under R version 4.2.3
library(epitools)
library(ggplot2)
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
library(lmtest)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
setwd("C:/Users/HP/Documents/PTDLĐT/")
data <- read_excel("For_EDA_dataset 1.xlsx")
## Warning: Expecting numeric in G3081 / R3081C7: got a date
## New names:
## • `` -> `...1`
data
- Bảng tần số và tần suất
table(data$purpose)
##
## For Rent For Sale
## 43183 110247
table(data$purpose)/sum(table(data$purpose))
##
## For Rent For Sale
## 0.2814508 0.7185492
Biến purpose thể hiện mục đích của căn hộ dùng để thuê hay để bán. Dựa vào kết quả thống kê, ta thấy có 43.183 căn hộ được sử dụng với mục đích thuê (chiếm 28,14% tổng căn hộ) và 110.247 căn hộ được sử dụng với mục đích bán (tương đương 71,85%).
- Đồ thị
library(ggplot2)
data |> ggplot(aes(x = purpose, y = after_stat(count))) +
geom_bar(fill = 'blue') +geom_text(aes(label = scales::percent(after_stat(count/sum(count)))), stat ='count', color = 'red', vjust = - .5) +labs(x = 'Mục đích căn hộ', y = 'Căn hộ')
Dựa vào đồ thị cho thấy số lượng căn hộ được sử dụng với mục đích thuê và bán, ta thấy có sự chênh lệch rõ rệt giữa số lượng căn hộ cho thuê và căn hộ được bán, tỷ lệ căn hộ với mục đích bán ra gấp 2,57 lần so với tỷ lệ căn hộ cho thuê.
Ước lượng tỷ lệ căn hộ với mục đích bán có phải là 72% hay không (nghĩa là ta kiểm định giả thuyết” H0: p= 0.72”)
a <- data[data$purpose == 'For Sale',]
prop.test(length(a$purpose),length(data$purpose),p= 0.72)
##
## 1-sample proportions test with continuity correction
##
## data: length(a$purpose) out of length(data$purpose), null probability 0.72
## X-squared = 1.5948, df = 1, p-value = 0.2066
## alternative hypothesis: true p is not equal to 0.72
## 95 percent confidence interval:
## 0.7162903 0.7207971
## sample estimates:
## p
## 0.7185492
Kết quả kiểm định với P_value >5%, cho thấy chưa đủ cơ sở bác bỏ giả thuyết \(H_0\). Do đó tỷ lệ số căn hộ với mục đích bán bằng 72% với mức ý nghĩa 5%.
Khoảng ước lượng tỷ lệ số căn hộ với mục đích bán với độ tin cậy 95% là (0,7162903; 0,7207971)
- Mã hóa dữ liệu price
price <- cut(data$price, breaks = c(min(data$price),mean(data$price),max(data$price)), labels=c("Thấp","Cao"))
table(price)
## price
## Thấp Cao
## 111009 42418
- Mã hóa dữ liệu Area_in_Marla
area <- cut(data$Area_in_Marla, breaks = c(min(data$Area_in_Marla),mean(data$Area_in_Marla),max(data$Area_in_Marla)), labels=c("Thấp","Cao"))
table(area)
## area
## Thấp Cao
## 115969 37449
- Mã hóa dữ liệu baths
baths <- cut(data$baths, breaks = c(min(data$baths),mean(data$baths),max(data$baths)), labels=c("ít","nhiều"))
table(baths)
## baths
## ít nhiều
## 28058 88546
- Mã hóa dữ liệu bedrooms
bedrooms <- cut(data$bedrooms, breaks = c(min(data$bedrooms),mean(data$bedrooms),max(data$bedrooms)), labels=c("ít","nhiều"))
table(bedrooms)
## bedrooms
## ít nhiều
## 72988 61563
Mô hình hồi quy logistic với link function = “logit” có dạng tổng quát như sau:
\[logit(π)=log(\frac{π}{1−π})=β_0+β_1X_1+β_2X_2+⋯+β_kX_k\]
mh1 <- glm(data = data, formula = factor(purpose) ~ price + baths + bedrooms + area + property_type, family = binomial(link = "logit"))
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
levels(factor(data$purpose))
## [1] "For Rent" "For Sale"
summary(mh1)
##
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + area +
## property_type, family = binomial(link = "logit"), data = data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.4904 -0.0085 0.0000 0.0000 4.7748
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.437e-01 6.064e-01 -0.237 0.812743
## price 5.737e-06 1.397e-07 41.077 < 2e-16 ***
## baths 8.546e-02 6.942e-02 1.231 0.218275
## bedrooms -4.237e-01 7.679e-02 -5.517 3.45e-08 ***
## areaCao -4.124e+00 3.002e-01 -13.740 < 2e-16 ***
## property_typeFlat -3.533e+00 6.172e-01 -5.725 1.04e-08 ***
## property_typeHouse -4.849e+00 6.125e-01 -7.918 2.42e-15 ***
## property_typeLower Portion -5.472e+00 6.699e-01 -8.168 3.13e-16 ***
## property_typePenthouse -4.389e+00 1.285e+00 -3.416 0.000636 ***
## property_typeRoom -5.041e+00 8.471e-01 -5.951 2.66e-09 ***
## property_typeUpper Portion -5.375e+00 6.566e-01 -8.186 2.69e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 182359.0 on 153417 degrees of freedom
## Residual deviance: 2928.4 on 153407 degrees of freedom
## (12 observations deleted due to missingness)
## AIC: 2950.4
##
## Number of Fisher Scoring iterations: 17
Kết quả hồi quy cho thấy mô hình 1 như sau:
MH1: \[logit(π) = 5.737e^{-06}price - 4.237e^{-01}bedrooms - 4.124e^{+00}area - 3.533e^{+00}propertyFlat - 4.849e^{+00}propertyHouse -5.472e^{+00}propertyLower Portion - 4.389e^{+00}propertyPenthouse - 5.041e^{+00}propertyRoom - 5.375e^{+00}propertyUpper Portion\]
với π = P(cardio = “N” hoặc “Y”)
Mô hình hồi quy probit với link function = “probit” có dạng tổng quát như sau:
\[probit(π)=\Phi (1-π))=β_0+β_1X_1+β_2X_2+⋯+β_kX_k\]
mh2 <- glm(data = data, formula = factor(purpose) ~ price + baths + bedrooms + area + property_type, family = binomial(link = "probit"))
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
levels(factor(data$purpose))
## [1] "For Rent" "For Sale"
summary(mh2)
##
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + area +
## property_type, family = binomial(link = "probit"), data = data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.4904 -0.0057 0.0000 0.0000 5.1301
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -6.171e-01 2.775e-01 -2.224 0.026160 *
## price 2.522e-06 4.380e-08 57.582 < 2e-16 ***
## baths 3.894e-02 2.499e-02 1.558 0.119281
## bedrooms -1.509e-01 2.733e-02 -5.522 3.35e-08 ***
## areaCao -1.394e+00 9.952e-02 -14.008 < 2e-16 ***
## property_typeFlat -1.431e+00 2.799e-01 -5.110 3.22e-07 ***
## property_typeHouse -1.863e+00 2.768e-01 -6.731 1.69e-11 ***
## property_typeLower Portion -2.047e+00 2.906e-01 -7.042 1.89e-12 ***
## property_typePenthouse -1.606e+00 4.797e-01 -3.347 0.000817 ***
## property_typeRoom -1.870e+00 3.391e-01 -5.514 3.51e-08 ***
## property_typeUpper Portion -1.990e+00 2.872e-01 -6.927 4.31e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 182359.0 on 153417 degrees of freedom
## Residual deviance: 3263.1 on 153407 degrees of freedom
## (12 observations deleted due to missingness)
## AIC: 3285.1
##
## Number of Fisher Scoring iterations: 19
Vậy nên mô hình probit được xác định như sau:
\[probit(π)=\Phi (1-π))= - 6.171e^{-01} + 2.522e^{-06}price - 1.509e^{-01}bedrooms - 1.394e^{+00}area - 1.431e^{+00}propertyFlat - 1.863e^{+00}propertyHouse - 2.047e^{+00}propertyLower Portion - 1.606e^{+00}propertyPenthouse - 1.870e^{+00}propertyRoom - 1.990e^{+00}propertyUpper Portion\]
với π = P(cardio = “N” hoặc “Y”)
Mô hình hồi quy cloglog với link function = “cloglog” có dạng tổng quát như sau:
\[cloglog(π)=log(-log(1-π))=β_0+β_1X_1+β_2X_2+⋯+β_kX_k\]
mh3 <- glm(data = data, formula = factor(purpose) ~ price + baths + bedrooms + area + property_type, family = binomial(link = "cloglog"))
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
levels(factor(data$purpose))
## [1] "For Rent" "For Sale"
summary(mh3)
##
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + area +
## property_type, family = binomial(link = "cloglog"), data = data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.4904 -0.0016 0.0000 0.0000 5.5212
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 8.221e-01 5.450e-01 1.508 0.131
## price 3.525e-06 7.448e-08 47.330 < 2e-16 ***
## baths -6.843e-02 5.609e-02 -1.220 0.222
## bedrooms -2.966e-01 6.105e-02 -4.858 1.18e-06 ***
## areaCao -7.918e+00 2.708e-01 -29.237 < 2e-16 ***
## property_typeFlat -3.841e+00 5.460e-01 -7.036 1.98e-12 ***
## property_typeHouse -5.103e+00 5.489e-01 -9.296 < 2e-16 ***
## property_typeLower Portion -5.691e+00 5.853e-01 -9.723 < 2e-16 ***
## property_typePenthouse -4.060e+00 8.218e-01 -4.940 7.82e-07 ***
## property_typeRoom -5.390e+00 6.897e-01 -7.815 5.48e-15 ***
## property_typeUpper Portion -5.758e+00 5.708e-01 -10.088 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 182359 on 153417 degrees of freedom
## Residual deviance: 3838 on 153407 degrees of freedom
## (12 observations deleted due to missingness)
## AIC: 3860
##
## Number of Fisher Scoring iterations: 25
Vậy nên mô hình cloglog được xác định như sau:
\[cloglog(π)=log(-log(1-π))= 3.525e^{-06}price - 2.966e^{-01}bedrooms - 7.918e^{+00}area - 3.841^{+00}propertyFlat - 5.103e^{+00}propertyHouse - 5.691e^{+00}propertyLower Portion - 4.060e^{+00}propertyPenthouse - 5.390e^{+00}propertyRoom - 5.758e^{+00}propertyUpper Portion\]
với π = P(cardio = “N” hoặc “Y”)
Để đánh giá các mô hình hồi quy logit, probit và cloglog, ta sử dụng các tiêu chí sau:
# Tiêu chí AIC - Akaike Information Criterion
aic1 <- AIC(mh1)
aic2 <- AIC(mh2)
aic3 <- AIC(mh3)
AIC <-cbind(aic1,aic2,aic3)
AIC
## aic1 aic2 aic3
## [1,] 2950.38 3285.127 3859.954
# Tiêu chí Deviance
de1 <- deviance(mh1)
de2 <- deviance(mh2)
de3 <- deviance(mh3)
deviance <- cbind(de1,de2,de3)
deviance
## de1 de2 de3
## [1,] 2928.38 3263.127 3837.954
# Tiêu chí Brier Score
bs1 <- BrierScore(mh1)
bs2 <- BrierScore(mh2)
bs3 <- BrierScore(mh3)
BrierScore <- cbind(bs1,bs2,bs3)
BrierScore
## bs1 bs2 bs3
## [1,] 0.001572506 0.001816104 0.001978942
Thông qua kết quả các tiêu chí AIC, deviance và Brier Score cho thấy giá trị của AIC, deviance và Brier Score của mô hình 1 là nhỏ nhất, nghĩa là mô hình logistic với hàm liên kết logit là mô hình tốt nhất trong 3 mô hình với hàm liên kết logit, probit, clogclog.
summary(mh1)
##
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + area +
## property_type, family = binomial(link = "logit"), data = data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.4904 -0.0085 0.0000 0.0000 4.7748
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.437e-01 6.064e-01 -0.237 0.812743
## price 5.737e-06 1.397e-07 41.077 < 2e-16 ***
## baths 8.546e-02 6.942e-02 1.231 0.218275
## bedrooms -4.237e-01 7.679e-02 -5.517 3.45e-08 ***
## areaCao -4.124e+00 3.002e-01 -13.740 < 2e-16 ***
## property_typeFlat -3.533e+00 6.172e-01 -5.725 1.04e-08 ***
## property_typeHouse -4.849e+00 6.125e-01 -7.918 2.42e-15 ***
## property_typeLower Portion -5.472e+00 6.699e-01 -8.168 3.13e-16 ***
## property_typePenthouse -4.389e+00 1.285e+00 -3.416 0.000636 ***
## property_typeRoom -5.041e+00 8.471e-01 -5.951 2.66e-09 ***
## property_typeUpper Portion -5.375e+00 6.566e-01 -8.186 2.69e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 182359.0 on 153417 degrees of freedom
## Residual deviance: 2928.4 on 153407 degrees of freedom
## (12 observations deleted due to missingness)
## AIC: 2950.4
##
## Number of Fisher Scoring iterations: 17
Kết quả hồi quy cho thấy mô hình 1 như sau:
MH1: \[logit(π) = 5.737e^{-06}price - 4.237e^{-01}bedrooms - 4.124e^{+00}area - 3.533e^{+00}propertyFlat - 4.849e^{+00}propertyHouse -5.472e^{+00}propertyLower Portion - 4.389e^{+00}propertyPenthouse - 5.041e^{+00}propertyRoom - 5.375e^{+00}propertyUpper Portion\]
với π = P(cardio = “N” hoặc “Y”)
Đối với mô hình hồi quy logit của biến phụ thuộc mục đích của căn hộ (purpose) với các biến giá tài sản (price), số phòng tắm (baths), số phòng ngủ (bedrooms), khu vực ở Marla (area), loại tài sản (property).
Ta thấy rằng có 9 yếu tố có mức ý nghĩa thống kê ở mức ý nghĩa 5% là giá tài sản (price), số phòng tắm (baths), số phòng ngủ (bedrooms), khu vực ở Marla (area), loại tài sản (property) như Flat, House, Lower Portion, Penthouse, Room, Upper Portion.
Giá tài sản tác động cùng hướng với mục đích của căn hộ.
Số phòng ngủ (bedrooms), khu vực ở Marla (area), loại tài sản (property) như Flat, House, Lower Portion, Penthouse, Room, Upper Portion có tác động ngược chiều với mục đích căn hộ
Ngoài ra, yếu tố số phòng tắm (baths) khống có ý nghĩa thống kế, tức là yếu tố không có ảnh hưởng đáng kể đến mục đích căn hộ.