setwd("D:/UEH/Kinh Te Luong/Huong dan BT NHOM KTL 2019 hk3")
source("functions.R")
import_library("readxl")
import_library("gdata")
import_library("stringr")
import_library("dplyr")
import_library("foreign")
import_library("glue")
import_library("magrittr")
import_library("ggplot2")
import_library("ggfortify")
import_library("AER")
import_library("PerformanceAnalytics")
import_library("leaps")
import_library("olsrr")
import_library("car")
import_library("magick")
import_library("fBasics")
import_library("lmtest")
import_library("corrplot")
import_library("BMA")
khaosat <- read_excel("khaosat.xlsx")
attach(khaosat)
dim(khaosat)## [1] 125 15
## [1] "square" "nupeinro" "isCenter" "isGoodSecuri"
## [5] "isGoodQuality" "eprice" "wprice" "isTownhouse"
## [9] "isLimitTime" "isAvaifurni" "isPrivateToilet" "isAcquainHost"
## [13] "isParking" "isHwinbal"
Các giá trị trong bảng thể hiện mối quan hệ giữa 2 biến. Càng gần về 1 thì 2 biến được xem là có mối tương quan hoang toàn
Các giá trị trong hàm Corrplot biểu thị mức độ tương quan tuyến tính giữa các biến khác nhau. Nếu giá trị bị mờ dần, nó biểu thị rằng có mối quan hệ tuyến tính tối thiểu giữa các biến.
Dùng để xuất ra file đồ thị chất lượng cao
png(height=1200, width=1500, pointsize=15, file=“overlap.png”)
full <- regsubsets(price~., data = khaosat, nvmax = 14)
smfull = summary(full)
full_model = lm(price ~ ., data = khaosat)
print(summary(full_model))##
## Call:
## lm(formula = price ~ ., data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8465 -1.1251 -0.2677 0.7548 8.2068
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.024828 1.820849 -0.563 0.5747
## square 0.068330 0.015692 4.355 3.00e-05 ***
## nupeinro 0.755771 0.161792 4.671 8.52e-06 ***
## isCenter 0.645374 0.477718 1.351 0.1795
## isGoodSecuri 0.314560 0.516968 0.608 0.5441
## isGoodQuality -0.471288 0.496449 -0.949 0.3445
## eprice 0.469690 0.319236 1.471 0.1441
## wprice 0.009084 0.048718 0.186 0.8524
## isTownhouse 0.255977 0.585482 0.437 0.6628
## isLimitTime -1.136639 0.493470 -2.303 0.0231 *
## isAvaifurni 0.727710 0.535891 1.358 0.1773
## isPrivateToilet 0.131667 0.664956 0.198 0.8434
## isAcquainHost 0.040483 0.603812 0.067 0.9467
## isParking 0.097440 0.749906 0.130 0.8969
## isHwinbal -1.286280 0.571378 -2.251 0.0264 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.365 on 110 degrees of freedom
## Multiple R-squared: 0.5252, Adjusted R-squared: 0.4648
## F-statistic: 8.691 on 14 and 110 DF, p-value: 2.08e-12
## [1] "which" "rsq" "rss" "adjr2" "cp" "bic" "outmat" "obj"
Ở đây chúng ta thấy có những tiêu chí quen thuộc như \({R^2}\) (kí hiệu rsq), \({\bar R^2}\), BIC (kí hiệu bic). Giả sử chúng ta chọn bốn tiêu chí là \({R^2}\), \({\bar R^2}\), BIC, Cp và RSS:
c6_r2 <- smfull$rsq
c6_adjr2 <- smfull$adjr2
c6_bic <- smfull$bic
c6_cp <- smfull$cp
c6_rss <- smfull$rss
mininumVars <- c(
BIC = which.min(c6_bic),
CP = which.min(c6_cp)
)
maximumVars <- c(
R2 = which.max(c6_r2),
AdjR2 = which.max(c6_adjr2)
)Bảng sau thể hiện số lượng biến trong mô hình cho Cp và BIC thấp nhất
## BIC CP
## 3 6
Bảng sau thể hiện số lượng biến trong mô hìnhc có \({R^2}\) và \({\bar R^2}\) lớn nhất
## R2 AdjR2
## 14 7
Chúng ta có thể đánh giá các tiêu chuẩn lựa chọn mô hình thay đổi ra sao với các biến số trong mô hình bằng hình ảnh sau:
par(mfrow=c(2, 3))
par(bg = "white")
plot(c6_r2, xlab = "So luong bien", ylab = "R2", type = "b", pch = 16)
points(which.max(c6_r2), c6_r2[which.max(c6_r2)], col = "red", cex = 2, pch = 20)
plot(c6_adjr2, xlab = "So luong bien", ylab = "AdjR2", type = "b", pch = 16)
points(which.max(c6_adjr2), c6_adjr2[which.max(c6_adjr2)], col = "red", cex = 2, pch = 20)
plot(c6_rss, xlab = "So luong bien", ylab = "RSS", type="b", pch = 16)
plot(c6_bic, xlab = "So luong bien", ylab = "BIC", type = "b", pch = 16)
points(which.min(c6_bic), c6_bic[which.min(c6_bic)], col = "red", cex = 2, pch = 20)
plot(c6_cp, xlab = "So luong bien", ylab = "Mallows' Cp", type = "b", pch = 16)
points(which.min(c6_cp), c6_cp[which.min(c6_cp)], col = "red", cex = 2, pch = 20) Chúng ta thấy rằng với R2 chẳng hạn, khi số lượng biến tăng thì tiêu chí này cũng tăng nhưng với tốc độ chậm dần. Đến ngưỡng 9 biến số thì gia tăng thêm biến số gần như không dẫn đến tăng tiêu chí này một cách dáng kể (đồ thị gần như là một đường thẳng). Điều này ngụ ý rằng nếu lấy mục tiêu là tối đa hóa \({R^2}\) nhưng lại đảm bảo số lượng biến tối thiểu thì nên chọn mô hình có 9 biến số. Chúng ta có thể biết mô hình là những biến số nào cùng với hệ số hồi quy của chúng:
## (Intercept) square nupeinro isCenter isGoodSecuri
## -0.75843130 0.06821769 0.75937871 0.60227853 0.30019087
## isGoodQuality eprice isLimitTime isAvaifurni isHwinbal
## -0.43633917 0.47082354 -1.08444688 0.77063488 -1.26054274
Chi tiết tiêu chí \({R^2}\)
Biểu đồ
par(mfrow=c(1, 1))
plot(c6_r2, xlab = "So luong bien", ylab = "R2", type = "b", pch = 16)
points(which.max(c6_r2), c6_r2[which.max(c6_r2)], col = "red", cex = 2, pch = 20)Tương tự nếu chúng ta chọn mô hình có BIC thấp nhất thì mô hình ấy sẽ có 3 biến số. Cụ thể các biến ấy cùng hệ số hồi quy của chúng là:
## (Intercept) square nupeinro isHwinbal
## 0.56133931 0.06922291 0.86731274 -1.22713356
Nếu chúng ta chọn mô hình có Cp thấp nhất thì mô hình ấy sẽ có 6 biến số. Cụ thể là:
## (Intercept) square nupeinro eprice isLimitTime isAvaifurni
## -0.55678450 0.06434177 0.80654550 0.48833701 -1.07264881 0.76692623
## isHwinbal
## -1.39019198
Nếu chọn tiêu chí Cp, chúng ta có thể phân tích chi tiết hơn như sau:
Như đã phân tích ở trên ta thấy được 6 biến có Cp thấp nhất. Và đây cũng là tiêu chí mà tôi sử dụng để đánh giá mô hình cũng nhu ra quyết định chọn và bỏ biến nào.
Thông qua các số liệu trên các biến được giữ lại là: square, nupeinro, eprice, isLimitTime, isAvaifurni với kết quả hệ số hồi quy được thể hiện ở mục 6.6
Các biến còn lại được lược bỏ. Dựa theo tiêu chí đánh giá Cp của Mallows
Nếu chúng ta chọn mô hinh có \({\bar R^2}\) lớn nhất thì mô hình ấy sẽ có 7 biến số Cụ thể là:
## (Intercept) square nupeinro isCenter eprice isLimitTime
## -0.76605594 0.06692645 0.74822784 0.61165648 0.48888739 -1.05224386
## isAvaifurni isHwinbal
## 0.70951312 -1.30048885
Phân tích chi tiết tiêu chí \({\bar R^2}\)
Đồ thị thể hiện sự biến thiên số lượng biến của mô hình theo tiêu chí AdjR2
par(mfrow=c(1, 1))
plot(c6_adjr2, xlab = "So luong bien", ylab = "AdjR2", type = "b", pch = 16)
points(which.max(c6_adjr2), c6_adjr2[which.max(c6_adjr2)], col = "red", cex = 2, pch = 20)Dùng BIC để làm tiêu chuẩn chọn mô hình tốt nhất
Dùng nhiều công xuất máy tính (Vài nghìn biến mới dùng nhiều thôi. Như của mình chỉ có 15 biến thì ezz)
Ở đây, chúng ta không giải thích chi tiết về phương pháp này. Và xem như các bạn đã biết về nó.
Mục đích của câu này là loại bỏ biến nào.
Nói chung quy là chúng ta sẽ chọn mô hình gồm nhưng biến nào. Chính xác là tìm mô hình tối ưu nhất
Thật ra không có mô hình “tốt nhất” mà chỉ có mô hình tối ưu nhất mà thôi
Tối ưu = Ít tham số + Giải thích dữ liệu nhiều
Ở đây chúng ta dùng package “BMA”
Đầu tiên chúng ta định nghĩa đối tượng BMA_X là tập hợp những biến giải thích
Chúng ta có thể dùng lệnh cbind(square, nupeinro, isCenter, isGoodSecuri, isGoodQuality, eprice, wprice, isTownhouse, isLimitTime, isAvaifurni, isPrivateToilet, isAcquainHost, isParking, isHwinbal)
Nhưng ở đây tôi dùng khaosat[, -1] để cho nhanh và cũng gọn gàng hơn.
Tiếp đến là định nghĩa đối tương BMA_Y là biến phụ thuộc. Tức là biến “price”
Tiến hành chạy BMA
##
## Call:
## bicreg(x = BMA_X, y = BMA_Y, strict = FALSE, OR = 20)
##
##
## 47 models were selected
## Best 5 models (cumulative posterior probability = 0.3858 ):
##
## p!=0 EV SD model 1 model 2 model 3
## Intercept 100.0 -9.393e-02 1.342527 0.56134 -0.41076 1.05307
## square 100.0 6.675e-02 0.015350 0.06922 0.06353 0.06746
## nupeinro 100.0 8.612e-01 0.156185 0.86731 0.91942 0.85019
## isCenter 18.4 1.337e-01 0.343574 . . .
## isGoodSecuri 2.7 1.251e-03 0.079529 . . .
## isGoodQuality 4.4 -1.645e-02 0.121893 . . .
## eprice 23.5 1.277e-01 0.275164 . . .
## wprice 2.7 9.842e-05 0.008002 . . .
## isTownhouse 2.8 1.397e-03 0.097161 . . .
## isLimitTime 36.4 -3.312e-01 0.520956 . . -0.89487
## isAvaifurni 15.5 1.194e-01 0.344461 . . .
## isPrivateToilet 2.7 1.089e-03 0.106133 . . .
## isAcquainHost 2.8 -3.661e-03 0.097301 . . .
## isParking 2.7 -1.795e-03 0.121278 . . .
## isHwinbal 63.7 -8.127e-01 0.749205 -1.22713 . -1.36788
##
## nVar 3 2 4
## r2 0.472 0.448 0.488
## BIC -65.26697 -64.59437 -64.34605
## post prob 0.127 0.091 0.080
## model 4 model 5
## Intercept -2.39308 -0.97374
## square 0.06497 0.06805
## nupeinro 0.92912 0.86348
## isCenter . .
## isGoodSecuri . .
## isGoodQuality . .
## eprice 0.56237 0.56219
## wprice . .
## isTownhouse . .
## isLimitTime . -1.00915
## isAvaifurni . .
## isPrivateToilet . .
## isAcquainHost . .
## isParking . .
## isHwinbal . -1.24960
##
## nVar 3 5
## r2 0.463 0.502
## BIC -63.21609 -63.09825
## post prob 0.045 0.043
Như ta thấy hàm bicreg chọn cho chúng ta 5 mô hình.
Và 5 mô hình này được chọn lựa vào xác xuất hậu định
Cột 1 bao gồm các biến giải thích
Cột 2 (p!=0) là xác suất hệ số hồi quy của mỗi biến giải thích ở cột 1 khác 0. Đây là kết quả của việc máy tính tính toán rất nhiều các mô hình để đưa đến kết quả này.
Cột 3 (EV) là Giá trị kỳ vọng
Cột 4 (SD) là Độ lệch chuẩn
Ở kết quả trên. Chúng ta thấy mình có đến 5 lựa chọn. Một điều mà tôi rất ưa thích ở phương pháp này.
Thay vì chỉ có 1 mộ hình để chúng ta lựa chọn thì bây giờ chúng ta có đến 5 lựa chọn so với các phương pháp cổ điển
Vì ở đời thì làm gì chỉ có 1 sự lựa chọn :))
Như chúng ta thấy thì ở “Mô hình 1” (model 1) chúng thấy chỉ với 3 biến square, nupeinro, isHwinbal chúng ta đã có thể giải thích được khoảng 47.2%. Trong khi model 5 giải thích được tận 50.2% nhưng tần suất xuất hiện của mô hình này chỉ có 4.3%, ít gấp 3 lần so với tần suất xuất hiện của Mô hình 1 vì thế lựa chọn Mô hình 1 là một sự lựa chọn tối ưu nhất ở trường hợp này. Các bạn có thể thử phân tích và so sánh tương tự ở các cặp mô hình khác.
Sau đây là một biểu đồ thể hiện trực quan phương pháp của chúng ta Biểu đồ này là một cách để nó thể hiện xác suất của biến đó xuất hiện trong mô hình hồi quy tuyến tính đa biến
Ở đây có 2 màu thể hiện dấu của hệ số hồi quy “xanh” thể hiện cho dấu âm (-) và màu “Đỏ” thể hiện cho dấu dương (+)
Vậy cuối cùng chúng ta kết luận mô hình tối ưu nhất mà chung ta sẽ chọn là Mô hình 1 \[price = 0.56134\; + \;0.06922square\; + \;0.86731nupeinro\; - \;1.22713isHwinbal\]
Các biến độc lập trong mô hình trên giải thích được 47.2% cho biến phụ thuộc price
Với tần suất xuất hiện là 12.7%
À chờ chút kiểm định phát cuối xem thử mấy biến isGoodSecuri, isGoodQuality, wprice, isTownhouse, isPrivateToilet, isAcquainHost, isParking
Xem thử có ý nghĩa thống kê đồng thời hay không.
linearHypothesis(full_model, c(
"isGoodSecuri=0",
"isGoodQuality=0",
"wprice=0",
"isTownhouse=0",
"isPrivateToilet=0",
"isAcquainHost=0",
"isParking=0"
))## Linear hypothesis test
##
## Hypothesis:
## isGoodSecuri = 0
## isGoodQuality = 0
## wprice = 0
## isTownhouse = 0
## isPrivateToilet = 0
## isAcquainHost = 0
## isParking = 0
##
## Model 1: restricted model
## Model 2: price ~ square + nupeinro + isCenter + isGoodSecuri + isGoodQuality +
## eprice + wprice + isTownhouse + isLimitTime + isAvaifurni +
## isPrivateToilet + isAcquainHost + isParking + isHwinbal
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 117 622.14
## 2 110 615.27 7 6.8673 0.1754 0.9898
Ơn giời. Bác bỏ \({H_0}:\)Các biến trên đồng thời = 0. Với mức ý nghĩa 5%
RR_result <- c()
RR_result_string <- c()
for (var in vars) {
query <- as.formula(paste("price", paste(var), sep = " ~ "))
print(paste('query = ', paste("price", paste(var), sep = " ~ ")))
result_1 <- summary(lm(query, data=khaosat))
flow_RR <- result_1$r.squared
RR_result_string <- c(RR_result_string, paste(var, ":",toString(flow_RR), sep=" "))
RR_result <- c(RR_result, flow_RR)
print(result_1)
glue("RR: {flow_RR}")
print("##########################################################")
}## [1] "query = price ~ square"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.0754 -1.3260 -0.5199 0.1463 13.6463
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.24291 0.46165 2.692 0.00809 **
## square 0.10554 0.01545 6.832 3.43e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.764 on 123 degrees of freedom
## Multiple R-squared: 0.2751, Adjusted R-squared: 0.2692
## F-statistic: 46.68 on 1 and 123 DF, p-value: 3.428e-10
##
## [1] "##########################################################"
## [1] "query = price ~ nupeinro"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.1613 -1.2648 -0.3628 0.4346 12.8362
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.3659 0.4776 0.766 0.445
## nupeinro 1.1995 0.1416 8.468 6.29e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.58 on 123 degrees of freedom
## Multiple R-squared: 0.3683, Adjusted R-squared: 0.3632
## F-statistic: 71.71 on 1 and 123 DF, p-value: 6.289e-14
##
## [1] "##########################################################"
## [1] "query = price ~ isCenter"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6739 -1.7178 -0.8739 0.7822 13.1261
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.2178 0.3675 8.756 1.31e-14 ***
## isCenter 1.6561 0.5698 2.907 0.00433 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.14 on 123 degrees of freedom
## Multiple R-squared: 0.06428, Adjusted R-squared: 0.05667
## F-statistic: 8.449 on 1 and 123 DF, p-value: 0.004333
##
## [1] "##########################################################"
## [1] "query = price ~ isGoodSecuri"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.9033 -1.8033 -1.0033 0.1967 14.1967
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.1837 0.5559 7.526 9.6e-12 ***
## isGoodSecuri -0.3804 0.6515 -0.584 0.56
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.241 on 123 degrees of freedom
## Multiple R-squared: 0.002764, Adjusted R-squared: -0.005344
## F-statistic: 0.3409 on 1 and 123 DF, p-value: 0.5604
##
## [1] "##########################################################"
## [1] "query = price ~ isGoodQuality"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8449 -1.7449 -1.0111 -0.0111 14.2551
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.7449 0.4633 8.083 5.02e-13 ***
## isGoodQuality 0.2662 0.5942 0.448 0.655
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.243 on 123 degrees of freedom
## Multiple R-squared: 0.001629, Adjusted R-squared: -0.006487
## F-statistic: 0.2007 on 1 and 123 DF, p-value: 0.6549
##
## [1] "##########################################################"
## [1] "query = price ~ eprice"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0347 -1.7424 -0.9809 0.2191 14.0653
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.8578 1.4314 1.996 0.0481 *
## eprice 0.3077 0.4112 0.748 0.4557
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.238 on 123 degrees of freedom
## Multiple R-squared: 0.004532, Adjusted R-squared: -0.003561
## F-statistic: 0.56 on 1 and 123 DF, p-value: 0.4557
##
## [1] "##########################################################"
## [1] "query = price ~ wprice"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.961 -1.843 -0.937 0.064 13.975
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.08523 0.53482 7.638 5.32e-12 ***
## wprice -0.02488 0.06262 -0.397 0.692
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.244 on 123 degrees of freedom
## Multiple R-squared: 0.001281, Adjusted R-squared: -0.006838
## F-statistic: 0.1578 on 1 and 123 DF, p-value: 0.6919
##
## [1] "##########################################################"
## [1] "query = price ~ isTownhouse"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.97 -1.87 -0.87 0.13 14.13
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.8700 0.3213 12.045 <2e-16 ***
## isTownhouse 0.1995 0.7490 0.266 0.79
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.245 on 123 degrees of freedom
## Multiple R-squared: 0.0005765, Adjusted R-squared: -0.007549
## F-statistic: 0.07095 on 1 and 123 DF, p-value: 0.7904
##
## [1] "##########################################################"
## [1] "query = price ~ isLimitTime"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4029 -1.8029 -1.0650 0.6971 13.6971
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.3029 0.3463 12.424 <2e-16 ***
## isLimitTime -1.2379 0.6123 -2.022 0.0454 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.193 on 123 degrees of freedom
## Multiple R-squared: 0.03216, Adjusted R-squared: 0.0243
## F-statistic: 4.088 on 1 and 123 DF, p-value: 0.04537
##
## [1] "##########################################################"
## [1] "query = price ~ isAvaifurni"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.9378 -1.4378 -0.7630 0.5622 14.7370
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.2630 0.3291 9.914 < 2e-16 ***
## isAvaifurni 2.1748 0.6050 3.595 0.000468 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.088 on 123 degrees of freedom
## Multiple R-squared: 0.09508, Adjusted R-squared: 0.08772
## F-statistic: 12.92 on 1 and 123 DF, p-value: 0.0004681
##
## [1] "##########################################################"
## [1] "query = price ~ isPrivateToilet"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.0892 -1.6892 -0.9892 0.0108 14.0833
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.4167 0.7636 4.475 1.72e-05 ***
## isPrivateToilet 0.5725 0.8253 0.694 0.489
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.239 on 123 degrees of freedom
## Multiple R-squared: 0.003898, Adjusted R-squared: -0.004201
## F-statistic: 0.4813 on 1 and 123 DF, p-value: 0.4891
##
## [1] "##########################################################"
## [1] "query = price ~ isAcquainHost"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1034 -1.6034 -1.0034 -0.0034 13.9966
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.0034 0.3207 12.482 <2e-16 ***
## isAcquainHost -0.5251 0.7477 -0.702 0.484
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.239 on 123 degrees of freedom
## Multiple R-squared: 0.003994, Adjusted R-squared: -0.004104
## F-statistic: 0.4932 on 1 and 123 DF, p-value: 0.4838
##
## [1] "##########################################################"
## [1] "query = price ~ isParking"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.108 -1.896 -0.896 0.104 14.104
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.0083 0.9369 4.278 3.75e-05 ***
## isParking -0.1124 0.9854 -0.114 0.909
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.246 on 123 degrees of freedom
## Multiple R-squared: 0.0001057, Adjusted R-squared: -0.008024
## F-statistic: 0.013 on 1 and 123 DF, p-value: 0.9094
##
## [1] "##########################################################"
## [1] "query = price ~ isHwinbal"
##
## Call:
## lm(formula = query, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.4815 -1.6382 -0.8382 0.3618 14.3618
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.8815 0.6167 7.915 1.23e-12 ***
## isHwinbal -1.2433 0.6965 -1.785 0.0767 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.205 on 123 degrees of freedom
## Multiple R-squared: 0.02525, Adjusted R-squared: 0.01732
## F-statistic: 3.186 on 1 and 123 DF, p-value: 0.07673
##
## [1] "##########################################################"
## Mo hinh co R^2 lon nhat : price ~ nupeinro | value = 0.368301668982343
Tuyến tính, Lin Log, Hồi quy qua gốc tọa độ, Dạng hàm bậc 2
tuyentinh_model <- lm(price ~ nupeinro, data = khaosat)
standards$TuyenTinh <- initStd(tuyentinh_model, full_model)
print(summary(tuyentinh_model))##
## Call:
## lm(formula = price ~ nupeinro, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.1613 -1.2648 -0.3628 0.4346 12.8362
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.3659 0.4776 0.766 0.445
## nupeinro 1.1995 0.1416 8.468 6.29e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.58 on 123 degrees of freedom
## Multiple R-squared: 0.3683, Adjusted R-squared: 0.3632
## F-statistic: 71.71 on 1 and 123 DF, p-value: 6.289e-14
linLog_model <- lm(price ~ log(nupeinro), data = khaosat)
standards$LinLog <- initStd(linLog_model, full_model)
print(summary(linLog_model))##
## Call:
## lm(formula = price ~ log(nupeinro), data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.7693 -1.5976 -0.5976 0.6057 12.6857
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.8810 0.5124 1.719 0.0881 .
## log(nupeinro) 3.1980 0.4739 6.749 5.22e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.773 on 123 degrees of freedom
## Multiple R-squared: 0.2702, Adjusted R-squared: 0.2643
## F-statistic: 45.54 on 1 and 123 DF, p-value: 5.219e-10
Origin_Model <- lm(price ~ 0 + nupeinro, data = khaosat)
standards$GocToaDo <- initStd(Origin_Model, full_model)
print(summary(Origin_Model))##
## Call:
## lm(formula = price ~ 0 + nupeinro, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.6505 -1.1780 -0.2835 0.6165 12.8220
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## nupeinro 1.29450 0.06832 18.95 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.575 on 124 degrees of freedom
## Multiple R-squared: 0.7433, Adjusted R-squared: 0.7412
## F-statistic: 359 on 1 and 124 DF, p-value: < 2.2e-16
SQR_Model = lm(price ~ nupeinro + I(nupeinro^2), data = khaosat)
standards$HamBac2 <- initStd(SQR_Model, full_model)
print(summary(SQR_Model))##
## Call:
## lm(formula = price ~ nupeinro + I(nupeinro^2), data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.8579 -1.2206 -0.3836 0.2164 13.1967
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.52928 0.88907 1.720 0.088 .
## nupeinro 0.46632 0.49416 0.944 0.347
## I(nupeinro^2) 0.08805 0.05688 1.548 0.124
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.565 on 122 degrees of freedom
## Multiple R-squared: 0.3805, Adjusted R-squared: 0.3703
## F-statistic: 37.46 on 2 and 122 DF, p-value: 2.069e-13
## Standar TuyenTinh LinLog GocToaDo HamBac2
## 1 AIC 595.6418325 613.6832179 594.2368473 595.2107407
## 2 BIC 604.1267737 622.1681591 599.8934748 606.5239956
## 3 AR2 0.3631659 0.2642869 0.7411985 0.3703125
## 4 MCP 25.3481263 48.0710928 26.0464214 24.5293402
standard2 = data.frame(Standar2 = c("AIC", "BIC", "AR2", "MCP"))
LinLog_Model2 = lm(price ~ log(nupeinro), data = khaosat)
standard2$LinLog <- initStd(LinLog_Model2, full_model)
print(summary(LinLog_Model2))##
## Call:
## lm(formula = price ~ log(nupeinro), data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.7693 -1.5976 -0.5976 0.6057 12.6857
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.8810 0.5124 1.719 0.0881 .
## log(nupeinro) 3.1980 0.4739 6.749 5.22e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.773 on 123 degrees of freedom
## Multiple R-squared: 0.2702, Adjusted R-squared: 0.2643
## F-statistic: 45.54 on 1 and 123 DF, p-value: 5.219e-10
log_lin_model = lm(log(price) ~ nupeinro, data = khaosat)
standard2$LogLin <- initStd(log_lin_model, full_model)
print(summary(log_lin_model))##
## Call:
## lm(formula = log(price) ~ nupeinro, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.96332 -0.25007 -0.01808 0.21109 1.49485
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.47038 0.08524 5.518 1.93e-07 ***
## nupeinro 0.23199 0.02528 9.177 1.31e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4604 on 123 degrees of freedom
## Multiple R-squared: 0.4064, Adjusted R-squared: 0.4016
## F-statistic: 84.21 on 1 and 123 DF, p-value: 1.309e-15
## Standar2 LinLog LogLin
## 1 AIC 613.6832179 164.8265088
## 2 BIC 622.1681591 173.3114500
## 3 AR2 0.2642869 0.4015681
## 4 MCP 48.0710928 -116.3379017
3 biến định lượng là: square, nupeinro, eprice
2 biến định tính là : isLimitTime, isHwinbal
Chạy hồi quy \(price\) theo \(({\beta _1})\;square\), \(({\beta _2})\;nupeinro\), \(({\beta _3})\;eprice\), \(({\delta _1})\;isLimitTime\), \(({\delta _2})\;isHwinbal\)
Thực hiện các kiểm định
hoiquy_cau2 = lm(price ~ square + nupeinro + eprice + isLimitTime + isHwinbal, data = khaosat)
print(summary((hoiquy_cau2)))##
## Call:
## lm(formula = price ~ square + nupeinro + eprice + isLimitTime +
## isHwinbal, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3037 -1.0360 -0.3234 0.5833 8.6209
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.97374 1.27812 -0.762 0.4477
## square 0.06805 0.01479 4.602 1.05e-05 ***
## nupeinro 0.86348 0.14513 5.950 2.77e-08 ***
## eprice 0.56219 0.30230 1.860 0.0654 .
## isLimitTime -1.00915 0.45796 -2.204 0.0295 *
## isHwinbal -1.24960 0.52348 -2.387 0.0186 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.328 on 119 degrees of freedom
## Multiple R-squared: 0.5024, Adjusted R-squared: 0.4815
## F-statistic: 24.03 on 5 and 119 DF, p-value: < 2.2e-16
t_test(model, var, val)
model: Hoi quy lm()
var: = 0 neu la b0, = 1 neu la b1
val: Value of test
Kiểm định t
## t_VALUE p_VALUE_OF_t
## 4.602026e+00 1.054038e-05
## t_VALUE p_VALUE_OF_t
## -2.960191205 0.003711412
Kiểm định F
## Linear hypothesis test
##
## Hypothesis:
## isHwinbal = 0.3
##
## Model 1: restricted model
## Model 2: price ~ square + nupeinro + eprice + isLimitTime + isHwinbal
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 120 692.32
## 2 119 644.84 1 47.483 8.7627 0.003711 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Dùng kiểm định Wald
## Linear hypothesis test
##
## Hypothesis:
## square = 30
## nupeinro = 3
## isLimitTime = 1
##
## Model 1: restricted model
## Model 2: price ~ square + nupeinro + eprice + isLimitTime + isHwinbal
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 122 28425306
## 2 119 645 3 28424662 1748525 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Analysis of Variance Table
##
## Response: price
## Df Sum Sq Mean Sq F value Pr(>F)
## square 1 356.47 356.47 65.7848 5.125e-13 ***
## nupeinro 1 223.91 223.91 41.3219 2.792e-09 ***
## eprice 1 19.48 19.48 3.5944 0.06040 .
## isLimitTime 1 20.26 20.26 3.7390 0.05553 .
## isHwinbal 1 30.88 30.88 5.6983 0.01856 *
## Residuals 119 644.84 5.42
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
linearHypothesis(hoiquy_cau2, c(
"square=0",
"nupeinro=0",
"eprice=0",
"isLimitTime=0",
"isHwinbal=0"
))## Linear hypothesis test
##
## Hypothesis:
## square = 0
## nupeinro = 0
## eprice = 0
## isLimitTime = 0
## isHwinbal = 0
##
## Model 1: restricted model
## Model 2: price ~ square + nupeinro + eprice + isLimitTime + isHwinbal
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 124 1295.84
## 2 119 644.84 5 651 24.028 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Công thức tính khỏang tin cậy \[{\hat \beta _3} - {t_{\alpha /2}}se({\hat \beta _3}) \le {\beta _3} \le {\hat \beta _3} + {t_{\alpha /2}}se({\hat \beta _3})\]
## 2.5 % 97.5 %
## (Intercept) -3.50453611 1.55705571
## square 0.03876999 0.09732879
## nupeinro 0.57611673 1.15084386
## eprice -0.03639040 1.16076994
## isLimitTime -1.91595870 -0.10234355
## isHwinbal -2.28614178 -0.21305785
## MEAN_square MEAN_nupeinro MEAN_eprice
## 25.240000 2.952000 3.409088
square = 50, nupeinro = 4, eprice = 3.8, isLimitTime = 1, isHwinbal = 0
Với độ tin cậy 94%
predict(
hoiquy_cau2,
data.frame(
square = 50,
nupeinro = 4,
eprice = 3.8,
isLimitTime = 1,
isHwinbal = 0
),
interval ="confidence",
level = 0.94
)## fit lwr upr
## 1 7.00982 5.774781 8.24486
Chú ý rằng ngoài hệ số VIF như trên người ta còn lấy TOL = 1/VIF (nghịch đảo của VIF) là tiêu chí nhận định về hiện tượng đa cộng tuyến. Hiện tại, ngưỡng VIF (hay TOL, r23) bằng bao nhiêu để chỉ ra hiện tượng đa cộng tuyến vẫn là một vấn đề chưa có sự thống nhất giữa các nhà kinh tế lượng. Gujarati & Porter (2009) chỉ ra một số dấu hiệu của hiện tượng đa cộng tuyến trong mô hình khi: (1) hệ số VIF ≥ 10, hoặc (2) hệ số tương quan r của bất kì cặp biến nào trong mô hình lớn hơn 0.8. Trong khi đó Allison (1999) đưa ra tiêu chí chặt hơn khi chọn VIF > 2.5 (hay r > 0.775).
## square nupeinro eprice isLimitTime isHwinbal
## 1.291326 1.289283 1.046021 1.052744 1.070476
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 90.2546
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Nov 28 23:29:31 2019 by user: mrlua
hoiquy_cau3 <- lm(price ~ square + nupeinro + eprice + isLimitTime + isHwinbal + square:isHwinbal + nupeinro:isHwinbal, data = khaosat)
print(summary((hoiquy_cau3)))##
## Call:
## lm(formula = price ~ square + nupeinro + eprice + isLimitTime +
## isHwinbal + square:isHwinbal + nupeinro:isHwinbal, data = khaosat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.2349 -1.0254 -0.3078 0.5208 8.9511
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.72288 1.49227 -1.155 0.250633
## square 0.10582 0.02998 3.530 0.000595 ***
## nupeinro 0.78444 0.27160 2.888 0.004616 **
## eprice 0.59700 0.30356 1.967 0.051594 .
## isLimitTime -0.90523 0.46458 -1.948 0.053751 .
## isHwinbal -0.31870 1.06523 -0.299 0.765329
## square:isHwinbal -0.05036 0.03446 -1.462 0.146547
## nupeinro:isHwinbal 0.07919 0.32212 0.246 0.806243
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.323 on 117 degrees of freedom
## Multiple R-squared: 0.5127, Adjusted R-squared: 0.4835
## F-statistic: 17.58 on 7 and 117 DF, p-value: 9.054e-16
linearHypothesis(hoiquy_cau3, c(
"isLimitTime=0",
"isHwinbal=0",
"square:isHwinbal=0",
"nupeinro:isHwinbal=0"
))## Linear hypothesis test
##
## Hypothesis:
## isLimitTime = 0
## isHwinbal = 0
## square:isHwinbal = 0
## nupeinro:isHwinbal = 0
##
## Model 1: restricted model
## Model 2: price ~ square + nupeinro + eprice + isLimitTime + isHwinbal +
## square:isHwinbal + nupeinro:isHwinbal
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 121 695.97
## 2 117 631.47 4 64.5 2.9877 0.02171 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\[{H_0}:\;phương\;sai\;là\;không\;thay\;đổi;\;{H_1}:\;*phương sai\;là\;thay\;đổi\]
##
## studentized Breusch-Pagan test
##
## data: hoiquy_cau3
## BP = 38.509, df = 7, p-value = 2.424e-06
P-Value = 2.424e-06 < 0.05 : Bác bỏ \({H_0}\)
Vậy phương sai thay đổi
##
## studentized Breusch-Pagan test
##
## data: hoiquy_cau3
## BP = 21.532, df = 2, p-value = 2.111e-05
P-Value = 2.111e-05 < 0.05 : Bác bỏ \({H_0}\)
Vậy phương sai thay đổi
#FIX Data
khaosat$square.isHwinbal <- square*isHwinbal
khaosat$nupeinro.isHwinbal <- nupeinro*isHwinbal
#WLS
hqwls <- lm(
price ~ square + nupeinro
+ eprice + isLimitTime
+ isHwinbal + square:isHwinbal
+ nupeinro:isHwinbal,
weight = nupeinro,
data = khaosat
)
bptest(hqwls)##
## studentized Breusch-Pagan test
##
## data: hqwls
## BP = 38.509, df = 7, p-value = 2.424e-06
#GLS
mauGSL <- nupeinro
hqgls <- lm(
I(price/sqrt(mauGSL)) ~ 0
+ I(1/sqrt(mauGSL))
+ I(square/sqrt(mauGSL))
+ I(nupeinro/sqrt(mauGSL))
+ I(eprice/sqrt(mauGSL))
+ I(isLimitTime/sqrt(mauGSL))
+ I(isHwinbal/sqrt(mauGSL))
+ I(square.isHwinbal/sqrt(mauGSL))
+ I(nupeinro.isHwinbal/sqrt(mauGSL)),
data = khaosat
)
bptest(hqgls)##
## studentized Breusch-Pagan test
##
## data: hqgls
## BP = 13.717, df = 7, p-value = 0.05644
P-Value = 0.05644 > 0.05 Chúng ta có bằng chứng thống kê chấp nhận \({H_0}\). Nghĩa là mô hình có phương sai không đổi.
Để xác định mô hình có thiếu biến quan trọng hay không. \({H_0}:\;Mô\;hình\;có\;dạng\;hàm\;đúng\)
##
## RESET test
##
## data: hoiquy_cau3
## RESET = 26.377, df1 = 2, df2 = 115, p-value = 3.727e-10
##
## RESET test
##
## data: hoiquy_cau3
## RESET = 18.629, df1 = 3, df2 = 114, p-value = 6.663e-10