ch0bt2 1
ch0bt2 1
August 8, 2024
# B?i toan co khoang tin cay/kiem dinh can goi cac packages sau :
library(sandwich)
library(car)
library(carData)
library(lmtest)
##
## Attaching package: 'zoo'
#library(tseries)
library(dynlm) # uoc luong khi co bien tre
#library(nardl)
#library(dLagM)
#library(vars)
library(zoo)
library(readxl)
CH0BT2=read_excel("D:/dataR/ch8/CH9BT2.xls")
CH0BT2
## # A tibble: 22 × 2
## Y X
## <dbl> <dbl>
## 1 37.0 52.8
## 2 33.6 55.9
## 3 35.4 63.0
## 4 42.4 72.9
## 5 52.5 84.8
## 6 53.7 86.6
## 7 58.5 98.8
## 8 67.5 113.
## 9 78.1 127.
## 10 95.1 144.
## # ℹ 12 more rows
Y=CH0BT2$Y
X=CH0BT2$X
X=ts(X, start=c(1970), frequency=1)
Y=ts(Y, start=c(1970), frequency=1)
plot(X)
plot(Y)
plot(X,Y)
##
## Time series regression with "ts" data:
## Start = 1971, End = 1991
##
## Call:
## dynlm(formula = Y ~ X + L(Y, 1))
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.851 -5.995 1.580 4.335 11.492
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -22.93243 4.36718 -5.251 5.41e-05 ***
## X 0.83775 0.05299 15.809 5.34e-12 ***
## L(Y, 1) 0.03620 0.06044 0.599 0.557
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.186 on 18 degrees of freedom
## Multiple R-squared: 0.9856, Adjusted R-squared: 0.984
## F-statistic: 617.5 on 2 and 18 DF, p-value: < 2.2e-16
resettest(reg0)
##
## RESET test
##
## data: reg0
## RESET = 5.5483, df1 = 2, df2 = 16, p-value = 0.01478
##
## Time series regression with "ts" data:
## Start = 1971, End = 1991
##
## Call:
## dynlm(formula = log(Y) ~ log(X) + L(log(Y), 1))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.080153 -0.045621 0.002032 0.034247 0.095286
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.40100 0.14135 -9.912 1.02e-08 ***
## log(X) 1.12735 0.06863 16.426 2.79e-12 ***
## L(log(Y), 1) 0.08569 0.05374 1.595 0.128
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.05088 on 18 degrees of freedom
## Multiple R-squared: 0.9934, Adjusted R-squared: 0.9927
## F-statistic: 1361 on 2 and 18 DF, p-value: < 2.2e-16
resettest(reg)
##
## RESET test
##
## data: reg
## RESET = 3.1762, df1 = 2, df2 = 16, p-value = 0.06892
##
## Time series regression with "ts" data:
## Start = 1971, End = 1991
##
## Call:
## dynlm(formula = log(Y) ~ L(log(X), 1) + L(log(Y), 1))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.37480 -0.04296 -0.00891 0.04588 0.57070
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.5134 0.7701 -1.965 0.0650 .
## L(log(X), 1) 1.7372 0.6298 2.758 0.0129 *
## L(log(Y), 1) -0.5333 0.5286 -1.009 0.3264
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1706 on 18 degrees of freedom
## Multiple R-squared: 0.9262, Adjusted R-squared: 0.918
## F-statistic: 112.9 on 2 and 18 DF, p-value: 6.522e-11
resettest(reg1)
##
## RESET test
##
## data: reg1
## RESET = 1.3676, df1 = 2, df2 = 16, p-value = 0.2829
##
## Time series regression with "ts" data:
## Start = 1972, End = 1991
##
## Call:
## dynlm(formula = Y ~ X + L(Y, 1:2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.395 -5.529 1.156 5.112 10.165
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -24.34955 4.59025 -5.305 7.12e-05 ***
## X 0.84710 0.05177 16.363 2.06e-11 ***
## L(Y, 1:2)1 0.11829 0.07993 1.480 0.158
## L(Y, 1:2)2 -0.09348 0.05882 -1.589 0.132
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.75 on 16 degrees of freedom
## Multiple R-squared: 0.9874, Adjusted R-squared: 0.985
## F-statistic: 416.4 on 3 and 16 DF, p-value: 2.17e-15
##
## Time series regression with "ts" data:
## Start = 1974, End = 1991
##
## Call:
## dynlm(formula = Y ~ L(Y, 1:4) + L(X, 1:4))
##
## Residuals:
## Min 1Q Median 3Q Max
## -24.384 -5.886 -0.723 2.573 47.685
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -84.7708 35.8889 -2.362 0.0425 *
## L(Y, 1:4)1 -0.1423 0.8810 -0.162 0.8752
## L(Y, 1:4)2 -2.2960 0.9276 -2.475 0.0353 *
## L(Y, 1:4)3 -1.4174 1.0879 -1.303 0.2250
## L(Y, 1:4)4 -1.6331 1.0707 -1.525 0.1615
## L(X, 1:4)1 0.5464 0.8563 0.638 0.5393
## L(X, 1:4)2 1.6084 0.8827 1.822 0.1018
## L(X, 1:4)3 1.5932 0.9863 1.615 0.1407
## L(X, 1:4)4 1.5259 1.0505 1.453 0.1803
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 21.18 on 9 degrees of freedom
## Multiple R-squared: 0.9063, Adjusted R-squared: 0.8231
## F-statistic: 10.88 on 8 and 9 DF, p-value: 0.0008298
ởng đến Y # tức là kiểm định tất cả các hệ số của X đều bằng 0. Dùng từ khóa
“matchCoefs” cho kiểm định hệ số của tất cả các biến có tên”X”
names(coef(reg6))
hypothesis = c("L(X, 1:4)1= 0", "L(X, 1:4)2 = 0", "L(X, 1:4)3 = 0",
"L(X, 1:4)4 = 0")
linearHypothesis(reg6, hypothesis)
# CÁCH KHÁC NGẮN GỌN HƠN ĐỂ KIỂM ĐỊNH X KHÔNG ẢNH HƯỞNG ĐẾN Y
MyH0_6=matchCoefs(reg6,"X")
linearHypothesis(reg6,MyH0_6)
##
## Time series regression with "ts" data:
## Start = 1974, End = 1991
##
## Call:
## dynlm(formula = X ~ L(X, 1:4) + L(Y, 1:4))
##
## Residuals:
## Min 1Q Median 3Q Max
## -23.692 -5.661 -2.443 4.171 43.894
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -68.8486 34.5408 -1.993 0.0774 .
## L(X, 1:4)1 1.0185 0.8241 1.236 0.2478
## L(X, 1:4)2 1.5202 0.8495 1.790 0.1071
## L(X, 1:4)3 1.8438 0.9493 1.942 0.0840 .
## L(X, 1:4)4 1.7053 1.0110 1.687 0.1259
## L(Y, 1:4)1 -0.5982 0.8479 -0.705 0.4984
## L(Y, 1:4)2 -2.3349 0.8927 -2.615 0.0280 *
## L(Y, 1:4)3 -1.4580 1.0470 -1.392 0.1972
## L(Y, 1:4)4 -1.9579 1.0305 -1.900 0.0899 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 20.39 on 9 degrees of freedom
## Multiple R-squared: 0.9283, Adjusted R-squared: 0.8645
## F-statistic: 14.56 on 8 and 9 DF, p-value: 0.000264
hypothesis1 <- c("L(Y, 1:4)1= 0", "L(Y, 1:4)2 = 0", "L(Y, 1:4)3 = 0",
"L(Y, 1:4)4 = 0")
linearHypothesis(reg7, hypothesis1)
library(lmtest)
# grangertest.default(X,Y, order = 4, na.action = na.omit,
data=CH0BT2)
grangertest(X,Y, order=4)
grangertest(Y,X, order=4)
##
## Time series regression with "ts" data:
## Start = 1974, End = 1991
##
## Call:
## dynlm(formula = Y ~ Z0 + Z1 + Z2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.4038 -5.7470 -0.2315 3.7626 16.5968
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -27.77482 6.50841 -4.268 0.000781 ***
## Z0 0.78566 0.05142 15.278 3.99e-10 ***
## Z1 -0.69833 0.07245 -9.639 1.47e-07 ***
## Z2 0.13140 0.01753 7.497 2.89e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.029 on 14 degrees of freedom
## Multiple R-squared: 0.9791, Adjusted R-squared: 0.9746
## F-statistic: 218.3 on 3 and 14 DF, p-value: 5.483e-12
## (Intercept) Z0 Z1 Z2
## -27.7748233 0.7856644 -0.6983303 0.1313976
# Tính các se(...) như thế nào? Sinh viên tính tiếp.