0% found this document useful (0 votes)
4 views11 pages

ch0bt2 1

Uploaded by

Tra Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

ch0bt2 1

Uploaded by

Tra Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

BÀI TẬP ARDL SỐ 2

NGUYEN QUANG DONG

August 8, 2024
# B?i toan co khoang tin cay/kiem dinh can goi cac packages sau :
library(sandwich)
library(car)

## Loading required package: carData

## Warning: package 'carData' was built under R version 4.3.2

library(carData)
library(lmtest)

## Loading required package: zoo

##
## Attaching package: 'zoo'

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


##
## as.Date, as.Date.numeric

#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)

#abline(reg = lm(Y ~ X,col="red",lwd=2))


UOC LUONG KY VONG MUC DAU TU PHU THUOC THU NHAP: NERLOVE 2

Partial adjustment model - Y = b0+b1X+ b2Y(-1)


reg0=dynlm(Y~X+L(Y,1))
summary(reg0)

##
## 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

# Partial adjustment model- Ln(Y) = b0+b1*Ln(X)+ b2*Ln(Y(-1))


reg=dynlm(log(Y)~log(X)+L(log(Y),1))
summary(reg)

##
## 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

# Ham hoi quy DAI HAN???

# CO TON TAI HIEU CHiNH BO PHAN KHONG? HAY GIAI THICH

# ĐỔI DẠNG HÀM-LERLOVE 1-Adaptive Expectations Model


reg1=dynlm(log(Y)~L(log(X),1)+L(log(Y),1))
summary (reg1)

##
## 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

# CO TON TAI HIEU CHINH BO PHAN KHONG?

ƯỚC LƯỢNG ĐẦU TƯ KỲ VỌNG PHỤ THUỘC THU NHẬP KỲ VỌNG


reg5=dynlm(Y~X+L(Y,1:2))
summary (reg5)

##
## 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

# CO TON TAI HIEU CHINH BO PHAN KHONG

KIỂM ĐỊNH QUAN HỆ NHÂN QUẢ VỚI TRỄN k=4


reg6 <- dynlm(Y ~ L(Y, 1:4) + L(X, 1:4))
summary(reg6)

##
## 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))

## [1] "(Intercept)" "L(Y, 1:4)1" "L(Y, 1:4)2" "L(Y, 1:4)3" "L(Y,


1:4)4"
## [6] "L(X, 1:4)1" "L(X, 1:4)2" "L(X, 1:4)3" "L(X, 1:4)4"

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)

## Linear hypothesis test


##
## Hypothesis:
## L(X,4)1 = 0
## L(X,4)2 = 0
## L(X,4)3 = 0
## L(X,4)4 = 0
##
## Model 1: restricted model
## Model 2: Y ~ L(Y, 1:4) + L(X, 1:4)
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 13 9495.2
## 2 9 4039.0 4 5456.3 3.0396 0.07656 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# 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)

## Linear hypothesis test


##
## Hypothesis:
## L(X,4)1 = 0
## L(X,4)2 = 0
## L(X,4)3 = 0
## L(X,4)4 = 0
##
## Model 1: restricted model
## Model 2: Y ~ L(Y, 1:4) + L(X, 1:4)
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 13 9495.2
## 2 9 4039.0 4 5456.3 3.0396 0.07656 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Kiểm định Y không ảnh hưởng đến X


reg7=dynlm(X~L(X,1:4)+L(Y,1:4))
summary(reg7)

##
## 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)

## Linear hypothesis test


##
## Hypothesis:
## L(Y,4)1 = 0
## L(Y,4)2 = 0
## L(Y,4)3 = 0
## L(Y,4)4 = 0
##
## Model 1: restricted model
## Model 2: X ~ L(X, 1:4) + L(Y, 1:4)
##
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 13 9683.5
## 2 9 3741.2 4 5942.3 3.5737 0.05209 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# KET LUAN?? TU REG6, REG

library(lmtest)
# grangertest.default(X,Y, order = 4, na.action = na.omit,
data=CH0BT2)
grangertest(X,Y, order=4)

## Granger causality test


##
## Model 1: Y ~ Lags(Y, 1:4) + Lags(X, 1:4)
## Model 2: Y ~ Lags(Y, 1:4)
## Res.Df Df F Pr(>F)
## 1 9
## 2 13 -4 3.0396 0.07656 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#Granger causality test

grangertest(Y,X, order=4)

## Granger causality test


##
## Model 1: X ~ Lags(X, 1:4) + Lags(Y, 1:4)
## Model 2: X ~ Lags(X, 1:4)
## Res.Df Df F Pr(>F)
## 1 9
## 2 13 -4 3.5737 0.05209 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

ĐA THỨC ALMON BẬC 2, TRỄ K=4 THỜI KỲ


Z0 = X+ lag(X,-1)+lag(X,-2)+lag(X,-3)+lag(X,-4)
Z1=lag(X,-1)+2*lag(X,-2)+3*lag(X,-3)+4*lag(X,-4)
Z2=lag(X,-1)+4*lag(X,-2)+9*lag(X,-3)+16*lag(X,-4)
reg8=dynlm(Y~Z0+Z1+Z2)
summary(reg8)

##
## 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

# Lay ra uoc luong cac he so cua da thuc ALmon


czhat=coef(reg8)
czhat

## (Intercept) Z0 Z1 Z2
## -27.7748233 0.7856644 -0.6983303 0.1313976

# Tinh cac he so cua mo hinh ban dau


heso=c(a=czhat[1],b0=czhat[2],b1=czhat[2]+czhat[3]+czhat[4],b2=czhat[2
]+2*czhat[3]+4*czhat[4],b3=czhat[2]+3*czhat[3]+9*czhat[4],b4=czhat[2]+
4*czhat[3]+16*czhat[4])
heso
## a.(Intercept) b0.Z0 b1.Z0 b2.Z0
b3.Z0
## -27.77482327 0.78566443 0.21873173 -0.08540570 -
0.12674785
## b4.Z0
## 0.09470528

# Tính các se(...) như thế nào? Sinh viên tính tiếp.

You might also like