1 Mô tả 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:

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

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à

Baths: số phòng tắm

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

Bedrooms: số phòng ngủ

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

Agency: hãng

Agent: đại lý

Area in Marla: khu vực ở Marla

Biến số thứ tự

library(readxl)
For_EDA_dataset_1 <- read_excel("phần mền r/For_EDA_dataset 1.xlsx")
## Warning: Expecting numeric in G3081 / R3081C7: got a date
## New names:
## • `` -> `...1`
d <- For_EDA_dataset_1
str(d)
## 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 ...

2 Thống kê mô tả cho purpose

Bảng tần số - bảng tần suất

table(d$purpose)
## 
## For Rent For Sale 
##    43183   110247
table(d$purpose)/sum(table(d$purpose))
## 
## For Rent For Sale 
##   0.2815   0.7185

Đồ Thị Cột

d  |> 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) + theme_classic() + labs(x = 'Mục đích mua nhà', y = 'Số người')

Quan sát từ bảng và đồ thị có thể thấy có 43.183 (chiếm 28.25%) bất động sản được sử dụng với mục đích cho thuê và 110.247 (chiếm 71,85%) bất động sản được sử dụng với mục đích để bán. Tỉ lệ này chênh lệch 2,55 lần

3 Ước lượng tỉ lệ giá trị tỷ lệ

nha <- mapvalues(d$purpose, from=c('For Rent','For Sale'), to=c('1', '0'))
k  <- length(nha)
f1 <- length(nha[nha<0.5])

prop.test(n=k,x=f1)
## 
##  1-sample proportions test with continuity correction
## 
## data:  f1 out of k, null probability 0.5
## X-squared = 29313, df = 1, p-value <2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.7163 0.7208
## sample estimates:
##      p 
## 0.7185

Ta được khoảng ước lượng tỷ lệ những người “mua nhà để ở” với độ tin cậy 95% là khoảng (0.7163; 0.7208)

f2 <- length(nha[nha>0.5])
prop.test(x= f2, n= k)
## 
##  1-sample proportions test with continuity correction
## 
## data:  f2 out of k, null probability 0.5
## X-squared = 29313, df = 1, p-value <2e-16
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.2792 0.2837
## sample estimates:
##      p 
## 0.2815

Khoảng ước lượng tỷ lệ những người “Mua nhà để bán lại” với độ tin cậy 95% là khoảng (0.2792, 0.2837)

4 Mô hình hồi quy

4.1 Hàm liên kết cho dữ liệu nhị phân: logit

levels(d$purpose)
## NULL
fit <- glm( factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + property_type, family = binomial(link = 'logit'), data = d )
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fit)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + 
##     property_type, family = binomial(link = "logit"), data = d)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49    0.00    0.00    0.00    8.49  
## 
## Coefficients:
##                             Estimate Std. Error   z value Pr(>|z|)    
## (Intercept)                 4.80e+13   2.73e+06  1.76e+07   <2e-16 ***
## price                       6.58e+07   5.08e-03  1.29e+10   <2e-16 ***
## baths                      -4.01e+13   9.16e+04 -4.38e+08   <2e-16 ***
## bedrooms                    6.75e+13   1.22e+05  5.54e+08   <2e-16 ***
## Area_in_Marla              -2.98e+12   1.84e+03 -1.62e+09   <2e-16 ***
## property_typeFlat           1.56e+15   2.75e+06  5.65e+08   <2e-16 ***
## property_typeHouse         -5.18e+14   2.74e+06 -1.89e+08   <2e-16 ***
## property_typeLower Portion -3.93e+14   2.83e+06 -1.39e+08   <2e-16 ***
## property_typePenthouse     -4.68e+14   4.39e+06 -1.07e+08   <2e-16 ***
## property_typeRoom          -1.28e+15   3.77e+06 -3.38e+08   <2e-16 ***
## property_typeUpper Portion -2.95e+14   2.80e+06 -1.06e+08   <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
BrierScore(fit)
## [1] 0.1253
confusionMatrix(table(predict(fit, type="response") >= 0.5 , fit$data$purpose == 'For Sale'))
## Confusion Matrix and Statistics
## 
##        
##          FALSE   TRUE
##   FALSE  33707   9750
##   TRUE    9476 100497
##                                         
##                Accuracy : 0.875         
##                  95% CI : (0.873, 0.876)
##     No Information Rate : 0.719         
##     P-Value [Acc > NIR] : <2e-16        
##                                         
##                   Kappa : 0.691         
##                                         
##  Mcnemar's Test P-Value : 0.049         
##                                         
##             Sensitivity : 0.781         
##             Specificity : 0.912         
##          Pos Pred Value : 0.776         
##          Neg Pred Value : 0.914         
##              Prevalence : 0.281         
##          Detection Rate : 0.220         
##    Detection Prevalence : 0.283         
##       Balanced Accuracy : 0.846         
##                                         
##        'Positive' Class : FALSE         
## 

Tại đây em đã tiến hành phân tích dữ liệu nhị phân với loại hàm liên kết là logitta có các phương trình: \(logit(\pi)= log(\frac{\pi}{1-\pi}) = -0.732 +0.083x +1.353y353y\)

AIC: 1385973

Deviance: 1385951

Brier Score: 0,1253

Độ chính xác theo ma trận nhầm lẫn: 0,875

4.2 Hàm liên kết cho dữ liệu nhị phân: probit

fit1 <- glm( factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + property_type, family = binomial(link = 'probit'), data = d )
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fit1)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + 
##     property_type, family = binomial(link = "probit"), data = d)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49    0.00    0.00    0.00    8.49  
## 
## Coefficients:
##                             Estimate Std. Error   z value Pr(>|z|)    
## (Intercept)                 7.01e+14   2.73e+06  2.57e+08   <2e-16 ***
## price                       5.40e+07   5.08e-03  1.06e+10   <2e-16 ***
## baths                      -5.22e+13   9.16e+04 -5.70e+08   <2e-16 ***
## bedrooms                   -1.22e+14   1.22e+05 -1.00e+09   <2e-16 ***
## Area_in_Marla              -2.86e+12   1.84e+03 -1.55e+09   <2e-16 ***
## property_typeFlat           1.82e+14   2.75e+06  6.61e+07   <2e-16 ***
## property_typeHouse          3.53e+14   2.74e+06  1.29e+08   <2e-16 ***
## property_typeLower Portion -4.69e+14   2.83e+06 -1.66e+08   <2e-16 ***
## property_typePenthouse     -9.52e+14   4.39e+06 -2.17e+08   <2e-16 ***
## property_typeRoom          -3.87e+15   3.77e+06 -1.03e+09   <2e-16 ***
## property_typeUpper Portion -3.31e+14   2.80e+06 -1.18e+08   <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: 1961424  on 153419  degrees of freedom
## AIC: 1961446
## 
## Number of Fisher Scoring iterations: 25
BrierScore(fit1)
## [1] 0.1773
confusionMatrix(table(predict(fit, type="response") >= 0.5 , fit1$data$purpose == 'For Sale'))
## Confusion Matrix and Statistics
## 
##        
##          FALSE   TRUE
##   FALSE  33707   9750
##   TRUE    9476 100497
##                                         
##                Accuracy : 0.875         
##                  95% CI : (0.873, 0.876)
##     No Information Rate : 0.719         
##     P-Value [Acc > NIR] : <2e-16        
##                                         
##                   Kappa : 0.691         
##                                         
##  Mcnemar's Test P-Value : 0.049         
##                                         
##             Sensitivity : 0.781         
##             Specificity : 0.912         
##          Pos Pred Value : 0.776         
##          Neg Pred Value : 0.914         
##              Prevalence : 0.281         
##          Detection Rate : 0.220         
##    Detection Prevalence : 0.283         
##       Balanced Accuracy : 0.846         
##                                         
##        'Positive' Class : FALSE         
## 

AIC: 1961446

Deviance: 1961424

Brier Score: 0,1773

Độ chính xác theo ma trận nhầm lẫn: 0,875

4.3 Hàm liên kết cho dữ liệu nhị phân: cloglog

fit2 <- glm( factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + property_type, family = binomial(link = 'cloglog'), data = d )
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fit2)
## 
## Call:
## glm(formula = factor(purpose) ~ price + baths + bedrooms + Area_in_Marla + 
##     property_type, family = binomial(link = "cloglog"), data = d)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
##  -8.49    0.00    0.00    0.00    8.49  
## 
## Coefficients:
##                             Estimate Std. Error   z value Pr(>|z|)    
## (Intercept)                 1.01e+14   2.73e+06  3.71e+07   <2e-16 ***
## price                       4.23e+07   5.08e-03  8.31e+09   <2e-16 ***
## baths                      -2.89e+13   9.16e+04 -3.16e+08   <2e-16 ***
## bedrooms                   -4.27e+13   1.22e+05 -3.50e+08   <2e-16 ***
## Area_in_Marla              -1.87e+12   1.84e+03 -1.02e+09   <2e-16 ***
## property_typeFlat           3.19e+14   2.75e+06  1.16e+08   <2e-16 ***
## property_typeHouse         -9.16e+13   2.74e+06 -3.34e+07   <2e-16 ***
## property_typeLower Portion -6.76e+14   2.83e+06 -2.39e+08   <2e-16 ***
## property_typePenthouse      4.04e+14   4.39e+06  9.21e+07   <2e-16 ***
## property_typeRoom          -2.25e+14   3.77e+06 -5.97e+07   <2e-16 ***
## property_typeUpper Portion -1.77e+15   2.80e+06 -6.35e+08   <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
BrierScore(fit2)
## [1] 0.1217
confusionMatrix(table(predict(fit2, type="response") >= 0.5 , fit2$data$purpose == 'For Sale'))
## Confusion Matrix and Statistics
## 
##        
##          FALSE   TRUE
##   FALSE  32529   8017
##   TRUE   10654 102230
##                                        
##                Accuracy : 0.878        
##                  95% CI : (0.877, 0.88)
##     No Information Rate : 0.719        
##     P-Value [Acc > NIR] : <2e-16       
##                                        
##                   Kappa : 0.693        
##                                        
##  Mcnemar's Test P-Value : <2e-16       
##                                        
##             Sensitivity : 0.753        
##             Specificity : 0.927        
##          Pos Pred Value : 0.802        
##          Neg Pred Value : 0.906        
##              Prevalence : 0.281        
##          Detection Rate : 0.212        
##    Detection Prevalence : 0.264        
##       Balanced Accuracy : 0.840        
##                                        
##        'Positive' Class : FALSE        
## 

AIC: 1345964

Deviance: 1345942

Brier Score:0.1217

Độ chính xác theo ma trận nhầm lẫn: 0,878

4.4 Đánh giá

Các phân tích dữ liệu nhị phân theo 3 loại hàm liên kết là logit, probit, cloglog ta có các chỉ số đánh giá như sau:

logit probit cloglog
AIC 1385973 1961446 1345964
deviance 1385951 1961424 1345942
Brier Score 0.1253 0.1773 0.1217
Matrix 0.875 0.875 0.878

Nhận xét: cloglog là hàm liên kết là tốt nhất. Vì các chỉ số đánh giá nhỏ nhất và ma trận nhầm lẫn ta có thể nhận thấy mô hình hoạt động với độ chính xác lớn nhất là 87,8%.