Boston Project

Author

Manh Cuong NEU

Published

December 6, 2023

Các packges cần thiết

library(MASS)
library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following object is masked from 'package:MASS':

    select
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ lubridate 1.9.2     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.0
✔ readr     2.1.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ dplyr::select() masks MASS::select()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(corrplot)
corrplot 0.92 loaded
library(AER)
Loading required package: car
Loading required package: carData

Attaching package: 'car'

The following object is masked from 'package:purrr':

    some

The following object is masked from 'package:dplyr':

    recode

Loading required package: lmtest
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: sandwich
Loading required package: survival
library(lmtest)
library(gplots)

Attaching package: 'gplots'

The following object is masked from 'package:stats':

    lowess
library(corrgram)

Tổng quan dữ liệu Boston

data("Boston")
dim(Boston)
[1] 506  14
attach(Boston)
?Boston
starting httpd help server ... done

Dữ liệu Boston

Biến Định nghĩa
crim tỷ lệ tội phạm/dân số
zn tỷ lệ đất ở trên diện tích 25,000 feet vuông
indus tỷ lệ diện tích ko có dịch vụ bán lẻ
chas Có gần sông Charles không (1= gần; 0: không)
nox nồng độ oxit nitric
rm số phòng trung bình mỗi nhà
age tỷ lệ số căn nhà ở được xây trước năm 1940
dis khoảng cách trung bình (có trọng số) đến 5 trung tâm việc làm ở Boston
rad biến chỉ số về khả năng tiếp cận với đường cao tốc
tax thuế suất tài sản với giá trị 10,000 USD
ptratio tỷ lệ học sinh/giáo viên
black \(1000(Bk-0.63)^2\) với \(Bk\) là tỷ lệ người da đen
lstat tỷ lệ người dân hạ lưu trên tổng dân số
medv giá trị trung bình của nhà (tính theo 1000USD)

Thống kê mô tả

Giá trị các biến

library(psych)

Attaching package: 'psych'
The following object is masked from 'package:car':

    logit
The following objects are masked from 'package:ggplot2':

    %+%, alpha
describe(Boston)
        vars   n   mean     sd median trimmed    mad    min    max  range  skew
crim       1 506   3.61   8.60   0.26    1.68   0.33   0.01  88.98  88.97  5.19
zn         2 506  11.36  23.32   0.00    5.08   0.00   0.00 100.00 100.00  2.21
indus      3 506  11.14   6.86   9.69   10.93   9.37   0.46  27.74  27.28  0.29
chas       4 506   0.07   0.25   0.00    0.00   0.00   0.00   1.00   1.00  3.39
nox        5 506   0.55   0.12   0.54    0.55   0.13   0.38   0.87   0.49  0.72
rm         6 506   6.28   0.70   6.21    6.25   0.51   3.56   8.78   5.22  0.40
age        7 506  68.57  28.15  77.50   71.20  28.98   2.90 100.00  97.10 -0.60
dis        8 506   3.80   2.11   3.21    3.54   1.91   1.13  12.13  11.00  1.01
rad        9 506   9.55   8.71   5.00    8.73   2.97   1.00  24.00  23.00  1.00
tax       10 506 408.24 168.54 330.00  400.04 108.23 187.00 711.00 524.00  0.67
ptratio   11 506  18.46   2.16  19.05   18.66   1.70  12.60  22.00   9.40 -0.80
black     12 506 356.67  91.29 391.44  383.17   8.09   0.32 396.90 396.58 -2.87
lstat     13 506  12.65   7.14  11.36   11.90   7.11   1.73  37.97  36.24  0.90
medv      14 506  22.53   9.20  21.20   21.56   5.93   5.00  50.00  45.00  1.10
        kurtosis   se
crim       36.60 0.38
zn          3.95 1.04
indus      -1.24 0.30
chas        9.48 0.01
nox        -0.09 0.01
rm          1.84 0.03
age        -0.98 1.25
dis         0.46 0.09
rad        -0.88 0.39
tax        -1.15 7.49
ptratio    -0.30 0.10
black       7.10 4.06
lstat       0.46 0.32
medv        1.45 0.41

Biểu đồ

par(mfrow = c(3,4))
hist(medv,
     col='skyblue',
     main = "Histogram của giá nhà",
     xlab="Giá nhà",
     ylab="Tần suất")

hist(crim,
     col='skyblue',
     main = "Histogram của tỷ lệ tội phạm",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(zn,
     col='skyblue',
     main = "Histogram của tỷ lệ đất",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(indus,
     col='skyblue',
     main = "Histogram của tỷ lệ diện tích khu công nghiệp",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(nox,
     col='skyblue',
     main = "Histogram của nồng độ oxit nitric",
     xlab="Nồng độ",
     ylab="Tần số")

hist(rm,
     col='skyblue',
     main = "Histogram của số phòng mỗi nhà",
     xlab="Số phòng",
     ylab="Tần số")

hist(age,
     col='skyblue',
     main = "Histogram của tỷ lệ số căn nhà trước 1940",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(dis,
     col='skyblue',
     main = "Histogram của khoảng cách đến trung tâm",
     xlab="Khoảng cách",
     ylab="Tần số")

hist(tax,
     col='skyblue',
     main = "Histogram của thuế trên tài sản",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(ptratio,
     col='skyblue',
     main = "Histogram của tỷ lệ học sinh trên giáo viên",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(black,
     col='skyblue',
     main = "Histogram của tỷ lệ người da đen",
     xlab="Tỷ lệ",
     ylab="Tần số")

hist(lstat,
     col='skyblue',
     main = "Histogram của tỷ lệ người dân hạ lưu",
     xlab="Tỷ lệ",
     ylab="Tần số")

Phân tích khám phá

Ma trận hệ số tương quan

#Chuyển biến medv thành log(medv) trong dữ liệu Boston
data.new=mutate(Boston,medv = log(medv))
View(data.new)
cor=cor(data.new)
ord=corrMatOrder(cor,order = "AOE")
corBoston=cor[ord,ord]
corrplot.mixed(corBoston,
               lower = "number",
               upper = "square")

Biểu đồ Scatter plot

par(mfrow = c(3,4))
ggplot(Boston, aes(x = crim, y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "crim vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = zn, y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
   labs(title = "zn vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = indus , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "indus vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = nox , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "nox vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = rm , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "rm vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = age , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "age vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = dis , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "dis vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = rad , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "rad vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = tax , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "tax vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = ptratio , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "ptratio vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = black , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "black vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

ggplot(Boston, aes(x = lstat , y = log(medv)))+
       geom_point(col = "lightpink")+
       geom_smooth(method = "lm")+
  labs(title = "lstat vs log(medv)")
`geom_smooth()` using formula = 'y ~ x'

Kiểm định độc lập

Kiểm định tính độc lập của hai biến định tính chas (vị trí so với sông Charles) và rad (vị trí so với đường cao tốc)

Vị trí so với đường cao tốc có mối liên hệ với sông Charles hay không?

Vẽ biểu đồ về mối liên hệ giữa vị trí so với đường cao tốc và so với sông Charles

pl <- ggplot(Boston, aes(x=factor(chas), y=rad)) + geom_count(color='green') 
pl

Tiến hành kiểm khi bình phương với hai giả thuyết

H0: không có mối liên hệ giữa tiếp cận đường cao tốc và sông Charles

H1: có mối liên hệ giữa tiếp cận đường cao tốc và sông Charles

chisq.test(Boston$chas, Boston$rad)
Warning in chisq.test(Boston$chas, Boston$rad): Chi-squared approximation may
be incorrect

    Pearson's Chi-squared test

data:  Boston$chas and Boston$rad
X-squared = 13.898, df = 8, p-value = 0.08447

Giá trị p-value = 0.08447>0.05, do vậy, không thể bác bỏ giả thuyết H0 với mức ý nghĩa 5%, điều này cho thấy, chưa thể khẳng định có mối liên hệ giữa vị trí so với đường cao tốc và vị trí so với sông Charles

Kiểm định so sánh

So sánh giá nhà gần sông và không gần sông

pl <- ggplot(Boston, aes(x=factor(chas), y=medv)) + geom_boxplot(color='gray', fill='lightblue') 
pl

t.test(medv~chas, data=Boston)

    Welch Two Sample t-test

data:  medv by chas
t = -3.1133, df = 36.876, p-value = 0.003567
alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
95 percent confidence interval:
 -10.476831  -2.215483
sample estimates:
mean in group 0 mean in group 1 
       22.09384        28.44000 

p value < 0.05 -> có sự khác biệt có ý nghĩa thống kê giữa giá nhà gần sông và gần sông

Giá nhà từng khu vực

table(Boston$chas)  

  0   1 
471  35 
by(Boston$medv, chas, summary)
chas: 0
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   5.00   16.60   20.90   22.09   24.80   50.00 
------------------------------------------------------------ 
chas: 1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  13.40   21.10   23.30   28.44   33.15   50.00 

So sánh giá nhà theo chỉ số tiếp cận đường cao tốc

rad.factor <- as.factor(Boston$rad) 
summary(aov(medv~rad.factor, data=Boston))
             Df Sum Sq Mean Sq F value Pr(>F)    
rad.factor    8   9767  1220.9   18.42 <2e-16 ***
Residuals   497  32949    66.3                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

p-value <0.05

TukeyHSD(aov(medv~rad.factor, data=Boston))
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = medv ~ rad.factor, data = Boston)

$rad.factor
            diff         lwr        upr     p adj
2-1    2.4683333  -5.2119811 10.1486478 0.9857004
3-1    3.5639474  -3.4438330 10.5717277 0.8130843
4-1   -2.9777273  -9.1441546  3.1887000 0.8533059
5-1    1.3419565  -4.8038127  7.4877257 0.9990074
6-1   -3.3880769 -10.9329254  4.1567716 0.8980082
7-1    2.7408824  -5.6273684 11.1091331 0.9838484
8-1    5.9933333  -1.6869811 13.6736478 0.2693436
24-1  -7.9612121 -14.0480693 -1.8743549 0.0017491
3-2    1.0956140  -5.5185004  7.7097285 0.9998724
4-2   -5.4460606 -11.1611591  0.2690379 0.0757417
5-2   -1.1263768  -6.8191796  4.5664260 0.9995178
6-2   -5.8564103 -13.0370971  1.3242766 0.2150865
7-2    0.2725490  -7.7689151  8.3140131 1.0000000
8-2    3.5250000  -3.7978925 10.8478925 0.8555297
24-2 -10.4295455 -16.0586977 -4.8003932 0.0000005
4-3   -6.5416746 -11.3149446 -1.7684047 0.0007837
5-3   -2.2219908  -6.9685432  2.5245615 0.8740056
6-3   -6.9520243 -13.4083405 -0.4957080 0.0239506
7-3   -0.8230650  -8.2248824  6.5787524 0.9999941
8-3    2.4293860  -4.1847285  9.0435004 0.9670666
24-3 -11.5251595 -16.1951820 -6.8551369 0.0000000
5-4    4.3196838   0.9365492  7.7028184 0.0025704
6-4   -0.4103497  -5.9420637  5.1213644 0.9999998
7-4    5.7186096  -0.8921952 12.3294145 0.1521826
8-4    8.9710606   3.2559621 14.6861591 0.0000476
24-4  -4.9834848  -8.2583819 -1.7085878 0.0000965
6-5   -4.7300334 -10.2387096  0.7786427 0.1594459
7-5    1.3989258  -5.1926138  7.9904654 0.9991931
8-5    4.6513768  -1.0414260 10.3441796 0.2129726
24-5  -9.3031686 -12.5389999 -6.0673373 0.0000000
7-6    6.1289593  -1.7832246 14.0411432 0.2788950
8-6    9.3814103   2.2007234 16.5620971 0.0017813
24-6  -4.5731352 -10.0160080  0.8697376 0.1818375
8-7    3.2524510  -4.7890131 11.2939151 0.9421679
24-7 -10.7020945 -17.2387408 -4.1654481 0.0000170
24-8 -13.9545455 -19.5836977 -8.3253932 0.0000000
boxplot(medv~rad, data=Boston, col=rainbow(9)) 

Xây dựng mô hình hồi quy tuyến tính

Mô hình đề xuất

Mô hình 1 và Mô hình 2:

\[ log(medv) = \beta_0 + \beta_1\times crim + \beta_2\times zn + \beta_3\times indus + \beta_4\times chas + \beta_5\times nox + \\ \beta_6\times rm + \beta_7\times age + \beta_8\times dis + \beta_9\times rad + \beta_{10}\times tax + \beta_{11}\times ptratio + \\ \beta_{12}\times black + \beta_{13}\times lstat + \epsilon. \]

Mô hình 3 và Mô hình 4:

\[ medv = \beta_0 + \beta_1\times crim + \beta_2\times zn + \beta_3\times indus + \beta_4\times chas + \beta_5\times nox + \\ \beta_6\times rm + \beta_7\times age + \beta_8\times dis + \beta_9\times rad + \beta_{10}\times tax + \beta_{11}\times ptratio + \\ \beta_{12}\times black + \beta_{13}\times lstat + \epsilon. \]

model1=lm(log(medv)~.,data = Boston) 
summary(model1)

Call:
lm(formula = log(medv) ~ ., data = Boston)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.73361 -0.09747 -0.01657  0.09629  0.86435 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  4.1020423  0.2042726  20.081  < 2e-16 ***
crim        -0.0102715  0.0013155  -7.808 3.52e-14 ***
zn           0.0011725  0.0005495   2.134 0.033349 *  
indus        0.0024668  0.0024614   1.002 0.316755    
chas         0.1008876  0.0344859   2.925 0.003598 ** 
nox         -0.7783993  0.1528902  -5.091 5.07e-07 ***
rm           0.0908331  0.0167280   5.430 8.87e-08 ***
age          0.0002106  0.0005287   0.398 0.690567    
dis         -0.0490873  0.0079834  -6.149 1.62e-09 ***
rad          0.0142673  0.0026556   5.373 1.20e-07 ***
tax         -0.0006258  0.0001505  -4.157 3.80e-05 ***
ptratio     -0.0382715  0.0052365  -7.309 1.10e-12 ***
black        0.0004136  0.0001075   3.847 0.000135 ***
lstat       -0.0290355  0.0020299 -14.304  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1899 on 492 degrees of freedom
Multiple R-squared:  0.7896,    Adjusted R-squared:  0.7841 
F-statistic: 142.1 on 13 and 492 DF,  p-value: < 2.2e-16
model2=lm(log(medv)~.-indus - age,data = Boston) 
summary(model2)

Call:
lm(formula = log(medv) ~ . - indus - age, data = Boston)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.73400 -0.09460 -0.01771  0.09782  0.86290 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  4.0836823  0.2030491  20.112  < 2e-16 ***
crim        -0.0103187  0.0013134  -7.856 2.49e-14 ***
zn           0.0010874  0.0005418   2.007 0.045308 *  
chas         0.1051484  0.0342285   3.072 0.002244 ** 
nox         -0.7217440  0.1416535  -5.095 4.97e-07 ***
rm           0.0906728  0.0162807   5.569 4.20e-08 ***
dis         -0.0517059  0.0074420  -6.948 1.18e-11 ***
rad          0.0134457  0.0025405   5.293 1.82e-07 ***
tax         -0.0005579  0.0001351  -4.129 4.28e-05 ***
ptratio     -0.0374259  0.0051715  -7.237 1.77e-12 ***
black        0.0004127  0.0001071   3.852 0.000133 ***
lstat       -0.0286039  0.0019002 -15.053  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1898 on 494 degrees of freedom
Multiple R-squared:  0.7891,    Adjusted R-squared:  0.7844 
F-statistic: 168.1 on 11 and 494 DF,  p-value: < 2.2e-16
model3=lm(medv~.,data = Boston ) 
summary(model3)

Call:
lm(formula = medv ~ ., data = Boston)

Residuals:
    Min      1Q  Median      3Q     Max 
-15.595  -2.730  -0.518   1.777  26.199 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.646e+01  5.103e+00   7.144 3.28e-12 ***
crim        -1.080e-01  3.286e-02  -3.287 0.001087 ** 
zn           4.642e-02  1.373e-02   3.382 0.000778 ***
indus        2.056e-02  6.150e-02   0.334 0.738288    
chas         2.687e+00  8.616e-01   3.118 0.001925 ** 
nox         -1.777e+01  3.820e+00  -4.651 4.25e-06 ***
rm           3.810e+00  4.179e-01   9.116  < 2e-16 ***
age          6.922e-04  1.321e-02   0.052 0.958229    
dis         -1.476e+00  1.995e-01  -7.398 6.01e-13 ***
rad          3.060e-01  6.635e-02   4.613 5.07e-06 ***
tax         -1.233e-02  3.760e-03  -3.280 0.001112 ** 
ptratio     -9.527e-01  1.308e-01  -7.283 1.31e-12 ***
black        9.312e-03  2.686e-03   3.467 0.000573 ***
lstat       -5.248e-01  5.072e-02 -10.347  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 4.745 on 492 degrees of freedom
Multiple R-squared:  0.7406,    Adjusted R-squared:  0.7338 
F-statistic: 108.1 on 13 and 492 DF,  p-value: < 2.2e-16
model4=lm(medv~. - indus - age, data = Boston) 
summary(model4)

Call:
lm(formula = medv ~ . - indus - age, data = Boston)

Residuals:
     Min       1Q   Median       3Q      Max 
-15.5984  -2.7386  -0.5046   1.7273  26.2373 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  36.341145   5.067492   7.171 2.73e-12 ***
crim         -0.108413   0.032779  -3.307 0.001010 ** 
zn            0.045845   0.013523   3.390 0.000754 ***
chas          2.718716   0.854240   3.183 0.001551 ** 
nox         -17.376023   3.535243  -4.915 1.21e-06 ***
rm            3.801579   0.406316   9.356  < 2e-16 ***
dis          -1.492711   0.185731  -8.037 6.84e-15 ***
rad           0.299608   0.063402   4.726 3.00e-06 ***
tax          -0.011778   0.003372  -3.493 0.000521 ***
ptratio      -0.946525   0.129066  -7.334 9.24e-13 ***
black         0.009291   0.002674   3.475 0.000557 ***
lstat        -0.522553   0.047424 -11.019  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 4.736 on 494 degrees of freedom
Multiple R-squared:  0.7406,    Adjusted R-squared:  0.7348 
F-statistic: 128.2 on 11 and 494 DF,  p-value: < 2.2e-16

So sánh

library(texreg)
Version:  1.39.3
Date:     2023-11-09
Author:   Philip Leifeld (University of Essex)

Consider submitting praise using the praise or praise_interactive functions.
Please cite the JSS article in your publications -- see citation("texreg").

Attaching package: 'texreg'
The following object is masked from 'package:tidyr':

    extract
library(stargazer)

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
stargazer(model1, model2, model3, model4, type = "text") #Compare to 4 models

===========================================================================================================================
                                                              Dependent variable:                                          
                    -------------------------------------------------------------------------------------------------------
                                         log(medv)                                             medv                        
                               (1)                       (2)                       (3)                       (4)           
---------------------------------------------------------------------------------------------------------------------------
crim                        -0.010***                 -0.010***                 -0.108***                 -0.108***        
                             (0.001)                   (0.001)                   (0.033)                   (0.033)         
                                                                                                                           
zn                           0.001**                   0.001**                  0.046***                  0.046***         
                             (0.001)                   (0.001)                   (0.014)                   (0.014)         
                                                                                                                           
indus                         0.002                                               0.021                                    
                             (0.002)                                             (0.061)                                   
                                                                                                                           
chas                        0.101***                  0.105***                  2.687***                  2.719***         
                             (0.034)                   (0.034)                   (0.862)                   (0.854)         
                                                                                                                           
nox                         -0.778***                 -0.722***                -17.767***                -17.376***        
                             (0.153)                   (0.142)                   (3.820)                   (3.535)         
                                                                                                                           
rm                          0.091***                  0.091***                  3.810***                  3.802***         
                             (0.017)                   (0.016)                   (0.418)                   (0.406)         
                                                                                                                           
age                          0.0002                                               0.001                                    
                             (0.001)                                             (0.013)                                   
                                                                                                                           
dis                         -0.049***                 -0.052***                 -1.476***                 -1.493***        
                             (0.008)                   (0.007)                   (0.199)                   (0.186)         
                                                                                                                           
rad                         0.014***                  0.013***                  0.306***                  0.300***         
                             (0.003)                   (0.003)                   (0.066)                   (0.063)         
                                                                                                                           
tax                         -0.001***                 -0.001***                 -0.012***                 -0.012***        
                            (0.0002)                  (0.0001)                   (0.004)                   (0.003)         
                                                                                                                           
ptratio                     -0.038***                 -0.037***                 -0.953***                 -0.947***        
                             (0.005)                   (0.005)                   (0.131)                   (0.129)         
                                                                                                                           
black                       0.0004***                 0.0004***                 0.009***                  0.009***         
                            (0.0001)                  (0.0001)                   (0.003)                   (0.003)         
                                                                                                                           
lstat                       -0.029***                 -0.029***                 -0.525***                 -0.523***        
                             (0.002)                   (0.002)                   (0.051)                   (0.047)         
                                                                                                                           
Constant                    4.102***                  4.084***                  36.459***                 36.341***        
                             (0.204)                   (0.203)                   (5.103)                   (5.067)         
                                                                                                                           
---------------------------------------------------------------------------------------------------------------------------
Observations                   506                       506                       506                       506           
R2                            0.790                     0.789                     0.741                     0.741          
Adjusted R2                   0.784                     0.784                     0.734                     0.735          
Residual Std. Error     0.190 (df = 492)          0.190 (df = 494)          4.745 (df = 492)          4.736 (df = 494)     
F Statistic         142.066*** (df = 13; 492) 168.075*** (df = 11; 494) 108.077*** (df = 13; 492) 128.206*** (df = 11; 494)
===========================================================================================================================
Note:                                                                                           *p<0.1; **p<0.05; ***p<0.01

Kiểm định lựa chọn mô hình phù hợp

Giả thuyết lựa chọn mô hình giữa mô hình 1 và mô hình 2

\[ H_o: \beta_3 = \beta_7 = 0 => chọn \ mô \ hình \ 2 \\ H_1: Tồn \ tại \ \beta_i \ khác \ 0 => chọn \ mô \ hình \ 1 \]

anova(model1,model2)
Analysis of Variance Table

Model 1: log(medv) ~ crim + zn + indus + chas + nox + rm + age + dis + 
    rad + tax + ptratio + black + lstat
Model 2: log(medv) ~ (crim + zn + indus + chas + nox + rm + age + dis + 
    rad + tax + ptratio + black + lstat) - indus - age
  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1    492 17.749                           
2    494 17.791 -2 -0.041988 0.5819 0.5592

P-value = 0.5592 > 0.05 => lựa chọn mô hình 2

Biện luận

log - lin: Khi X thay đổi 1 đơn vị thì (Beta * 100)% sự thay đổi của Y

summary(model2)

Call:
lm(formula = log(medv) ~ . - indus - age, data = Boston)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.73400 -0.09460 -0.01771  0.09782  0.86290 

Coefficients:
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)  4.0836823  0.2030491  20.112  < 2e-16 ***
crim        -0.0103187  0.0013134  -7.856 2.49e-14 ***
zn           0.0010874  0.0005418   2.007 0.045308 *  
chas         0.1051484  0.0342285   3.072 0.002244 ** 
nox         -0.7217440  0.1416535  -5.095 4.97e-07 ***
rm           0.0906728  0.0162807   5.569 4.20e-08 ***
dis         -0.0517059  0.0074420  -6.948 1.18e-11 ***
rad          0.0134457  0.0025405   5.293 1.82e-07 ***
tax         -0.0005579  0.0001351  -4.129 4.28e-05 ***
ptratio     -0.0374259  0.0051715  -7.237 1.77e-12 ***
black        0.0004127  0.0001071   3.852 0.000133 ***
lstat       -0.0286039  0.0019002 -15.053  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1898 on 494 degrees of freedom
Multiple R-squared:  0.7891,    Adjusted R-squared:  0.7844 
F-statistic: 168.1 on 11 and 494 DF,  p-value: < 2.2e-16

\[ \widehat{log(medv)} = 4.084 - 0.01 \times crim + 0.001\times zn + 0.105\times chas -0.722\times nox + 0.091\times rm -0.052\times dis \\ + 0.013\times rad - 0.0006\times tax - 0.037\times ptratio + 0.0004\times black - 0.029\times lstat + \widehat\epsilon_i. \]

Dự báo

obs = data.frame(crim = 0.02,
                 zn = 12.5,
                 indus = 1,
                 chas = 1,
                 nox = 0.5,
                 rm = 6,
                 age = 50,
                 dis = 4,
                 rad = 4,
                 tax = 200, 
                 ptratio = 15,
                 black = 300,
                 lstat = 5)
predict(model2, obs, interval = "confidence")
       fit      lwr     upr
1 3.540173 3.458575 3.62177
exp(3.540173)
[1] 34.47288

Kiểm định chuẩn đoán

Đa cộng tuyến

library(car) 
vif(model2)
    crim       zn     chas      nox       rm      dis      rad      tax 
1.789704 2.239229 1.059819 3.778011 1.834806 3.443420 6.861126 7.272386 
 ptratio    black    lstat 
1.757681 1.341559 2.581984 

Phương sai thay đổi

library(lmtest) 
bptest(model2)

    studentized Breusch-Pagan test

data:  model2
BP = 69.355, df = 11, p-value = 1.62e-10

Tồn tại phương sai thay đổi

Kiểm tra phân phối chuẩn

library(tseries) 
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
jarque.bera.test(model2$residuals)

    Jarque Bera Test

data:  model2$residuals
X-squared = 227.36, df = 2, p-value < 2.2e-16

Phân phối chưa chuẩn