### Selected commands used in Chapter 2 ### > phi1=matrix(c(.2,-.6,.3,1.1),2,2) % Input phi_1 > phi1 [,1] [,2] [1,] 0.2 0.3 [2,] -0.6 1.1 > sig=matrix(c(1,0.8,0.8,2),2,2) % Input sigma_a > sig [,1] [,2] [1,] 1.0 0.8 [2,] 0.8 2.0 > m1=eigen(phi1) % Obtain eigenvalues & vectors > m1 $values [1] 0.8 0.5 $vectors [,1] [,2] [1,] -0.4472136 -0.7071068 [2,] -0.8944272 -0.7071068 > I4=diag(rep(1,4)) ## Create the 4-by-4 identity matrix > pp=kronecker(phi1,phi1) # Kronecker product > pp [,1] [,2] [,3] [,4] [1,] 0.04 0.06 0.06 0.09 [2,] -0.12 0.22 -0.18 0.33 [3,] -0.12 -0.18 0.22 0.33 [4,] 0.36 -0.66 -0.66 1.21 > c1=c(sig) > c1 [1] 1.0 0.8 0.8 2.0 > dd=I4-pp > ddinv=solve(dd) ## Obtain inverse > gam0=ddinv%*%matrix(c1,4,1) # Obtain Gamma_0 > gam0 [,1] [1,] 2.288889 [2,] 3.511111 [3,] 3.511111 [4,] 8.622222 > g0=matrix(gam0,2,2) > g1=phi1%*%g0 ## Obtain Gamma_1 > g1 [,1] [,2] [1,] 1.511111 3.288889 [2,] 2.488889 7.377778 > g2=phi1%*%g1 > g2 [,1] [,2] [1,] 1.048889 2.871111 [2,] 1.831111 6.142222 > D=diag(sqrt(diag(g0))) # To compute cross-correlation matrices > D [,1] [,2] [1,] 1.512907 0.000000 [2,] 0.000000 2.936362 > Di=solve(D) > Di%*%g0%*%Di [,1] [,2] [1,] 1.0000000 0.7903557 [2,] 0.7903557 1.0000000 > Di%*%g1%*%Di [,1] [,2] [1,] 0.6601942 0.7403332 [2,] 0.5602522 0.8556701 > Di%*%g2%*%Di [,1] [,2] [1,] 0.4582524 0.6462909 [2,] 0.4121855 0.7123711 > require(MTS) ### Load package > da=read.table("q-gdp-ukcaus.txt",header=T) > gdp=log(da[,3:5]) > dim(gdp) [1] 126 3 > z=diffM(gdp) > z=z*100 > dim(z) [1] 125 3 > Z=z[3:125,] > X=cbind(rep(1,123),z[2:124,],z[1:123,]) > X=as.matrix(X) > XPX=t(X)%*%X > XPXinv=solve(XPX) > Z=as.matrix(Z) > XPZ=t(X)%*%Z > bhat=XPXinv%*%XPZ > bhat uk ca us 0.12581630 0.123158083 0.28955814 uk 0.39306691 0.351313628 0.49069776 ca 0.10310572 0.338141505 0.24000097 us 0.05213660 0.469093555 0.23564221 uk 0.05660120 -0.191350134 -0.31195550 ca 0.10552241 -0.174833458 -0.13117863 us 0.01889462 -0.008677767 0.08531363 > A=Z-X%*%bhat > Sig=t(A)%*%A/(125-(3+1)*2-1) > Sig uk ca us uk 0.29948825 0.02814252 0.07883967 ca 0.02814252 0.30917711 0.14790523 us 0.07883967 0.14790523 0.37850674 > COV=kronecker(Sig,XPXinv) > se=sqrt(diag(COV)) > beta=c(bhat) > para=cbind(beta,se,beta/se) > colnames(para) <- c("beta","se","t-ratio") > para beta se t-ratio [1,] 0.125816304 0.07266338 1.7314953 [2,] 0.393066914 0.09341839 4.2075968 [3,] 0.103105720 0.09838425 1.0479901 [4,] 0.052136600 0.09112636 0.5721353 [5,] 0.056601196 0.09237356 0.6127424 [6,] 0.105522415 0.08755896 1.2051584 [7,] 0.018894618 0.09382091 0.2013903 [8,] 0.123158083 0.07382941 1.6681440 [9,] 0.351313628 0.09491747 3.7012536 [10,] 0.338141505 0.09996302 3.3826660 [11,] 0.469093555 0.09258865 5.0664259 [12,] -0.191350134 0.09385587 -2.0387658 [13,] -0.174833458 0.08896401 -1.9652155 [14,] -0.008677767 0.09532645 -0.0910321 [15,] 0.289558145 0.08168880 3.5446492 [16,] 0.490697759 0.10502176 4.6723437 [17,] 0.240000969 0.11060443 2.1699038 [18,] 0.235642214 0.10244504 2.3001819 [19,] -0.311955500 0.10384715 -3.0039871 [20,] -0.131178630 0.09843454 -1.3326484 [21,] 0.085313633 0.10547428 0.8088572 > #### Use the VAR command in MTS > m1=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 #### Bayesian estimation > C=0.1*diag(7) > V0=diag(3) > mm=BVAR(z,p=2,C,V0) Bayesian estimate: Est s.e. t-ratio [1,] 0.125805143 0.07123059 1.76616742 [2,] 0.392103983 0.09150764 4.28493158 [3,] 0.102894946 0.09633822 1.06805941 [4,] 0.052438976 0.08925487 0.58751947 [5,] 0.056937547 0.09048722 0.62923303 [6,] 0.105553695 0.08578002 1.23051603 [7,] 0.019147973 0.09188759 0.20838475 [8,] 0.123256168 0.07237470 1.70302833 [9,] 0.350253306 0.09297745 3.76707803 [10,] 0.337525508 0.09788562 3.44816232 [11,] 0.468440207 0.09068850 5.16537628 [12,] -0.190144541 0.09194064 -2.06812294 [13,] -0.173964344 0.08715783 -1.99596908 [14,] -0.008627966 0.09336351 -0.09241262 [15,] 0.289317667 0.07987129 3.62229886 [16,] 0.489072359 0.10260807 4.76641231 [17,] 0.239456311 0.10802463 2.21668257 [18,] 0.235601116 0.10008202 2.35408023 [19,] -0.310286945 0.10146386 -3.05810301 [20,] -0.130271750 0.09618566 -1.35437813 [21,] 0.085039470 0.10303411 0.82535258 Covariance matrix: uk ca us uk 0.28839063 0.02647455 0.07394349 ca 0.02647455 0.29772937 0.13875034 us 0.07394349 0.13875034 0.36260138 > ### VAR order specification > z1=z/100 > m2=VARorder(z1) 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 > names(m2) [1] "aic" "aicor" "bic" "bicor" "hq" "hqor" "Mstat" "Mpv" > ### Recall VAR estimation > names(m1) [1] "data" "cnst" "order" "coef" "aic" "bic" [7] "hq" "residuals" "secoef" "Sigma" "Phi" "Ph0" > resi=m1$residuals > mq(resi,adj=18) # plot not shown Ljung-Box Statistics: m Q(m) df p-value [1,] 1.000 0.816 -9.000 1.00 [2,] 2.000 3.978 0.000 1.00 [3,] 3.000 16.665 9.000 0.05 [4,] 4.000 35.122 18.000 0.01 [5,] 5.000 38.189 27.000 0.07 [6,] 6.000 41.239 36.000 0.25 [7,] 7.000 47.621 45.000 0.37 [8,] 8.000 61.677 54.000 0.22 [9,] 9.000 67.366 63.000 0.33 [10,] 10.000 76.930 72.000 0.32 [11,] 11.000 81.567 81.000 0.46 [12,] 12.000 93.112 90.000 0.39 [13,] 13.000 105.327 99.000 0.31 [14,] 14.000 116.279 108.000 0.28 [15,] 15.000 128.974 117.000 0.21 [16,] 16.000 134.704 126.000 0.28 [17,] 17.000 138.552 135.000 0.40 [18,] 18.000 146.256 144.000 0.43 [19,] 19.000 162.418 153.000 0.29 [20,] 20.000 171.948 162.000 0.28 [21,] 21.000 174.913 171.000 0.40 [22,] 22.000 182.056 180.000 0.44 [23,] 23.000 190.276 189.000 0.46 [24,] 24.000 202.141 198.000 0.41 > ### Chi-square test for parameter constraints > m3=VARchi(z,p=2) Number of targeted parameters: 8 Chi-square test and p-value: 15.16379 0.05603778 > m3=VARchi(z,p=2,thres=1.96) Number of targeted parameters: 10 Chi-square test and p-value: 31.68739 0.000451394 > ### Model simplification > m2=refVAR(m1,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 > ### Model checking > MTSdiag(m2,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: ### Plot not shown ##### Prediction > VARpred(m1,8) ## Using unconstrained model 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 > ### Impulse response functions ### Using the simplified model > Phi=m2$Phi > Sig=m2$Sigma > VARirf(Phi,Sig) Press return to continue ## Plots not shown See Figures 2.8 and 2.9 of the book > VARirf(Phi,Sig,orth=F) Press return to continue ## Plots not shown See Figures 2.6 and 2.7 of the book > ### Forecast error variance decomposition > m2=refVAR(m1) ## using default threshold Constant term: Estimates: 0.1432855 0.1222856 0.2981361 Std.Error: 0.06905416 0.07289371 0.08087773 AR coefficient matrix AR( 1 )-matrix [,1] [,2] [,3] [1,] 0.434 0.151 0.000 [2,] 0.351 0.335 0.469 [3,] 0.492 0.269 0.233 standard error [,1] [,2] [,3] [1,] 0.0817 0.0779 0.0000 [2,] 0.0945 0.0941 0.0921 [3,] 0.1049 0.1044 0.1022 AR( 2 )-matrix [,1] [,2] [,3] [1,] 0.000 0.1176 0 [2,] -0.193 -0.1781 0 [3,] -0.293 -0.0986 0 standard error [,1] [,2] [,3] [1,] 0.000 0.0790 0 [2,] 0.091 0.0808 0 [3,] 0.101 0.0897 0 Residuals cov-mtx: [,1] [,2] [,3] [1,] 0.28472577 0.02649555 0.07479875 [2,] 0.02649555 0.29160249 0.13928307 [3,] 0.07479875 0.13928307 0.35897902 det(SSE) = 0.02294983 AIC = -3.566445 BIC = -3.2723 HQ = -3.446949 > names(m2) [1] "data" "order" "cnst" "coef" "aic" "bic" [7] "hq" "residuals" "secoef" "Sigma" "Phi" "Ph0" > Phi=m2$Phi > Sig=m2$Sigma > Theta=NULL > FEVdec(Phi,Theta,Sig,lag=5) Order of the ARMA mdoel: [1] 2 0 Standard deviation of forecast error: [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.5335970 0.5902949 0.6267952 0.6535902 0.6666530 0.6716619 [2,] 0.5400023 0.7164448 0.7694647 0.7794018 0.7826851 0.7838315 [3,] 0.5991486 0.7141959 0.7363465 0.7445789 0.7473451 0.7480486 Forecast-Error-Variance Decomposition Forecast horizon: 1 [,1] [,2] [,3] [1,] 1.00000000 0.0000000 0.0000000 [2,] 0.00845528 0.9915447 0.0000000 [3,] 0.05473849 0.1686923 0.7765692 Forecast horizon: 2 [,1] [,2] [,3] [1,] 0.9811542 0.01884579 0.0000000 [2,] 0.1466467 0.73368728 0.1196661 [3,] 0.2251607 0.19873371 0.5761056 Forecast horizon: 3 [,1] [,2] [,3] [1,] 0.9276841 0.06876506 0.003550864 [2,] 0.1996736 0.66313799 0.137188439 [3,] 0.2305791 0.21070427 0.558716606 Forecast horizon: 4 [,1] [,2] [,3] [1,] 0.8916501 0.09470853 0.01364134 [2,] 0.2039474 0.65624200 0.13981062 [3,] 0.2312793 0.21703145 0.55168924 Forecast horizon: 5 [,1] [,2] [,3] [1,] 0.8758332 0.1043414 0.01982541 [2,] 0.2037927 0.6553521 0.14085523 [3,] 0.2314104 0.2185403 0.55004939 Forecast horizon: 6 [,1] [,2] [,3] [1,] 0.8690834 0.1084458 0.02247087 [2,] 0.2038019 0.6547263 0.14147175 [3,] 0.2314182 0.2188607 0.54972110 >