library("vars",quietly =T)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

Time Series

Para seus dados use: dados<-ts(seus_dados,start = c(1961, 1), frequency = 12)

Importando uma base de dados:

data(Canada)

Modelo VAR:

model_var<- VAR(Canada, ## série temporal
                p = 2, ## lag
                type = "const" #const, trend or both
                )
print(model_var)
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation e: 
## ====================================== 
## Call:
## e = e.l1 + prod.l1 + rw.l1 + U.l1 + e.l2 + prod.l2 + rw.l2 + U.l2 + const 
## 
##          e.l1       prod.l1         rw.l1          U.l1          e.l2 
##  1.637821e+00  1.672717e-01 -6.311863e-02  2.655848e-01 -4.971338e-01 
##       prod.l2         rw.l2          U.l2         const 
## -1.016501e-01  3.844492e-03  1.326893e-01 -1.369984e+02 
## 
## 
## Estimated coefficients for equation prod: 
## ========================================= 
## Call:
## prod = e.l1 + prod.l1 + rw.l1 + U.l1 + e.l2 + prod.l2 + rw.l2 + U.l2 + const 
## 
##         e.l1      prod.l1        rw.l1         U.l1         e.l2      prod.l2 
##   -0.1727658    1.1504282    0.0513039   -0.4785013    0.3852589   -0.1724119 
##        rw.l2         U.l2        const 
##   -0.1188510    1.0159180 -166.7755177 
## 
## 
## Estimated coefficients for equation rw: 
## ======================================= 
## Call:
## rw = e.l1 + prod.l1 + rw.l1 + U.l1 + e.l2 + prod.l2 + rw.l2 + U.l2 + const 
## 
##          e.l1       prod.l1         rw.l1          U.l1          e.l2 
##  -0.268832871  -0.081065001   0.895478330   0.012130033   0.367848941 
##       prod.l2         rw.l2          U.l2         const 
##  -0.005180947   0.052676565  -0.127708256 -33.188338774 
## 
## 
## Estimated coefficients for equation U: 
## ====================================== 
## Call:
## U = e.l1 + prod.l1 + rw.l1 + U.l1 + e.l2 + prod.l2 + rw.l2 + U.l2 + const 
## 
##         e.l1      prod.l1        rw.l1         U.l1         e.l2      prod.l2 
##  -0.58076382  -0.07811707   0.01866214   0.61893150   0.40981822   0.05211668 
##        rw.l2         U.l2        const 
##   0.04180115  -0.07116885 149.78056487
plot(model_var)

Teste de normalidade

The multivariate version of this test is computed by using the residuals that are standardized by a Choleski decomposition of the variance-covariance matrix for the centered residuals.

normality.test(model_var)
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 5.094, df = 8, p-value = 0.7475
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 1.7761, df = 4, p-value = 0.7769
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object model_var
## Chi-squared = 3.3179, df = 4, p-value = 0.5061

Teste de Causalidade

  • Opção 1: a robust HC variance-covariance matrix for the Granger test:
granger_rhc<-causality(model_var, ## modelo VAR(p)
                       cause = "e", ## variavel de interesse
                       vcov.=vcovHC(model_var), 
                       boot=FALSE
                       )
granger_rhc
## $Granger
## 
##  Granger causality H0: e do not Granger-cause prod rw U
## 
## data:  VAR object model_var
## F-Test = 3.3923, df1 = 6, df2 = 292, p-value = 0.002994
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: e and prod rw U
## 
## data:  VAR object model_var
## Chi-squared = 26.068, df = 3, p-value = 9.228e-06
  • Opção 2: use a wild-bootstrap procedure to for the Granger test:
granger_boot<-causality(model_var, ## modelo VAR(p)
                        cause = "e", ## variavel de interesse
                        boot=TRUE,
                        boot.runs=1000
                        )
granger_boot
## $Granger
## 
##  Granger causality H0: e do not Granger-cause prod rw U
## 
## data:  VAR object model_var
## F-Test = 6.2768, boot.runs = 1000, p-value = 0.004
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: e and prod rw U
## 
## data:  VAR object model_var
## Chi-squared = 26.068, df = 3, p-value = 9.228e-06

Modelo SVAR

Matriz diagonal

Matriz diagonal com dimensão igual ao número de variáveis do modelo, e com a diagonal principal como “NA”.

amat <- diag(4) 
diag(amat) <- NA 
amat
##      [,1] [,2] [,3] [,4]
## [1,]   NA    0    0    0
## [2,]    0   NA    0    0
## [3,]    0    0   NA    0
## [4,]    0    0    0   NA

SVAR model - Estimation direct method

Estimating the SVAR-model with the directly minimizing the negative log-likelihood.

model_svar <-SVAR(x = model_var, 
                  estmethod = "direct", 
                  Amat = amat, 
                  Bmat = NULL,
                  hessian = TRUE, 
                  method= "BFGS"
                  ) 
print(model_svar)
## 
## SVAR Estimation Results:
## ======================== 
## 
## 
## Estimated A matrix:
##          e  prod    rw     U
## e    2.756 0.000 0.000 0.000
## prod 0.000 1.533 0.000 0.000
## rw   0.000 0.000 1.282 0.000
## U    0.000 0.000 0.000 3.576

Impulse Response using SVAR

A partir do modelo SVAR estimado acima, defina as variaveis para impulso e para resposta:

impulso_resposta <- irf(model_svar, #modelo SVAR
                        impulse = "e",  #impulse variables.
                        response = c("prod","rw","U"), #response variables.
                        n.ahead = 8, #passos a frente
                        ortho = FALSE, #tipo ortogonal (contemporaneo)
                        runs = 1000, #quantas simulacoes fazer (bootstrap)
                        ci= 0.95,
                        cumulative= TRUE #cumulative    
                        ) 

print(impulso_resposta)
## 
## Impulse response coefficients
## $e
##               prod          rw          U
##  [1,]  0.000000000  0.00000000  0.0000000
##  [2,] -0.062682030 -0.09753660 -0.2107098
##  [3,] -0.001856481 -0.20863945 -0.5344652
##  [4,]  0.128038245 -0.27324705 -0.9159506
##  [5,]  0.272167463 -0.26034152 -1.3141711
##  [6,]  0.394680480 -0.14807563 -1.6967827
##  [7,]  0.474995921  0.07570693 -2.0412242
##  [8,]  0.504780655  0.41395617 -2.3337765
##  [9,]  0.484388446  0.86199943 -2.5680118
## 
## 
## Lower Band, CI= 0.95 
## $e
##             prod         rw          U
##  [1,]  0.0000000  0.0000000  0.0000000
##  [2,] -0.2412703 -0.2924188 -0.2880805
##  [3,] -0.3780241 -0.5475483 -0.6952078
##  [4,] -0.5177550 -0.7745201 -1.1601256
##  [5,] -0.6626419 -0.9442921 -1.6415361
##  [6,] -0.8596586 -1.0609459 -2.1206938
##  [7,] -1.1426122 -1.0713947 -2.5381870
##  [8,] -1.4698517 -1.0361666 -2.9407216
##  [9,] -1.7906062 -0.8505542 -3.2458957
## 
## 
## Upper Band, CI= 0.95 
## $e
##            prod        rw          U
##  [1,] 0.0000000 0.0000000  0.0000000
##  [2,] 0.1242975 0.1430459 -0.1111069
##  [3,] 0.4301492 0.3212939 -0.2936590
##  [4,] 0.8351835 0.5731560 -0.4859137
##  [5,] 1.2947294 0.9307001 -0.6706322
##  [6,] 1.7125805 1.3591838 -0.7754324
##  [7,] 2.1171013 1.9148577 -0.8557885
##  [8,] 2.4742636 2.5262705 -0.9090738
##  [9,] 2.7481305 3.1436031 -0.8986217

Plot

plot(impulso_resposta)

Summary statistics

model_summary <- summary(model_svar)
model_summary 
## 
## SVAR Estimation Results:
## ======================== 
## 
## Call:
## SVAR(x = model_var, estmethod = "direct", Amat = amat, Bmat = NULL, 
##     hessian = TRUE, method = "BFGS")
## 
## Type: A-model 
## Sample size: 82 
## Log Likelihood: -222.436 
## Method: direct 
## Number of iterations: 52 
## Convergence code: 0 
## 
## LR overidentification test:
## 
##  LR overidentification
## 
## data:  Canada
## Chi^2 = 55, df = 6, p-value = 4e-10
## 
## 
## Estimated A matrix:
##          e  prod    rw     U
## e    2.756 0.000 0.000 0.000
## prod 0.000 1.533 0.000 0.000
## rw   0.000 0.000 1.282 0.000
## U    0.000 0.000 0.000 3.576
## 
## Estimated standard errors for A matrix:
##           e   prod     rw      U
## e    0.2152 0.0000 0.0000 0.0000
## prod 0.0000 0.1197 0.0000 0.0000
## rw   0.0000 0.0000 0.1001 0.0000
## U    0.0000 0.0000 0.0000 0.2792
## 
## Estimated B matrix:
##      e prod rw U
## e    1    0  0 0
## prod 0    1  0 0
## rw   0    0  1 0
## U    0    0  0 1
## 
## Covariance matrix of reduced form residuals (*100):
##          e  prod    rw     U
## e    13.16  0.00  0.00 0.000
## prod  0.00 42.57  0.00 0.000
## rw    0.00  0.00 60.89 0.000
## U     0.00  0.00  0.00 7.821

Variance-Covariance Matrix

The variance-covariance matrix of the reduced form residuals times

cov_mat<-model_summary$Sigma.U
cov_mat
##             e     prod       rw        U
## e    13.16347  0.00000  0.00000 0.000000
## prod  0.00000 42.57107  0.00000 0.000000
## rw    0.00000  0.00000 60.88582 0.000000
## U     0.00000  0.00000  0.00000 7.820998

Cholesky decomposition

Compute the Cholesky factorization of a real symmetric positive-definite square matrix.

chol_mat<-t(chol(model_summary$Sigma.U))
chol_mat
##            e     prod       rw        U
## e    3.62815 0.000000 0.000000 0.000000
## prod 0.00000 6.524651 0.000000 0.000000
## rw   0.00000 0.000000 7.802937 0.000000
## U    0.00000 0.000000 0.000000 2.796605

The lower triangular factor of the Cholesky decomposition, i.e., the matrix R such that R′R=x.