1 Nhập dữ liệu

library(readxl)
data <- read_excel("C:/For_EDA_dataset 1.xlsx")
## Warning: Expecting numeric in G3081 / R3081C7: got a date
## New names:
## • `` -> `...1`
str(data)
## tibble [153,430 × 15] (S3: tbl_df/tbl/data.frame)
##  $ ...1         : num [1:153430] 0 1 2 3 4 5 6 7 8 9 ...
##  $ property_type: chr [1:153430] "Flat" "Flat" "House" "House" ...
##  $ price        : num [1:153430] 10000000 6900000 16500000 43500000 7000000 34500000 27000000 7800000 50000000 40000000 ...
##  $ location     : chr [1:153430] "G-10" "E-11" "G-15" "Bani Gala" ...
##  $ city         : chr [1:153430] "Islamabad" "Islamabad" "Islamabad" "Islamabad" ...
##  $ province_name: chr [1:153430] "Islamabad Capital" "Islamabad Capital" "Islamabad Capital" "Islamabad Capital" ...
##  $ latitude     : num [1:153430] 3.37e+06 3.37e+07 3.36e+16 3.37e+13 3.35e+07 ...
##  $ longitude    : num [1:153430] 7.30e+06 7.30e+07 7.29e+07 7.32e+12 7.33e+07 ...
##  $ baths        : num [1:153430] 2 3 6 4 3 8 8 2 7 5 ...
##  $ purpose      : chr [1:153430] "For Sale" "For Sale" "For Sale" "For Sale" ...
##  $ bedrooms     : num [1:153430] 2 3 5 4 3 8 8 2 7 5 ...
##  $ date_added   : POSIXct[1:153430], format: "2019-02-04" "2019-05-04" ...
##  $ agency       : chr [1:153430] "Self" "Self" "Self" "Self" ...
##  $ agent        : chr [1:153430] "Self" "Self" "Self" "Self" ...
##  $ Area_in_Marla: num [1:153430] 4 5.6 8 40 8 32 20 6.2 20 20 ...

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:

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

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

Baths: số phòng tắm

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

Bedrooms: số phòng ngủ

Area in Marla: khu vực ở Marla

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

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

Province_name: tên tỉnh

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

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

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

Agency: hãng

Agent: đại lý

Biến số thứ tự

2 Thống kê mô tả biến Purpose

  • Bảng tần số
table(data$purpose)
## 
## For Rent For Sale 
##    43183   110247
  • Bảng tần suất
table(data$purpose)/sum(table(data$purpose))
## 
##  For Rent  For Sale 
## 0.2814508 0.7185492

Dựa vào kết quả của bảng tần số và bảng tần suất, ta thấy trong 153430 căn hộ thì có 43183 căn hộ cho thuê chiếm 28,15% và có 110247 căn hộ rao bán chiếm 71,85%.

  • Biểu đồ
library(ggplot2)
ggplot(data,aes(purpose)) + geom_bar(color ="black", fill = "pink") + ylab("Số căn hộ") + xlab("Mục đích của căn hộ")

3 Ước lượng cho tỷ lệ

Ước lượng tỷ lệ căn hộ với mục đích rao bán có phải là 30% hay không (nghĩa là chúng ta kiểm định giả thuyết” \(H_{0}\): p= 0.30”)

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

Vì p_value < 0.05 nên ta bác bỏ giả thuyết \(H_{0}\). Do đó tỷ lệ số căn hộ với mục đích rao bán không phải bằng 30% với mức ý nghĩa 5%.

Khoảng ước lượng tỷ lệ số căn hộ với mục đích rao bán với độ tin cậy 95% là (0.7162903;0.7207971)

4 Mô hình hồi quy

4.1 Hồi quy với hàm logit

logit <- glm(factor (purpose) ~ data$property_type + data$price + data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "logit"), data = data)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(logit)
## 
## Call:
## glm(formula = factor(purpose) ~ data$property_type + data$price + 
##     data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "logit"), 
##     data = data)
## 
## Coefficients:
##                                   Estimate Std. Error    z value Pr(>|z|)    
## (Intercept)                      4.798e+13  2.733e+06  1.756e+07   <2e-16 ***
## data$property_typeFlat           1.556e+15  2.752e+06  5.652e+08   <2e-16 ***
## data$property_typeHouse         -5.182e+14  2.739e+06 -1.892e+08   <2e-16 ***
## data$property_typeLower Portion -3.930e+14  2.827e+06 -1.390e+08   <2e-16 ***
## data$property_typePenthouse     -4.679e+14  4.388e+06 -1.066e+08   <2e-16 ***
## data$property_typeRoom          -1.277e+15  3.774e+06 -3.384e+08   <2e-16 ***
## data$property_typeUpper Portion -2.953e+14  2.795e+06 -1.056e+08   <2e-16 ***
## data$price                       6.576e+07  5.084e-03  1.294e+10   <2e-16 ***
## data$baths                      -4.013e+13  9.157e+04 -4.382e+08   <2e-16 ***
## data$bedrooms                    6.753e+13  1.220e+05  5.535e+08   <2e-16 ***
## data$Area_in_Marla              -2.979e+12  1.843e+03 -1.617e+09   <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:  182373  on 153429  degrees of freedom
## Residual deviance: 1385951  on 153419  degrees of freedom
## AIC: 1385973
## 
## Number of Fisher Scoring iterations: 25

4.2 Hồi quy với hàm probit

probit <- glm(factor (purpose) ~ data$property_type + data$price + data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "probit"), data = data)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(probit)
## 
## Call:
## glm(formula = factor(purpose) ~ data$property_type + data$price + 
##     data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "probit"), 
##     data = data)
## 
## Coefficients:
##                                   Estimate Std. Error    z value Pr(>|z|)    
## (Intercept)                      6.968e+14  2.733e+06  2.550e+08   <2e-16 ***
## data$property_typeFlat           1.917e+14  2.752e+06  6.964e+07   <2e-16 ***
## data$property_typeHouse          3.660e+14  2.739e+06  1.336e+08   <2e-16 ***
## data$property_typeLower Portion -4.624e+14  2.827e+06 -1.636e+08   <2e-16 ***
## data$property_typePenthouse     -9.449e+14  4.388e+06 -2.153e+08   <2e-16 ***
## data$property_typeRoom          -1.099e+15  3.774e+06 -2.911e+08   <2e-16 ***
## data$property_typeUpper Portion -3.249e+14  2.795e+06 -1.162e+08   <2e-16 ***
## data$price                       5.401e+07  5.084e-03  1.062e+10   <2e-16 ***
## data$baths                      -5.222e+13  9.157e+04 -5.702e+08   <2e-16 ***
## data$bedrooms                   -1.233e+14  1.220e+05 -1.011e+09   <2e-16 ***
## data$Area_in_Marla              -2.857e+12  1.843e+03 -1.551e+09   <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:  182373  on 153429  degrees of freedom
## Residual deviance: 1959910  on 153419  degrees of freedom
## AIC: 1959932
## 
## Number of Fisher Scoring iterations: 25

4.3 Hồi quy với hàm cloglog

log <- glm(factor (purpose) ~ data$property_type + data$price + data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "cloglog"), data = data)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(log)
## 
## Call:
## glm(formula = factor(purpose) ~ data$property_type + data$price + 
##     data$baths + data$bedrooms + data$Area_in_Marla, family = binomial(link = "cloglog"), 
##     data = data)
## 
## Coefficients:
##                                   Estimate Std. Error    z value Pr(>|z|)    
## (Intercept)                      1.014e+14  2.733e+06  3.711e+07   <2e-16 ***
## data$property_typeFlat           3.185e+14  2.752e+06  1.157e+08   <2e-16 ***
## data$property_typeHouse         -9.156e+13  2.739e+06 -3.342e+07   <2e-16 ***
## data$property_typeLower Portion -6.761e+14  2.827e+06 -2.392e+08   <2e-16 ***
## data$property_typePenthouse      4.042e+14  4.388e+06  9.213e+07   <2e-16 ***
## data$property_typeRoom          -2.253e+14  3.774e+06 -5.971e+07   <2e-16 ***
## data$property_typeUpper Portion -1.774e+15  2.795e+06 -6.346e+08   <2e-16 ***
## data$price                       4.226e+07  5.084e-03  8.311e+09   <2e-16 ***
## data$baths                      -2.894e+13  9.157e+04 -3.160e+08   <2e-16 ***
## data$bedrooms                   -4.268e+13  1.220e+05 -3.498e+08   <2e-16 ***
## data$Area_in_Marla              -1.871e+12  1.843e+03 -1.015e+09   <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:  182373  on 153429  degrees of freedom
## Residual deviance: 1345942  on 153419  degrees of freedom
## AIC: 1345964
## 
## Number of Fisher Scoring iterations: 25

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

5.1 Tiêu chí AIC

AIC(logit) = 1385973

AIC(probit) = 1959932

AIC(cloglog) = 1345964

Mô hình cloglog có AIC nhỏ nhất nên ta chọn mô hình cloglog

5.2 Deviance

Deviance(logit) = 1385951

Deviance(probit) = 1959910

Deviance(cloglog) = 1345942

Mô hình cloglog có deviance nhỏ nhất nên ta chọn mô hình cloglog

5.3 Brier Score

library(DescTools)
BrierScore(logit)
## [1] 0.125308
BrierScore(probit)
## [1] 0.1772013
BrierScore(log)
## [1] 0.1216907

Dựa vào tiêu chí Brier score ta thấy mô hình cloglog có giá trị nhỏ nhất nên ta chọn cloglog

5.4 Ma trận nhầm lẫn

5.4.1 Mô hình logit

library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following objects are masked from 'package:DescTools':
## 
##     MAE, RMSE
predictions <- predict(logit, newdata = data, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0")  
predictions1 <- factor(predicted_classes, levels = c("0","1"))
actual<- factor(data$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  33707   9750
##            1   9476 100497
##                                          
##                Accuracy : 0.8747         
##                  95% CI : (0.873, 0.8763)
##     No Information Rate : 0.7185         
##     P-Value [Acc > NIR] : < 2e-16        
##                                          
##                   Kappa : 0.6908         
##                                          
##  Mcnemar's Test P-Value : 0.04897        
##                                          
##             Sensitivity : 0.7806         
##             Specificity : 0.9116         
##          Pos Pred Value : 0.7756         
##          Neg Pred Value : 0.9138         
##              Prevalence : 0.2815         
##          Detection Rate : 0.2197         
##    Detection Prevalence : 0.2832         
##       Balanced Accuracy : 0.8461         
##                                          
##        'Positive' Class : 0              
## 

Mô hình logit có độ chính xác toàn thể là 87,47%, độ nhạy là 78.06% và độ hiệu quả là 91.16%

5.4.2 Mô hình probit

predictions <- predict(probit, newdata = data, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0") 
predictions1 <- factor(predicted_classes, levels = c("0","1"))
actual<- factor(data$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  16352    357
##            1  26831 109890
##                                           
##                Accuracy : 0.8228          
##                  95% CI : (0.8209, 0.8247)
##     No Information Rate : 0.7185          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.4615          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.3787          
##             Specificity : 0.9968          
##          Pos Pred Value : 0.9786          
##          Neg Pred Value : 0.8038          
##              Prevalence : 0.2815          
##          Detection Rate : 0.1066          
##    Detection Prevalence : 0.1089          
##       Balanced Accuracy : 0.6877          
##                                           
##        'Positive' Class : 0               
## 

Mô hình probit có độ chính xác toàn thể là 82.28%, độ nhạy là 37.87% và độ hiệu quả là 99.68%

5.4.3 Mô hình cloglog

predictions <- predict(log, newdata = data, type = "response")
predicted_classes <- ifelse(predictions > 0.5, "1", "0") 
predictions1 <- factor(predicted_classes, levels = c("0","1"))
actual<- factor(data$purpose, labels = c("0","1"))
confusionMatrix(table(predictions1, actual))
## Confusion Matrix and Statistics
## 
##             actual
## predictions1      0      1
##            0  32529   8017
##            1  10654 102230
##                                           
##                Accuracy : 0.8783          
##                  95% CI : (0.8767, 0.8799)
##     No Information Rate : 0.7185          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6934          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.7533          
##             Specificity : 0.9273          
##          Pos Pred Value : 0.8023          
##          Neg Pred Value : 0.9056          
##              Prevalence : 0.2815          
##          Detection Rate : 0.2120          
##    Detection Prevalence : 0.2643          
##       Balanced Accuracy : 0.8403          
##                                           
##        'Positive' Class : 0               
## 

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

MH cloglog có độ chính xác toàn thể cao nhất trong 3 mô hình nên chọn mô hình này.

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