NỘI DUNG

1. Giới thiệu về bộ dữ liệu

2. Cài đặt cơ bản

2.1 Các gói lệnh cơ bản

2.2 Xử lí dữ liệu gốc

3. Thông kê mô tả

4. Các hàm plot cơ bản

5. Phân tích hồi quy



library(dplyr)
## 
## 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
setwd("/Users/duongthibeba/Documents/Giang day/HK1-24-25/Thong ke du bao/Data")
nyc_new<-read.csv("nyc_new.csv",header=TRUE)
nyc_train <- nyc_new %>% sample_frac(.7)
nyc_test  <- anti_join(nyc_new, nyc_train, by = 'id')

model_1 <- lm (price ~ neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + reviews_per_month + calculated_host_listings_count +
                  +  availability_365, data = nyc_train)
# Các chỉ số MSE, R^2, adj R^2 của model_1
summary_model_1<-summary(model_1)
mse_1 <- summary_model_1$sigma^2
r_sq_1 <- summary_model_1$r.squared
adj_r_sq_1 <- summary_model_1$adj.r.squared
mse_1
## [1] 2126.051
r_sq_1
## [1] 0.4948995
adj_r_sq_1
## [1] 0.4946442
library(leaps)

best_fit_model <- regsubsets (price ~ neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + 
                                reviews_per_month +calculated_host_listings_count + availability_365, data = nyc_train,  nbest = 2, nvmax = 9)
plot(best_fit_model, scale="bic")

Loại bỏ các biến trong mô hình làm cho chỉ số BIC cao là number_of_reviews, reviews_per_month, calculated_host_listings_count. Vậy mô hình hồi quy theo tập con bao gồm các biến neighbourhood_group latitude longitude minimum_nights room_type availablity_365 Mô hình hồi quy trong trường hợp này là

model_2<- lm (price ~ room_type + neighbourhood_group  + latitude + longitude  + minimum_nights +
                availability_365 , data = nyc_train)

summary_model_2<-summary(model_2)
mse_2 <- summary_model_2$sigma^2
r_sq_2 <- summary_model_2$r.squared
adj_r_sq_2 <- summary_model_2$adj.r.squared
# Các chỉ số MSE, R^2, adj R^2 của model_2
mse_2
## [1] 2132.74
r_sq_2
## [1] 0.4932513
adj_r_sq_2
## [1] 0.4930543
null <- lm(price~1, data = nyc_train)
full <- lm(price ~ neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + 
             reviews_per_month +calculated_host_listings_count + availability_365, data = nyc_train)

step(null, scope =list(lower=null, upper= full), direction = "both")
## Start:  AIC=214755.4
## price ~ 1
## 
##                                  Df Sum of Sq       RSS    AIC
## + room_type                       2  43299932  64968186 201616
## + neighbourhood_group             4  11504949  96763170 211872
## + longitude                       1   9976452  98291667 212269
## + calculated_host_listings_count  1   1767220 106500899 214334
## + latitude                        1    337491 107930628 214677
## + reviews_per_month               1    162612 108105507 214719
## + availability_365                1    102383 108165735 214733
## + number_of_reviews               1     58526 108209593 214743
## + minimum_nights                  1     28624 108239495 214751
## <none>                                        108268119 214755
## 
## Step:  AIC=201615.7
## price ~ room_type
## 
##                                  Df Sum of Sq       RSS    AIC
## + neighbourhood_group             4   6469909  58498277 198924
## + longitude                       1   4395875  60572311 199815
## + calculated_host_listings_count  1    719185  64249001 201331
## + latitude                        1    542406  64425780 201402
## + availability_365                1    472850  64495336 201430
## + minimum_nights                  1    150624  64817562 201558
## + number_of_reviews               1     27751  64940436 201607
## + reviews_per_month               1     10353  64957834 201614
## <none>                                         64968186 201616
## - room_type                       2  43299932 108268119 214755
## 
## Step:  AIC=198924
## price ~ room_type + neighbourhood_group
## 
##                                  Df Sum of Sq      RSS    AIC
## + longitude                       1   1874556 56623721 198088
## + availability_365                1    857454 57640823 198546
## + latitude                        1    633529 57864748 198646
## + calculated_host_listings_count  1    348740 58149537 198772
## + minimum_nights                  1    300360 58197917 198794
## + reviews_per_month               1     11645 58486632 198921
## <none>                                        58498277 198924
## + number_of_reviews               1      4315 58493962 198924
## - neighbourhood_group             4   6469909 64968186 201616
## - room_type                       2  38264893 96763170 211872
## 
## Step:  AIC=198087.8
## price ~ room_type + neighbourhood_group + longitude
## 
##                                  Df Sum of Sq      RSS    AIC
## + availability_365                1   1072730 55550992 197598
## + minimum_nights                  1    341909 56281813 197934
## + latitude                        1    258847 56364875 197972
## + calculated_host_listings_count  1    251741 56371981 197975
## + reviews_per_month               1     61038 56562684 198062
## <none>                                        56623721 198088
## + number_of_reviews               1        14 56623708 198090
## - longitude                       1   1874556 58498277 198924
## - neighbourhood_group             4   3948590 60572311 199815
## - room_type                       2  35768463 92392184 210684
## 
## Step:  AIC=197597.5
## price ~ room_type + neighbourhood_group + longitude + availability_365
## 
##                                  Df Sum of Sq      RSS    AIC
## + minimum_nights                  1    523816 55027176 197356
## + latitude                        1    177064 55373928 197517
## + calculated_host_listings_count  1     87235 55463757 197559
## + number_of_reviews               1     47330 55503662 197578
## + reviews_per_month               1      5915 55545077 197597
## <none>                                        55550992 197598
## - availability_365                1   1072730 56623721 198088
## - longitude                       1   2089831 57640823 198546
## - neighbourhood_group             4   4157120 59708112 199447
## - room_type                       2  36120999 91671991 210485
## 
## Step:  AIC=197355.7
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights
## 
##                                  Df Sum of Sq      RSS    AIC
## + latitude                        1    162448 54864728 197282
## + calculated_host_listings_count  1    113829 54913347 197304
## + number_of_reviews               1     86831 54940345 197317
## <none>                                        55027176 197356
## + reviews_per_month               1       940 55026236 197357
## - minimum_nights                  1    523816 55550992 197598
## - availability_365                1   1254637 56281813 197934
## - longitude                       1   2164756 57191931 198347
## - neighbourhood_group             4   4311937 59339113 199289
## - room_type                       2  36576295 91603470 210468
## 
## Step:  AIC=197281.6
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude
## 
##                                  Df Sum of Sq      RSS    AIC
## + calculated_host_listings_count  1     95498 54769229 197239
## + number_of_reviews               1     82934 54781794 197245
## <none>                                        54864728 197282
## + reviews_per_month               1      2287 54862440 197283
## - latitude                        1    162448 55027176 197356
## - minimum_nights                  1    509200 55373928 197517
## - availability_365                1   1166036 56030763 197821
## - longitude                       1   1801538 56666266 198111
## - neighbourhood_group             4   3452813 58317541 198844
## - room_type                       2  36007538 90872266 210264
## 
## Step:  AIC=197238.8
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count
## 
##                                  Df Sum of Sq      RSS    AIC
## + number_of_reviews               1     67975 54701254 197209
## <none>                                        54769229 197239
## + reviews_per_month               1      1341 54767888 197240
## - calculated_host_listings_count  1     95498 54864728 197282
## - latitude                        1    144118 54913347 197304
## - minimum_nights                  1    533821 55303051 197486
## - availability_365                1   1002112 55771342 197703
## - longitude                       1   1742701 56511930 198043
## - neighbourhood_group             4   3270649 58039879 198724
## - room_type                       2  35722485 90491714 210158
## 
## Step:  AIC=197208.8
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews
## 
##                                  Df Sum of Sq      RSS    AIC
## + reviews_per_month               1     14976 54686278 197204
## <none>                                        54701254 197209
## - number_of_reviews               1     67975 54769229 197239
## - calculated_host_listings_count  1     80539 54781794 197245
## - latitude                        1    142120 54843375 197274
## - minimum_nights                  1    566958 55268212 197472
## - availability_365                1   1069443 55770698 197705
## - longitude                       1   1731140 56432395 198009
## - neighbourhood_group             4   3272547 57973801 198696
## - room_type                       2  35773278 90474533 210155
## 
## Step:  AIC=197203.8
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews + reviews_per_month
## 
##                                  Df Sum of Sq      RSS    AIC
## <none>                                        54686278 197204
## - reviews_per_month               1     14976 54701254 197209
## - calculated_host_listings_count  1     79450 54765728 197239
## - number_of_reviews               1     81610 54767888 197240
## - latitude                        1    136167 54822445 197266
## - minimum_nights                  1    540493 55226771 197455
## - availability_365                1   1046578 55732856 197690
## - longitude                       1   1746100 56432378 198011
## - neighbourhood_group             4   3267733 57954011 198689
## - room_type                       2  35787837 90474115 210157
## 
## Call:
## lm(formula = price ~ room_type + neighbourhood_group + longitude + 
##     availability_365 + minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews + reviews_per_month, data = nyc_train)
## 
## Coefficients:
##                      (Intercept)             room_typePrivate room  
##                       -1.804e+04                        -7.473e+01  
##             room_typeShared room       neighbourhood_groupBrooklyn  
##                       -1.026e+02                        -6.182e+00  
##     neighbourhood_groupManhattan         neighbourhood_groupQueens  
##                        2.204e+01                         7.021e+00  
## neighbourhood_groupStaten Island                         longitude  
##                       -7.845e+01                        -2.840e+02  
##                 availability_365                    minimum_nights  
##                        5.348e-02                        -2.993e-01  
##                         latitude    calculated_host_listings_count  
##                       -6.909e+01                         7.403e-02  
##                number_of_reviews                 reviews_per_month  
##                       -4.454e-02                         5.541e-01

Kết quả trên cho thấy mô hình hồi quy Step-wise với AIC bao gồm các biến độc lập: neighbourhood_group latitude longitude room_type minimum_nights number_of_reviews calculated_host_listings_coun availability_365

Mô hình hồi quy được xây dựng như sau:

model_3 <- lm(price ~ neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + 
            + calculated_host_listings_count + availability_365, data = nyc_train)

summary_model_3<-summary(model_3)
mse_3 <- summary_model_3$sigma^2
r_sq_3 <- summary_model_3$r.squared
adj_r_sq_3 <- summary_model_3$adj.r.squared
# Các chỉ số MSE, R^2, adj R^2 của model_3
mse_3
## [1] 2126.55
r_sq_3
## [1] 0.4947612
adj_r_sq_3
## [1] 0.4945255
null <- lm(price~1, data = nyc_train)
full <- lm(price ~ neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + 
             reviews_per_month +calculated_host_listings_count + availability_365, data = nyc_train)

n=dim(nyc_train[1])
step(null, scope =list(lower=null, upper= full), k=log(n), direction = "both")
## Start:  AIC=214763.6
## price ~ 1
## 
##                                  Df Sum of Sq       RSS    AIC
## + room_type                       2  43299932  64968186 201640
## + neighbourhood_group             4  11504949  96763170 211862
## + longitude                       1   9976452  98291667 212265
## + calculated_host_listings_count  1   1767220 106500899 214350
## + latitude                        1    337491 107930628 214693
## + reviews_per_month               1    162612 108105507 214715
## + availability_365                1    102383 108165735 214729
## + minimum_nights                  1     28624 108239495 214747
## + number_of_reviews               1     58526 108209593 214760
## <none>                                        108268119 214764
## 
## Step:  AIC=201640.2
## price ~ room_type
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq       RSS    AIC
## + neighbourhood_group             4   6469909  58498277 198910
## + longitude                       1   4395875  60572311 199807
## + calculated_host_listings_count  1    719185  64249001 201323
## + latitude                        1    542406  64425780 201435
## + availability_365                1    472850  64495336 201462
## + minimum_nights                  1    150624  64817562 201591
## + number_of_reviews               1     27751  64940436 201599
## <none>                                         64968186 201640
## + reviews_per_month               1     10353  64957834 201646
## - room_type                       2  43299932 108268119 214753
## 
## Step:  AIC=198981.1
## price ~ room_type + neighbourhood_group
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + longitude                       1   1874556 56623721 198153
## + availability_365                1    857454 57640823 198530
## + latitude                        1    633529 57864748 198630
## + minimum_nights                  1    300360 58197917 198778
## + calculated_host_listings_count  1    348740 58149537 198837
## + reviews_per_month               1     11645 58486632 198905
## <none>                                        58498277 198981
## + number_of_reviews               1      4315 58493962 198989
## - neighbourhood_group             4   6469909 64968186 201640
## - room_type                       2  38264893 96763170 211862
## 
## Step:  AIC=198153
## price ~ room_type + neighbourhood_group + longitude
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + availability_365                1   1072730 55550992 197671
## + latitude                        1    258847 56364875 197954
## + calculated_host_listings_count  1    251741 56371981 197957
## + minimum_nights                  1    341909 56281813 198007
## + number_of_reviews               1        14 56623708 198072
## + reviews_per_month               1     61038 56562684 198135
## <none>                                        56623721 198153
## - longitude                       1   1874556 58498277 198910
## - neighbourhood_group             4   3948590 60572311 199847
## - room_type                       2  35768463 92392184 210672
## 
## Step:  AIC=197670.9
## price ~ room_type + neighbourhood_group + longitude + availability_365
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + minimum_nights                  1    523816 55027176 197437
## + latitude                        1    177064 55373928 197497
## + calculated_host_listings_count  1     87235 55463757 197539
## + number_of_reviews               1     47330 55503662 197558
## <none>                                        55550992 197671
## + reviews_per_month               1      5915 55545077 197678
## - availability_365                1   1072730 56623721 198153
## - longitude                       1   2089831 57640823 198530
## - neighbourhood_group             4   4157120 59708112 199488
## - room_type                       2  36120999 91671991 210471
## 
## Step:  AIC=197437.3
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + latitude                        1    162448 54864728 197260
## + reviews_per_month               1       940 55026236 197335
## + calculated_host_listings_count  1    113829 54913347 197394
## + number_of_reviews               1     86831 54940345 197407
## <none>                                        55027176 197437
## - minimum_nights                  1    523816 55550992 197580
## - availability_365                1   1254637 56281813 198007
## - longitude                       1   2164756 57191931 198329
## - neighbourhood_group             4   4311937 59339113 199338
## - room_type                       2  36576295 91603470 210452
## 
## Step:  AIC=197371.3
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + calculated_host_listings_count  1     95498 54769229 197215
## + number_of_reviews               1     82934 54781794 197221
## <none>                                        54864728 197371
## + reviews_per_month               1      2287 54862440 197380
## - latitude                        1    162448 55027176 197437
## - minimum_nights                  1    509200 55373928 197497
## - availability_365                1   1166036 56030763 197902
## - longitude                       1   1801538 56666266 198091
## - neighbourhood_group             4   3452813 58317541 198901
## - room_type                       2  36007538 90872266 210246
## 
## Step:  AIC=197336.7
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + number_of_reviews               1     67975 54701254 197183
## - calculated_host_listings_count  1     95498 54864728 197260
## <none>                                        54769229 197337
## + reviews_per_month               1      1341 54767888 197346
## - latitude                        1    144118 54913347 197394
## - minimum_nights                  1    533821 55303051 197464
## - availability_365                1   1002112 55771342 197793
## - longitude                       1   1742701 56511930 198021
## - neighbourhood_group             4   3270649 58039879 198789
## - room_type                       2  35722485 90491714 210138
## 
## Step:  AIC=197314.9
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews
## Warning in k * dfs: longer object length is not a multiple of shorter object
## length
##                                  Df Sum of Sq      RSS    AIC
## + reviews_per_month               1     14976 54686278 197176
## - calculated_host_listings_count  1     80539 54781794 197221
## <none>                                        54701254 197315
## - number_of_reviews               1     67975 54769229 197337
## - latitude                        1    142120 54843375 197371
## - minimum_nights                  1    566958 55268212 197448
## - availability_365                1   1069443 55770698 197803
## - longitude                       1   1731140 56432395 197985
## - neighbourhood_group             4   3272547 57973801 198770
## - room_type                       2  35773278 90474533 210133
## 
## Step:  AIC=197318
## price ~ room_type + neighbourhood_group + longitude + availability_365 + 
##     minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews + reviews_per_month
## 
## Call:
## lm(formula = price ~ room_type + neighbourhood_group + longitude + 
##     availability_365 + minimum_nights + latitude + calculated_host_listings_count + 
##     number_of_reviews + reviews_per_month, data = nyc_train)
## 
## Coefficients:
##                      (Intercept)             room_typePrivate room  
##                       -1.804e+04                        -7.473e+01  
##             room_typeShared room       neighbourhood_groupBrooklyn  
##                       -1.026e+02                        -6.182e+00  
##     neighbourhood_groupManhattan         neighbourhood_groupQueens  
##                        2.204e+01                         7.021e+00  
## neighbourhood_groupStaten Island                         longitude  
##                       -7.845e+01                        -2.840e+02  
##                 availability_365                    minimum_nights  
##                        5.348e-02                        -2.993e-01  
##                         latitude    calculated_host_listings_count  
##                       -6.909e+01                         7.403e-02  
##                number_of_reviews                 reviews_per_month  
##                       -4.454e-02                         5.541e-01

Nhận xét: kết quả giống model_1

library(Matrix)
library(glmnet)
## Loaded glmnet 4.1-8
lasso_fit <- glmnet(x = as.matrix(nyc_train[, -c(which(colnames(nyc_train) %in% c("room_type", "neighbourhood_group","price", "name", "host_name", 
                                                                                        "neighbourhood", "last_review", "host_id", "id")))]), 
                    y = nyc_train$price, alpha = 0.5)

coef(lasso_fit,s = lasso_fit$lambda.min)
## 8 x 67 sparse Matrix of class "dgCMatrix"
##   [[ suppressing 67 column names 's0', 's1', 's2' ... ]]
##                                                                            
## (Intercept)                    116.9319 -2048.28804 -4102.36747 -6045.44506
## latitude                         .          .           .           .      
## longitude                        .        -29.27947   -57.05604   -83.33156
## minimum_nights                   .          .           .           .      
## number_of_reviews                .          .           .           .      
## reviews_per_month                .          .           .           .      
## calculated_host_listings_count   .          .           .           .      
## availability_365                 .          .           .           .      
##                                                                           
## (Intercept)                    -7878.552 -9603.5023 -11222.7823 -12739.440
## latitude                           .         .           .           .    
## longitude                       -108.120  -131.4459   -153.3428   -173.852
## minimum_nights                     .         .           .           .    
## number_of_reviews                  .         .           .           .    
## reviews_per_month                  .         .           .           .    
## calculated_host_listings_count     .         .           .           .    
## availability_365                   .         .           .           .    
##                                                                   
## (Intercept)                    -14156.9816 -15479.2697 -16710.4350
## latitude                            .           .           .     
## longitude                        -193.0209   -210.9017   -227.5503
## minimum_nights                      .           .           .     
## number_of_reviews                   .           .           .     
## reviews_per_month                   .           .           .     
## calculated_host_listings_count      .           .           .     
## availability_365                    .           .           .     
##                                                                         
## (Intercept)                    -1.783059e+04 -1.882653e+04 -1.974846e+04
## latitude                        .             .             .           
## longitude                      -2.426973e+02 -2.561635e+02 -2.686292e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  7.843550e-03  2.899403e-02  4.856309e-02
## availability_365                .             .             .           
##                                                                         
## (Intercept)                    -2.060079e+04 -2.143317e+04 -2.255146e+04
## latitude                        .             9.278605e-01  8.946540e+00
## longitude                      -2.801537e+02 -2.908976e+02 -3.016025e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  6.664620e-02  8.330368e-02  9.840362e-02
## availability_365                .             .             .           
##                                                                         
## (Intercept)                    -2.358336e+04 -2.453452e+04 -2.541040e+04
## latitude                        1.635016e+01  2.317781e+01  2.946795e+01
## longitude                      -3.114781e+02 -3.205791e+02 -3.289583e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  1.123066e-01  1.250963e-01  1.368521e-01
## availability_365                .             .             .           
##                                                                         
## (Intercept)                    -2.626274e+04 -2.707196e+04 -2.781599e+04
## latitude                        3.540414e+01  4.094413e+01  4.603754e+01
## longitude                      -3.372120e+02 -3.451000e+02 -3.523525e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  1.461838e-01  1.539384e-01  1.610305e-01
## availability_365                1.480731e-03  3.642288e-03  5.629404e-03
##                                                                         
## (Intercept)                    -2.849950e+04 -2.912699e+04 -2.970267e+04
## latitude                        5.071642e+01  5.501153e+01  5.895183e+01
## longitude                      -3.590153e+02 -3.651321e+02 -3.707440e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  1.675147e-01  1.734413e-01  1.788567e-01
## availability_365                7.454759e-03  9.130369e-03  1.066755e-02
##                                                                         
## (Intercept)                    -3.023051e+04 -3.071422e+04 -3.115728e+04
## latitude                        6.256453e+01  6.587508e+01  6.890726e+01
## longitude                      -3.758897e+02 -3.806053e+02 -3.849247e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  1.838035e-01  1.883213e-01  1.924464e-01
## availability_365                1.207692e-02  1.336841e-02  1.455132e-02
##                                                                         
## (Intercept)                    -3.156293e+04 -3.193415e+04 -3.227375e+04
## latitude                        7.168323e+01  7.422360e+01  7.654748e+01
## longitude                      -3.888792e+02 -3.924984e+02 -3.958092e+02
## minimum_nights                  .             .             .           
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  1.962122e-01  1.996493e-01  2.027860e-01
## availability_365                1.563427e-02  1.662532e-02  1.753192e-02
##                                                                         
## (Intercept)                    -3.258432e+04 -3.288032e+04 -3.315441e+04
## latitude                        7.867259e+01  8.068015e+01  8.253329e+01
## longitude                      -3.988370e+02 -4.017331e+02 -4.044181e+02
## minimum_nights                  .            -5.443633e-03 -1.236399e-02
## number_of_reviews               .             .             .           
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.056481e-01  2.084939e-01  2.111809e-01
## availability_365                1.836098e-02  1.919090e-02  1.997468e-02
##                                                                         
## (Intercept)                    -3.340585e+04 -3.363558e+04 -3.384540e+04
## latitude                        8.423333e+01  8.578645e+01  8.720503e+01
## longitude                      -4.068812e+02 -4.091316e+02 -4.111872e+02
## minimum_nights                 -1.867998e-02 -2.445098e-02 -2.972271e-02
## number_of_reviews               .             .            -1.373170e-04
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.136225e-01  2.158502e-01  2.178664e-01
## availability_365                2.069177e-02  2.134699e-02  2.195682e-02
##                                                                         
## (Intercept)                    -3.403533e+04 -3.420847e+04 -3.436652e+04
## latitude                        8.850567e+01  8.968962e+01  9.077039e+01
## longitude                      -4.130391e+02 -4.147282e+02 -4.162702e+02
## minimum_nights                 -3.501329e-02 -3.980885e-02 -4.418756e-02
## number_of_reviews              -1.619120e-03 -2.960639e-03 -4.185527e-03
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.194403e-01  2.208894e-01  2.222102e-01
## availability_365                2.263463e-02  2.325175e-02  2.381534e-02
##                                                                         
## (Intercept)                    -3.450943e+04 -3.464119e+04 -3.476143e+04
## latitude                        9.174890e+01  9.264968e+01  9.347168e+01
## longitude                      -4.176636e+02 -4.189492e+02 -4.201224e+02
## minimum_nights                 -4.818075e-02 -5.182964e-02 -5.515973e-02
## number_of_reviews              -5.289245e-03 -6.310980e-03 -7.243588e-03
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.234377e-01  2.245332e-01  2.255313e-01
## availability_365                2.432721e-02  2.479721e-02  2.522624e-02
##                                                                         
## (Intercept)                    -3.487114e+04 -3.497122e+04 -3.506251e+04
## latitude                        9.422163e+01  9.490576e+01  9.552978e+01
## longitude                      -4.211928e+02 -4.221693e+02 -4.230601e+02
## minimum_nights                 -5.819840e-02 -6.097079e-02 -6.349993e-02
## number_of_reviews              -8.094588e-03 -8.871019e-03 -9.579332e-03
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.264409e-01  2.272698e-01  2.280253e-01
## availability_365                2.561779e-02  2.597507e-02  2.630105e-02
##                                                                         
## (Intercept)                    -3.514577e+04 -3.522171e+04 -3.529096e+04
## latitude                        9.609892e+01  9.661797e+01  9.709128e+01
## longitude                      -4.238725e+02 -4.246135e+02 -4.252892e+02
## minimum_nights                 -6.580694e-02 -6.791110e-02 -6.983009e-02
## number_of_reviews              -1.022544e-02 -1.081474e-02 -1.135218e-02
## reviews_per_month               .             .             .           
## calculated_host_listings_count  2.287137e-01  2.293410e-01  2.299127e-01
## availability_365                2.659843e-02  2.686970e-02  2.711711e-02
##                                                                         
## (Intercept)                    -3.535410e+04 -3.541167e+04 -3.546197e+04
## latitude                        9.752287e+01  9.791639e+01  9.826535e+01
## longitude                      -4.259053e+02 -4.264671e+02 -4.269549e+02
## minimum_nights                 -7.158007e-02 -7.317579e-02 -7.458288e-02
## number_of_reviews              -1.184228e-02 -1.228919e-02 -1.266453e-02
## reviews_per_month               .             .             9.451821e-03
## calculated_host_listings_count  2.304337e-01  2.309084e-01  2.314111e-01
## availability_365                2.734276e-02  2.754853e-02  2.770570e-02
##                                                                         
## (Intercept)                    -3.552097e+04 -3.556825e+04 -3.561266e+04
## latitude                        9.861490e+01  9.891166e+01  9.919076e+01
## longitude                      -4.275599e+02 -4.280357e+02 -4.284823e+02
## minimum_nights                 -7.561603e-02 -7.664626e-02 -7.759156e-02
## number_of_reviews              -1.360722e-02 -1.417831e-02 -1.477098e-02
## reviews_per_month               3.161077e-02  4.735588e-02  6.271259e-02
## calculated_host_listings_count  2.317206e-01  2.320865e-01  2.323981e-01
## availability_365                2.787166e-02  2.800606e-02  2.813328e-02
##                                                                         
## (Intercept)                    -3.565335e+04 -3.569047e+04 -3.572431e+04
## latitude                        9.944591e+01  9.967860e+01  9.989070e+01
## longitude                      -4.288919e+02 -4.292655e+02 -4.296061e+02
## minimum_nights                 -7.845656e-02 -7.924551e-02 -7.996468e-02
## number_of_reviews              -1.531970e-02 -1.582085e-02 -1.627787e-02
## reviews_per_month               7.679593e-02  8.964252e-02  1.013560e-01
## calculated_host_listings_count  2.326772e-01  2.329308e-01  2.331618e-01
## availability_365                2.825004e-02  2.835658e-02  2.845371e-02
##                                                                         
## (Intercept)                    -3.575516e+04 -3.578327e+04 -3.580890e+04
## latitude                        1.000840e+02  1.002602e+02  1.004208e+02
## longitude                      -4.299167e+02 -4.301997e+02 -4.304576e+02
## minimum_nights                 -8.062017e-02 -8.121758e-02 -8.176207e-02
## number_of_reviews              -1.669455e-02 -1.707443e-02 -1.742075e-02
## reviews_per_month               1.120357e-01  1.217721e-01  1.306483e-01
## calculated_host_listings_count  2.333722e-01  2.335640e-01  2.337386e-01
## availability_365                2.854225e-02  2.862295e-02  2.869651e-02
##                                                                         
## (Intercept)                    -3.583225e+04 -3.585354e+04 -3.587294e+04
## latitude                        1.005672e+02  1.007006e+02  1.008222e+02
## longitude                      -4.306928e+02 -4.309071e+02 -4.311024e+02
## minimum_nights                 -8.225829e-02 -8.271053e-02 -8.312267e-02
## number_of_reviews              -1.773646e-02 -1.802424e-02 -1.828657e-02
## reviews_per_month               1.387398e-01  1.461157e-01  1.528389e-01
## calculated_host_listings_count  2.338978e-01  2.340428e-01  2.341749e-01
## availability_365                2.876356e-02  2.882466e-02  2.888035e-02
##                                                           
## (Intercept)                    -3.589062e+04 -3.590673e+04
## latitude                        1.009330e+02  1.010340e+02
## longitude                      -4.312804e+02 -4.314426e+02
## minimum_nights                 -8.349826e-02 -8.384053e-02
## number_of_reviews              -1.852568e-02 -1.874362e-02
## reviews_per_month               1.589672e-01  1.645528e-01
## calculated_host_listings_count  2.342953e-01  2.344050e-01
## availability_365                2.893111e-02  2.897737e-02
model_4<-lm(price ~ latitude + longitude + minimum_nights + number_of_reviews + reviews_per_month + calculated_host_listings_count + availability_365,
        data = nyc_train, nbest = 2, nvmax = 9)
## Warning: In lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
##  extra arguments 'nbest', 'nvmax' will be disregarded
summary_model_4<-summary(model_4)
mse_4 <- summary_model_4$sigma^2
r_sq_4 <- summary_model_4$r.squared
adj_r_sq_4 <- summary_model_4$adj.r.squared
# Các chỉ số MSE, R^2, adj R^2 của model_3
mse_4
## [1] 3733.324
r_sq_4
## [1] 0.1128416
adj_r_sq_4
## [1] 0.1126003

Vậy ta xét 4 mô hình hồi quy với các tiêu chuẩn đánh giá được cho như sau:

model_names <- c("model_1", "model_2", "model_3", "model_4")
mse_values <- c(mse_1, mse_2, mse_3, mse_4)
r_squared_values <- c(r_sq_1, r_sq_2, r_sq_3, r_sq_4)
adj_r_squared_values <- c(adj_r_sq_1, adj_r_sq_2, adj_r_sq_3, adj_r_sq_4)

# Tạo bảng dữ liệu
results_table <- data.frame(
  Model = model_names,
  MSE = mse_values,
  R_squared = r_squared_values,
  Adj_R_squared = adj_r_squared_values
)
# In bảng
print(results_table)
##     Model      MSE R_squared Adj_R_squared
## 1 model_1 2126.051 0.4948995     0.4946442
## 2 model_2 2132.740 0.4932513     0.4930543
## 3 model_3 2126.550 0.4947612     0.4945255
## 4 model_4 3733.324 0.1128416     0.1126003

Dựa vào tất cả các tiêu chí trên thì model_1 phù hợp hơn. - Sử dụng lệnh predict() để tính các giá trị dự đoán từ tập test từ đó đánh giá tính hiệu quả của mô hình

pre_test <- predict(object = model_1, newdata = nyc_test)
# Tính MSE, MAE
mse<-mean((pre_test - nyc_test$price)^2)
mae<-mean(abs(pre_test - nyc_test$price))
mse
## [1] 2210.627
mae
## [1] 34.69764

Tính MSPE của toàn bộ dữ liệu

library(boot)
model_glm_1 = glm(price~neighbourhood_group + latitude + longitude + room_type + minimum_nights  + number_of_reviews + 
                    reviews_per_month +calculated_host_listings_count + availability_365,  data = nyc_new)

cv.glm(data =nyc_new, glmfit = model_glm_1, K = 3)$delta[2]
## [1] 2151.959

So sánh MSE của Test Dataset bằng và MSPE của toàn bộ dữ liệu, ta có thể thấy rằng các giá trị gần như tương tự nhau. Do đó, các biến mà đã chọn cho mô hình 1là các ước lượng tốt cho biến phụ thuộc.

6. Kết luận