例子 2.1

ldl.decomp <- function(A){
  R <- chol(A)
  D <- diag(diag(R)) # 第一格diag()函数返回的是向量,第二个diag()函数把向量变成矩阵。
  Linv <- solve(t(solve(D, R))) # 第一个solve求解L,第二个solve求解L的逆矩阵。
  G <- D^2
  
  list(Linv=Linv, G=G)
}
Sigma <- rbind(c(2,1), c(1,1)); Sigma
##      [,1] [,2]
## [1,]    2    1
## [2,]    1    1
ldl.decomp(Sigma)
## $Linv
##      [,1] [,2]
## [1,]  1.0    0
## [2,] -0.5    1
## 
## $G
##      [,1] [,2]
## [1,]    2  0.0
## [2,]    0  0.5
Sigma <- rbind(c(1,1), c(1,2)); Sigma
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    2
ldl.decomp(Sigma)
## $Linv
##      [,1] [,2]
## [1,]    1    0
## [2,]   -1    1
## 
## $G
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
例 2.2
  • 考虑二元VAR(1)模型 \[ \begin{aligned} \left(\begin{array}{c} r_{1t} \\ r_{2t} \end{array}\right) = & \left(\begin{array}{c} 5 \\ 3 \end{array}\right) + \left(\begin{array}{cc} 0.2 & 0.3 \\ -0.6 & 1.1 \end{array}\right) \left(\begin{array}{c} r_{1,t-1} \\ r_{2,t-1} \end{array}\right) + \left(\begin{array}{c} a_{1t} \\ a_{2t} \end{array}\right) \\ \boldsymbol\Sigma =& \left(\begin{array}{cc} 1 & 0.8 \\ 0.8 & 2 \end{array}\right) \end{aligned} \]

计算\(\boldsymbol\Phi\)的特征值的绝对值:

abs(eigen(rbind(c(0.2, 0.3), c(-0.6, 1.1)))$value)
## [1] 0.8 0.5

英、加、美GDP的VAR建模

library(readr)
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(xts)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
da <- read_table("D:/齐安静 教学/时间序列分析/北大/ftsdata/q-gdp-ukcaus.txt")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   year = col_double(),
##   mon = col_double(),
##   uk = col_double(),
##   ca = col_double(),
##   us = col_double()
## )
ts.gdp3 <- ts(as.matrix(da[,c("uk", "ca", "us")]), 
              start=c(1980,1), frequency=4)
ts.gdp3r <- diff(log(ts.gdp3))*100
plot(as.xts(ts.gdp3r), type="l", 
     multi.panel=TRUE, theme="white",
     main="英国、加拿大、美国GDP的季度增长率(%)",
     major.ticks="years",
     grid.ticks.on = "years")

library(MTS, quietly = TRUE)
Z <- coredata(as.xts(ts.gdp3r))
m1.gdp3r <- MTS::VAR(Z, 1)
## Constant term: 
## Estimates:  0.1713324 0.1182869 0.2785892 
## Std.Error:  0.06790162 0.07193106 0.07877173 
## AR coefficient matrix 
## AR( 1 )-matrix 
##       [,1]  [,2]   [,3]
## [1,] 0.434 0.189 0.0373
## [2,] 0.185 0.245 0.3917
## [3,] 0.322 0.182 0.1674
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0811 0.0827 0.0872
## [2,] 0.0859 0.0877 0.0923
## [3,] 0.0940 0.0960 0.1011
##   
## Residuals cov-mtx: 
##            [,1]       [,2]       [,3]
## [1,] 0.28933472 0.01965508 0.06619853
## [2,] 0.01965508 0.32469319 0.16862723
## [3,] 0.06619853 0.16862723 0.38938665
##   
## det(SSE) =  0.02721916 
## AIC =  -3.459834 
## BIC =  -3.256196 
## HQ  =  -3.377107
Z <- coredata(as.xts(ts.gdp3r))
m2.gdp3r <- MTS::VAR(Z, 2)
## Constant term: 
## Estimates:  0.1258163 0.1231581 0.2895581 
## Std.Error:  0.07266338 0.07382941 0.0816888 
## AR coefficient matrix 
## AR( 1 )-matrix 
##       [,1]  [,2]   [,3]
## [1,] 0.393 0.103 0.0521
## [2,] 0.351 0.338 0.4691
## [3,] 0.491 0.240 0.2356
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0934 0.0984 0.0911
## [2,] 0.0949 0.1000 0.0926
## [3,] 0.1050 0.1106 0.1024
## AR( 2 )-matrix 
##         [,1]   [,2]     [,3]
## [1,]  0.0566  0.106  0.01889
## [2,] -0.1914 -0.175 -0.00868
## [3,] -0.3120 -0.131  0.08531
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0924 0.0876 0.0938
## [2,] 0.0939 0.0890 0.0953
## [3,] 0.1038 0.0984 0.1055
##   
## Residuals cov-mtx: 
##            [,1]       [,2]       [,3]
## [1,] 0.28244420 0.02654091 0.07435286
## [2,] 0.02654091 0.29158166 0.13948786
## [3,] 0.07435286 0.13948786 0.35696571
##   
## det(SSE) =  0.02258974 
## AIC =  -3.502259 
## BIC =  -3.094982 
## HQ  =  -3.336804

利用MTS包的VARorder函数可以计算VAR定阶的统计量检验和各种信息准则

library(MTS, quietly = TRUE)
Z <- coredata(as.xts(ts.gdp3r))
m3.gdp3r <- VARorder(Z/100)
## selected order: aic =  2 
## selected order: bic =  1 
## selected order: hq =  1 
## Summary table:  
##        p      AIC      BIC       HQ     M(p) p-value
##  [1,]  0 -30.9560 -30.9560 -30.9560   0.0000  0.0000
##  [2,]  1 -31.8830 -31.6794 -31.8003 115.1329  0.0000
##  [3,]  2 -31.9643 -31.5570 -31.7988  23.5389  0.0051
##  [4,]  3 -31.9236 -31.3127 -31.6754  10.4864  0.3126
##  [5,]  4 -31.8971 -31.0826 -31.5662  11.5767  0.2382
##  [6,]  5 -31.7818 -30.7636 -31.3682   2.7406  0.9737
##  [7,]  6 -31.7112 -30.4893 -31.2148   6.7822  0.6598
##  [8,]  7 -31.6180 -30.1925 -31.0389   4.5469  0.8719
##  [9,]  8 -31.7570 -30.1279 -31.0952  24.4833  0.0036
## [10,]  9 -31.6897 -29.8569 -30.9451   6.4007  0.6992
## [11,] 10 -31.5994 -29.5630 -30.7721   4.3226  0.8889
## [12,] 11 -31.6036 -29.3636 -30.6936  11.4922  0.2435
## [13,] 12 -31.6183 -29.1746 -30.6255  11.8168  0.2238
## [14,] 13 -31.6718 -29.0245 -30.5964  14.1266  0.1179
da1 <- coredata(as.xts(ts.gdp3r))
var1 <- vars::VAR(da1, p=1)
summary(var1)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: uk, ca, us 
## Deterministic variables: const 
## Sample size: 124 
## Log Likelihood: -304.407 
## Roots of the characteristic polynomial:
## 0.7091 0.08735 0.05004
## Call:
## vars::VAR(y = da1, p = 1)
## 
## 
## Estimation results for equation uk: 
## =================================== 
## uk = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.43435    0.08106   5.358 4.12e-07 ***
## ca.l1  0.18888    0.08275   2.282   0.0242 *  
## us.l1  0.03727    0.08716   0.428   0.6697    
## const  0.17133    0.06790   2.523   0.0129 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5468 on 120 degrees of freedom
## Multiple R-Squared: 0.3687,  Adjusted R-squared: 0.3529 
## F-statistic: 23.36 on 3 and 120 DF,  p-value: 5.596e-12 
## 
## 
## Estimation results for equation ca: 
## =================================== 
## ca = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.18499    0.08587   2.154   0.0332 *  
## ca.l1  0.24475    0.08766   2.792   0.0061 ** 
## us.l1  0.39166    0.09233   4.242 4.38e-05 ***
## const  0.11829    0.07193   1.644   0.1027    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5792 on 120 degrees of freedom
## Multiple R-Squared: 0.4685,  Adjusted R-squared: 0.4552 
## F-statistic: 35.26 on 3 and 120 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation us: 
## =================================== 
## us = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.32153    0.09404   3.419 0.000859 ***
## ca.l1  0.18196    0.09600   1.895 0.060438 .  
## us.l1  0.16740    0.10111   1.656 0.100410    
## const  0.27859    0.07877   3.537 0.000577 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.6343 on 120 degrees of freedom
## Multiple R-Squared: 0.3044,  Adjusted R-squared: 0.287 
## F-statistic:  17.5 on 3 and 120 DF,  p-value: 1.725e-09 
## 
## 
## 
## Covariance matrix of residuals:
##         uk      ca      us
## uk 0.29898 0.02031 0.06841
## ca 0.02031 0.33552 0.17425
## us 0.06841 0.17425 0.40237
## 
## Correlation matrix of residuals:
##         uk      ca     us
## uk 1.00000 0.06413 0.1972
## ca 0.06413 1.00000 0.4742
## us 0.19722 0.47424 1.0000
library(MTS, quietly = TRUE)
Z <- coredata(as.xts(ts.gdp3r))
m3.gdp3r <- VARorder(Z/100)
## selected order: aic =  2 
## selected order: bic =  1 
## selected order: hq =  1 
## Summary table:  
##        p      AIC      BIC       HQ     M(p) p-value
##  [1,]  0 -30.9560 -30.9560 -30.9560   0.0000  0.0000
##  [2,]  1 -31.8830 -31.6794 -31.8003 115.1329  0.0000
##  [3,]  2 -31.9643 -31.5570 -31.7988  23.5389  0.0051
##  [4,]  3 -31.9236 -31.3127 -31.6754  10.4864  0.3126
##  [5,]  4 -31.8971 -31.0826 -31.5662  11.5767  0.2382
##  [6,]  5 -31.7818 -30.7636 -31.3682   2.7406  0.9737
##  [7,]  6 -31.7112 -30.4893 -31.2148   6.7822  0.6598
##  [8,]  7 -31.6180 -30.1925 -31.0389   4.5469  0.8719
##  [9,]  8 -31.7570 -30.1279 -31.0952  24.4833  0.0036
## [10,]  9 -31.6897 -29.8569 -30.9451   6.4007  0.6992
## [11,] 10 -31.5994 -29.5630 -30.7721   4.3226  0.8889
## [12,] 11 -31.6036 -29.3636 -30.6936  11.4922  0.2435
## [13,] 12 -31.6183 -29.1746 -30.6255  11.8168  0.2238
## [14,] 13 -31.6718 -29.0245 -30.5964  14.1266  0.1179
da1 <- coredata(as.xts(ts.gdp3r))
var1 <- vars::VAR(da1, p=1)
summary(var1)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: uk, ca, us 
## Deterministic variables: const 
## Sample size: 124 
## Log Likelihood: -304.407 
## Roots of the characteristic polynomial:
## 0.7091 0.08735 0.05004
## Call:
## vars::VAR(y = da1, p = 1)
## 
## 
## Estimation results for equation uk: 
## =================================== 
## uk = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.43435    0.08106   5.358 4.12e-07 ***
## ca.l1  0.18888    0.08275   2.282   0.0242 *  
## us.l1  0.03727    0.08716   0.428   0.6697    
## const  0.17133    0.06790   2.523   0.0129 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5468 on 120 degrees of freedom
## Multiple R-Squared: 0.3687,  Adjusted R-squared: 0.3529 
## F-statistic: 23.36 on 3 and 120 DF,  p-value: 5.596e-12 
## 
## 
## Estimation results for equation ca: 
## =================================== 
## ca = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.18499    0.08587   2.154   0.0332 *  
## ca.l1  0.24475    0.08766   2.792   0.0061 ** 
## us.l1  0.39166    0.09233   4.242 4.38e-05 ***
## const  0.11829    0.07193   1.644   0.1027    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5792 on 120 degrees of freedom
## Multiple R-Squared: 0.4685,  Adjusted R-squared: 0.4552 
## F-statistic: 35.26 on 3 and 120 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation us: 
## =================================== 
## us = uk.l1 + ca.l1 + us.l1 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.32153    0.09404   3.419 0.000859 ***
## ca.l1  0.18196    0.09600   1.895 0.060438 .  
## us.l1  0.16740    0.10111   1.656 0.100410    
## const  0.27859    0.07877   3.537 0.000577 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.6343 on 120 degrees of freedom
## Multiple R-Squared: 0.3044,  Adjusted R-squared: 0.287 
## F-statistic:  17.5 on 3 and 120 DF,  p-value: 1.725e-09 
## 
## 
## 
## Covariance matrix of residuals:
##         uk      ca      us
## uk 0.29898 0.02031 0.06841
## ca 0.02031 0.33552 0.17425
## us 0.06841 0.17425 0.40237
## 
## Correlation matrix of residuals:
##         uk      ca     us
## uk 1.00000 0.06413 0.1972
## ca 0.06413 1.00000 0.4742
## us 0.19722 0.47424 1.0000
da1 <- coredata(as.xts(ts.gdp3r))
var2 <- vars::VAR(da1, ic="AIC", lag.max=5)
summary(var2)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: uk, ca, us 
## Deterministic variables: const 
## Sample size: 121 
## Log Likelihood: -252.647 
## Roots of the characteristic polynomial:
## 0.785 0.7516 0.7516 0.7336 0.7336 0.6144 0.6144 0.5679 0.5679 0.5165 0.5122 0.5122
## Call:
## vars::VAR(y = da1, lag.max = 5, ic = "AIC")
## 
## 
## Estimation results for equation uk: 
## =================================== 
## uk = uk.l1 + ca.l1 + us.l1 + uk.l2 + ca.l2 + us.l2 + uk.l3 + ca.l3 + us.l3 + uk.l4 + ca.l4 + us.l4 + const 
## 
##        Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.515685   0.095298   5.411  3.8e-07 ***
## ca.l1  0.071863   0.094459   0.761  0.44844    
## us.l1  0.063904   0.088668   0.721  0.47264    
## uk.l2 -0.050370   0.101228  -0.498  0.61979    
## ca.l2  0.160043   0.098620   1.623  0.10754    
## us.l2 -0.001984   0.095495  -0.021  0.98346    
## uk.l3  0.052363   0.102949   0.509  0.61205    
## ca.l3 -0.278830   0.098341  -2.835  0.00547 ** 
## us.l3  0.141145   0.093728   1.506  0.13501    
## uk.l4  0.040130   0.091050   0.441  0.66028    
## ca.l4  0.261731   0.086947   3.010  0.00325 ** 
## us.l4 -0.246485   0.088234  -2.794  0.00617 ** 
## const  0.147957   0.074786   1.978  0.05043 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5013 on 108 degrees of freedom
## Multiple R-Squared: 0.478,   Adjusted R-squared:  0.42 
## F-statistic: 8.242 on 12 and 108 DF,  p-value: 7.84e-11 
## 
## 
## Estimation results for equation ca: 
## =================================== 
## ca = uk.l1 + ca.l1 + us.l1 + uk.l2 + ca.l2 + us.l2 + uk.l3 + ca.l3 + us.l3 + uk.l4 + ca.l4 + us.l4 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.37782    0.10199   3.705 0.000336 ***
## ca.l1  0.31603    0.10109   3.126 0.002276 ** 
## us.l1  0.40963    0.09489   4.317 3.52e-05 ***
## uk.l2 -0.17399    0.10834  -1.606 0.111193    
## ca.l2 -0.25407    0.10554  -2.407 0.017771 *  
## us.l2  0.06295    0.10220   0.616 0.539247    
## uk.l3  0.09615    0.11018   0.873 0.384757    
## ca.l3  0.12035    0.10525   1.143 0.255362    
## us.l3  0.01366    0.10031   0.136 0.891925    
## uk.l4  0.07470    0.09744   0.767 0.445006    
## ca.l4 -0.09031    0.09305  -0.971 0.333959    
## us.l4 -0.09781    0.09443  -1.036 0.302622    
## const  0.07757    0.08004   0.969 0.334593    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5365 on 108 degrees of freedom
## Multiple R-Squared: 0.5661,  Adjusted R-squared: 0.5179 
## F-statistic: 11.74 on 12 and 108 DF,  p-value: 8.189e-15 
## 
## 
## Estimation results for equation us: 
## =================================== 
## us = uk.l1 + ca.l1 + us.l1 + uk.l2 + ca.l2 + us.l2 + uk.l3 + ca.l3 + us.l3 + uk.l4 + ca.l4 + us.l4 + const 
## 
##       Estimate Std. Error t value Pr(>|t|)    
## uk.l1  0.51905    0.10938   4.745 6.41e-06 ***
## ca.l1  0.17304    0.10841   1.596   0.1134    
## us.l1  0.15039    0.10177   1.478   0.1424    
## uk.l2 -0.21784    0.11618  -1.875   0.0635 .  
## ca.l2 -0.15931    0.11319  -1.407   0.1622    
## us.l2  0.22561    0.10960   2.058   0.0420 *  
## uk.l3  0.04783    0.11816   0.405   0.6864    
## ca.l3 -0.07856    0.11287  -0.696   0.4879    
## us.l3  0.07384    0.10758   0.686   0.4939    
## uk.l4  0.15409    0.10450   1.475   0.1433    
## ca.l4 -0.15182    0.09979  -1.521   0.1311    
## us.l4 -0.05350    0.10127  -0.528   0.5984    
## const  0.23868    0.08584   2.781   0.0064 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.5754 on 108 degrees of freedom
## Multiple R-Squared: 0.4531,  Adjusted R-squared: 0.3923 
## F-statistic: 7.457 on 12 and 108 DF,  p-value: 7.522e-10 
## 
## 
## 
## Covariance matrix of residuals:
##         uk      ca     us
## uk 0.25130 0.04912 0.1001
## ca 0.04912 0.28783 0.1084
## us 0.10009 0.10840 0.3310
## 
## Correlation matrix of residuals:
##        uk     ca     us
## uk 1.0000 0.1826 0.3470
## ca 0.1826 1.0000 0.3512
## us 0.3470 0.3512 1.0000
resi <- m2.gdp3r$residuals
MTS::mq(resi, adj=3^2 * 2+3)
## Ljung-Box Statistics:  
##           m       Q(m)     df    p-value
##  [1,]   1.000     0.816 -12.000     1.00
##  [2,]   2.000     3.978  -3.000     1.00
##  [3,]   3.000    16.665   6.000     1.00
##  [4,]   4.000    35.122  15.000     0.00
##  [5,]   5.000    38.189  24.000     0.03
##  [6,]   6.000    41.239  33.000     0.15
##  [7,]   7.000    47.621  42.000     0.25
##  [8,]   8.000    61.677  51.000     0.15
##  [9,]   9.000    67.366  60.000     0.24
## [10,]  10.000    76.930  69.000     0.24
## [11,]  11.000    81.567  78.000     0.37
## [12,]  12.000    93.112  87.000     0.31
## [13,]  13.000   105.327  96.000     0.24
## [14,]  14.000   116.279 105.000     0.21
## [15,]  15.000   128.974 114.000     0.16
## [16,]  16.000   134.704 123.000     0.22
## [17,]  17.000   138.552 132.000     0.33
## [18,]  18.000   146.256 141.000     0.36
## [19,]  19.000   162.418 150.000     0.23
## [20,]  20.000   171.948 159.000     0.23
## [21,]  21.000   174.913 168.000     0.34
## [22,]  22.000   182.056 177.000     0.38
## [23,]  23.000   190.276 186.000     0.40
## [24,]  24.000   202.141 195.000     0.35

library(MTS, quietly = TRUE)
Z <- coredata(as.xts(ts.gdp3r))
cat("================ Full model:\n")
## ================ Full model:
mods1.gdp3r <- VAR(Z, 2)
## Constant term: 
## Estimates:  0.1258163 0.1231581 0.2895581 
## Std.Error:  0.07266338 0.07382941 0.0816888 
## AR coefficient matrix 
## AR( 1 )-matrix 
##       [,1]  [,2]   [,3]
## [1,] 0.393 0.103 0.0521
## [2,] 0.351 0.338 0.4691
## [3,] 0.491 0.240 0.2356
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0934 0.0984 0.0911
## [2,] 0.0949 0.1000 0.0926
## [3,] 0.1050 0.1106 0.1024
## AR( 2 )-matrix 
##         [,1]   [,2]     [,3]
## [1,]  0.0566  0.106  0.01889
## [2,] -0.1914 -0.175 -0.00868
## [3,] -0.3120 -0.131  0.08531
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0924 0.0876 0.0938
## [2,] 0.0939 0.0890 0.0953
## [3,] 0.1038 0.0984 0.1055
##   
## Residuals cov-mtx: 
##            [,1]       [,2]       [,3]
## [1,] 0.28244420 0.02654091 0.07435286
## [2,] 0.02654091 0.29158166 0.13948786
## [3,] 0.07435286 0.13948786 0.35696571
##   
## det(SSE) =  0.02258974 
## AIC =  -3.502259 
## BIC =  -3.094982 
## HQ  =  -3.336804
cat("\n\n================ Restricted model:\n")
## 
## 
## ================ Restricted model:
mods2.gdp3r <- refVAR(mods1.gdp3r, thres=1.96)
## Constant term: 
## Estimates:  0.1628247 0 0.2827525 
## Std.Error:  0.06814101 0 0.07972864 
## AR coefficient matrix 
## AR( 1 )-matrix 
##       [,1]  [,2]  [,3]
## [1,] 0.467 0.207 0.000
## [2,] 0.334 0.270 0.496
## [3,] 0.468 0.225 0.232
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0790 0.0686 0.0000
## [2,] 0.0921 0.0875 0.0913
## [3,] 0.1027 0.0963 0.1023
## AR( 2 )-matrix 
##        [,1] [,2] [,3]
## [1,]  0.000    0    0
## [2,] -0.197    0    0
## [3,] -0.301    0    0
## standard error 
##        [,1] [,2] [,3]
## [1,] 0.0000    0    0
## [2,] 0.0921    0    0
## [3,] 0.1008    0    0
##   
## Residuals cov-mtx: 
##            [,1]       [,2]       [,3]
## [1,] 0.29003669 0.01803456 0.07055856
## [2,] 0.01803456 0.30802503 0.14598345
## [3,] 0.07055856 0.14598345 0.36268779
##   
## det(SSE) =  0.02494104 
## AIC =  -3.531241 
## BIC =  -3.304976 
## HQ  =  -3.439321
library(MTS, quietly = TRUE)
MTSdiag(mods2.gdp3r, adj=12)
## [1] "Covariance matrix:"
##        uk     ca     us
## uk 0.2924 0.0182 0.0711
## ca 0.0182 0.3084 0.1472
## us 0.0711 0.1472 0.3657
## CCM at lag:  0 
##        [,1]   [,2]  [,3]
## [1,] 1.0000 0.0605 0.218
## [2,] 0.0605 1.0000 0.438
## [3,] 0.2175 0.4382 1.000
## Simplified matrix: 
## CCM at lag:  1 
## . . . 
## . . . 
## . . . 
## CCM at lag:  2 
## . . . 
## . . . 
## . . . 
## CCM at lag:  3 
## . . . 
## . . . 
## . . . 
## CCM at lag:  4 
## . . - 
## . . . 
## . . . 
## CCM at lag:  5 
## . . . 
## . . . 
## . . . 
## CCM at lag:  6 
## . . . 
## . . . 
## . . . 
## CCM at lag:  7 
## . . . 
## . . . 
## . . . 
## CCM at lag:  8 
## . . . 
## . . . 
## . . . 
## CCM at lag:  9 
## . . . 
## . . . 
## . . . 
## CCM at lag:  10 
## . . . 
## . . . 
## . . . 
## CCM at lag:  11 
## . . . 
## . . . 
## . . . 
## CCM at lag:  12 
## . . . 
## . . . 
## . . . 
## CCM at lag:  13 
## . - . 
## . . . 
## . . . 
## CCM at lag:  14 
## . - . 
## . . . 
## . . . 
## CCM at lag:  15 
## . + . 
## . . . 
## . . . 
## CCM at lag:  16 
## . . . 
## . . . 
## . . . 
## CCM at lag:  17 
## . . . 
## . . . 
## . . . 
## CCM at lag:  18 
## . . . 
## . . . 
## . . . 
## CCM at lag:  19 
## . . . 
## . . + 
## . . . 
## CCM at lag:  20 
## . . . 
## . . . 
## . . . 
## CCM at lag:  21 
## . . . 
## . . . 
## . . . 
## CCM at lag:  22 
## . . . 
## . . . 
## . . . 
## CCM at lag:  23 
## . . . 
## . . . 
## . . . 
## CCM at lag:  24 
## . . . 
## . . . 
## . . .

## Hit Enter for p-value plot of individual ccm:

## Hit Enter to compute MQ-statistics: 
## 
## Ljung-Box Statistics:  
##          m       Q(m)     df    p-value
##  [1,]   1.00      1.78   -3.00     1.00
##  [2,]   2.00     12.41    6.00     1.00
##  [3,]   3.00     22.60   15.00     0.09
##  [4,]   4.00     37.71   24.00     0.04
##  [5,]   5.00     41.65   33.00     0.14
##  [6,]   6.00     44.95   42.00     0.35
##  [7,]   7.00     51.50   51.00     0.45
##  [8,]   8.00     64.87   60.00     0.31
##  [9,]   9.00     72.50   69.00     0.36
## [10,]  10.00     81.58   78.00     0.37
## [11,]  11.00     86.12   87.00     0.51
## [12,]  12.00     98.08   96.00     0.42
## [13,]  13.00    112.31  105.00     0.30
## [14,]  14.00    121.89  114.00     0.29
## [15,]  15.00    134.58  123.00     0.22
## [16,]  16.00    139.16  132.00     0.32
## [17,]  17.00    145.85  141.00     0.37
## [18,]  18.00    152.56  150.00     0.43
## [19,]  19.00    165.91  159.00     0.34
## [20,]  20.00    175.22  168.00     0.34
## [21,]  21.00    180.56  177.00     0.41
## [22,]  22.00    187.40  186.00     0.46
## [23,]  23.00    193.78  195.00     0.51
## [24,]  24.00    204.65  204.00     0.47

## Hit Enter to obtain residual plots:

- 残差的相关阵

cor(mods2.gdp3r$residuals)
##            [,1]       [,2]      [,3]
## [1,] 1.00000000 0.06054285 0.2175489
## [2,] 0.06054285 1.00000000 0.4382489
## [3,] 0.21754885 0.43824891 1.0000000

例3.4

GrangerTest(Z, p=2, locInput=3)
## Number of targeted zero parameters:  4 
## Chi-square test for Granger Causality and p-value:  27.2262 1.789152e-05
GrangerTest(Z, p=2, locInput=2)
## Number of targeted zero parameters:  4 
## Chi-square test for Granger Causality and p-value:  48.83871 6.309173e-10
GrangerTest(Z, p=2, locInput=1)
## Number of targeted zero parameters:  4 
## Chi-square test for Granger Causality and p-value:  8.948851 0.06239076 
## Constant term: 
## Estimates:  0.2104448 0.1231581 0.2895581 
## Std.Error:  0.06685632 0.07382941 0.0816888 
## AR coefficient matrix 
## AR( 1 )-matrix 
##       [,1]  [,2]  [,3]
## [1,] 0.473 0.000 0.000
## [2,] 0.351 0.338 0.469
## [3,] 0.491 0.240 0.236
## standard error 
##        [,1]  [,2]   [,3]
## [1,] 0.0899 0.000 0.0000
## [2,] 0.0949 0.100 0.0926
## [3,] 0.1050 0.111 0.1024
## AR( 2 )-matrix 
##        [,1]   [,2]     [,3]
## [1,]  0.151  0.000  0.00000
## [2,] -0.191 -0.175 -0.00868
## [3,] -0.312 -0.131  0.08531
## standard error 
##        [,1]   [,2]   [,3]
## [1,] 0.0859 0.0000 0.0000
## [2,] 0.0939 0.0890 0.0953
## [3,] 0.1038 0.0984 0.1055
##   
## Residuals cov-mtx: 
##            [,1]       [,2]       [,3]
## [1,] 0.30423344 0.02654091 0.07435286
## [2,] 0.02654091 0.29158166 0.13948786
## [3,] 0.07435286 0.13948786 0.35696571
##   
## det(SSE) =  0.02443371 
## AIC =  -3.487791 
## BIC =  -3.17102 
## HQ  =  -3.359104

例3.1中建立的三个国家的GDP增速的VAR(2)模型是基于1980年第二季度到2011年第二季度的数据, 用建立的模型进行超前1到8步预测。 第一个预测对应2011年第三季度, 最后一个预测对应2013年第二季度。

library(MTS, quietly = TRUE)
VARpred(m2.gdp3r, 8)
## orig  125 
## Forecasts at origin:  125 
##          uk      ca     us
## [1,] 0.3129 0.05166 0.1660
## [2,] 0.2647 0.31687 0.4889
## [3,] 0.3143 0.48231 0.5205
## [4,] 0.3839 0.53053 0.5998
## [5,] 0.4412 0.56978 0.6297
## [6,] 0.4799 0.59478 0.6530
## [7,] 0.5068 0.60967 0.6630
## [8,] 0.5247 0.61689 0.6688
## Standard Errors of predictions:  
##        [,1]   [,2]   [,3]
## [1,] 0.5315 0.5400 0.5975
## [2,] 0.5804 0.7165 0.7077
## [3,] 0.6202 0.7672 0.7345
## [4,] 0.6484 0.7785 0.7442
## [5,] 0.6629 0.7824 0.7475
## [6,] 0.6692 0.7838 0.7484
## [7,] 0.6719 0.7842 0.7486
## [8,] 0.6729 0.7843 0.7487
## Root mean square errors of predictions:  
##        [,1]   [,2]   [,3]
## [1,] 0.5461 0.5549 0.6140
## [2,] 0.6001 0.7799 0.7499
## [3,] 0.6365 0.7879 0.7456
## [4,] 0.6601 0.7832 0.7484
## [5,] 0.6689 0.7841 0.7488
## [6,] 0.6719 0.7844 0.7488
## [7,] 0.6730 0.7844 0.7487
## [8,] 0.6734 0.7844 0.7487
Z <- coredata(as.xts(ts.gdp3r))
colMeans(Z)
##        uk        ca        us 
## 0.5223092 0.6153672 0.6473996
Z <- coredata(as.xts(ts.gdp3r))
apply(Z, 2, sd)
##        uk        ca        us 
## 0.7086442 0.7851955 0.7872912

例3.6

library(MTS, quietly = TRUE)
VARMAirf(Phi = mods2.gdp3r$Phi, 
  Sigma = mods2.gdp3r$Sig, 
  orth=FALSE)

## Press return to continue

例3.7
  • 对例3.2中建立的简化的VAR(2)模型作正交化的脉冲响应函数图和累积响应图。
library(MTS, quietly = TRUE)
VARMAirf(Phi = mods2.gdp3r$Phi, 
  Sigma = mods2.gdp3r$Sig, 
  orth=TRUE)

## Press return to continue