29 abr 2019

Vectores Autoregresivos Estructurales (SVAR) en R

En la siguiente entrada, se coloca un ejemplo de cómo estimar SVAR en R, cuya descripción teórica en se puede obtener de Pfaf y Kronberg. El ejemplo usado fue tomado del curso de Mactroeconometría impartido por Francisco Ramírez (2017). En el mismo, se muestra un modelo para estudiar choques fiscales [DatosSVARfiscal.txt].

> library(vars)
> Datoss2 <- read.delim("DatosSVARfiscal.txt")
> str(Datoss2)
'data.frame':    101 obs. of  25 variables:
 $ TRIM          : Factor w/ 101 levels "1991Q1","1991Q2",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ PIB           : num  43.2 40.7 37.8 41.1 46.3 ...
 $ PIB_NOM       : num  32009 30842 29257 31443 35235 ...
 $ CONS_PRIV_NOM : num  24781 25009 25280 27567 28065 ...
 $ CONS_PRIV     : num  44.3 45.5 45.8 49.5 49.1 ...
 $ FBK_FIJO_NOM  : num  4137 4436 4700 4692 5053 ...
 $ GK_T          : num  1072 1257 1246 1548 1424 ...
 $ FBK_PRIV_NOM  : num  3066 3179 3453 3145 3629 ...
 $ FBK_REAL      : num  17.4 19.8 21 21.7 23.3 ...
 $ FBK_PRIV_REAL : num  15.1 16.5 18 17 19.5 ...
 $ FBK_PUB_REAL  : num  31.2 38.8 38.5 49.5 45.4 ...
 $ DEF_FBK       : num  24.1 22.8 22.7 22 22 ...
 $ IPC           : num  16.6 16.7 17 17.3 17.2 ...
 $ DEF_X         : num  100 96.6 94.4 94.8 95.2 ...
 $ DEF_M         : num  100 96.2 94.4 93.7 90.6 ...
 $ TOT           : num  100 100 100 101 105 ...
 $ DEF_FBK_      : num  100 94.5 94.4 91.2 91.4 ...
 $ FBK_PRIV_REAL_: num  100 110 119 113 130 ...
 $ FBK_PUB_REAL_ : num  100 124 123 158 145 ...

> t<-TOT/IPC*100
> g<-GK_T/IPC*100

Note que aquí asumimos que el pool de variables es integrado de primer orden, solo por ilustrar, no obstante, los ejercicios formales requieren realizar los test formales de estacionariedad.

> dataSVARfiscal<-cbind(diff(log(g)), diff(log(t)), diff(log( PIB)))
> varFiscal <- VAR(dataSVARfiscal, lag=3 )
> summary(varFiscal)
 
VAR Estimation Results:
========================= 
Endogenous variables: y1, y2, y3 
Deterministic variables: const 
Sample size: 97 
Log Likelihood: 328.483 
Roots of the characteristic polynomial:
0.8663 0.8663 0.8525 0.668 0.668 0.5717 0.3806 0.3806 0.1177
Call:
VAR(y = dataSVARfiscal, lag.max = 3)
 
Estimation results for equation y1: 
=================================== 
y1 = y1.l1 + y2.l1 + y3.l1 + y1.l2 + y2.l2 + y3.l2 + y1.l3 + y2.l3 + y3.l3 + const 
 
       Estimate Std. Error t value Pr(>|t|)
y1.l1 -0.648691   0.109000  -5.951 5.46e-08
y2.l1 -1.461169   0.702201  -2.081  0.04039
y3.l1 -0.009005   1.410575  -0.006  0.99492
y1.l2 -0.298701   0.121274  -2.463  0.01575
y2.l2  0.417021   0.722954   0.577  0.56554
y3.l2  1.780093   1.025162   1.736  0.08603
y1.l3 -0.341051   0.103402  -3.298  0.00141
y2.l3 -0.022021   0.723046  -0.030  0.97577
y3.l3  0.327109   1.330332   0.246  0.80635
const -0.023032   0.060497  -0.381  0.70435
 
Residual standard error: 0.3809 on 87 degrees of freedom
Multiple R-Squared: 0.5349,       Adjusted R-squared: 0.4868 
F-statistic: 11.12 on 9 and 87 DF,  p-value: 2.307e-11 
 
Estimation results for equation y2: 
=================================== 
y2 = y1.l1 + y2.l1 + y3.l1 + y1.l2 + y2.l2 + y3.l2 + y1.l3 + y2.l3 + y3.l3 + const 
 
        Estimate Std. Error t value Pr(>|t|)
y1.l1  0.0008346  0.0170514   0.049   0.9611
y2.l1  0.0263933  0.1098484   0.240   0.8107
y3.l1  0.0112770  0.2206623   0.051   0.9594
y1.l2  0.0049534  0.0189713   0.261   0.7946
y2.l2 -0.1297057  0.1130948  -1.147   0.2546
y3.l2  0.1002767  0.1603705   0.625   0.5334
y1.l3 -0.0057926  0.0161756  -0.358   0.7211
y2.l3 -0.0275359  0.1131091  -0.243   0.8082
y3.l3  0.0699884  0.2081096   0.336   0.7375
const -0.0246364  0.0094638  -2.603   0.0109
 
Residual standard error: 0.05959 on 87 degrees of freedom
Multiple R-Squared: 0.0317,       Adjusted R-squared: -0.06847 
F-statistic: 0.3165 on 9 and 87 DF,  p-value: 0.9676 
 
Estimation results for equation y3: 
=================================== 
y3 = y1.l1 + y2.l1 + y3.l1 + y1.l2 + y2.l2 + y3.l2 + y1.l3 + y2.l3 + y3.l3 + const 
 
       Estimate Std. Error t value Pr(>|t|)
y1.l1  0.006645   0.007680   0.865   0.3893
y2.l1  0.039474   0.049474   0.798   0.4271
y3.l1 -0.448993   0.099382  -4.518 1.96e-05
y1.l2  0.020530   0.008544   2.403   0.0184
y2.l2  0.044998   0.050936   0.883   0.3794
y3.l2 -0.647567   0.072228  -8.966 5.28e-14
y1.l3 -0.002425   0.007285  -0.333   0.7401
y2.l3  0.058697   0.050942   1.152   0.2524
y3.l3 -0.437573   0.093729  -4.668 1.09e-05
const  0.036131   0.004262   8.477 5.28e-13
 
Residual standard error: 0.02684 on 87 degrees of freedom
Multiple R-Squared: 0.5599,       Adjusted R-squared: 0.5144 
F-statistic:  12.3 on 9 and 87 DF,  p-value: 2.432e-12 
 
Covariance matrix of residuals:
         y1        y2        y3
y1 0.145114 4.875e-03 3.240e-03
y2 0.004875 3.551e-03 7.574e-05
y3 0.003240 7.574e-05 7.203e-04
 
Correlation matrix of residuals:
       y1      y2      y3
y1 1.0000 0.21473 0.31693
y2 0.2147 1.00000 0.04736
y3 0.3169 0.04736 1.00000

Una vez estimado el modelo VAR en su forma reducida y sin restringir, es necesario especificar las matrices AB, teniendo presente que al incluirse ambas matrices, el número de restricciones necesarias para estimar el modelo es k^2 + k*(k-1)/2. 

Solo por recordar, la especificación matricial del modelo estructural es:

y(t)=C+inv(A)A1*y(t-1)+…+ inv(A)Ap*y(t-p)+ inv(A)B*e(t)

> A<-matrix(c(1,0,0,
+             0,1,-1.5,
+             NA,NA,1), 3, 3, byrow = T)
> A
     [,1] [,2] [,3]
[1,]    1    0  0.0
[2,]    0    1 -1.5
[3,]   NA   NA  1.0

> B<-diag(3)
> B[1,1]<-B[2,2]<-B[3,3]<-NA
> B
     [,1] [,2] [,3]
[1,]   NA    0    0
[2,]    0   NA    0
[3,]    0    0   NA

Ahora, se estima el modelo estructural usando la función SVAR:

> svarFiscal <- SVAR(varFiscal, estmethod = "scoring",
+                Bmat = B, Amat = A, max.iter = 200)
> 
> svarFiscal
 
SVAR Estimation Results:
======================== 
 
 
Estimated A matrix:
         y1     y2   y3
y1  1.00000 0.0000  0.0
y2  0.00000 1.0000 -1.5
y3 -0.03215 0.2924  1.0
 
Estimated B matrix:
       y1      y2     y3
y1 0.3809 0.00000 0.0000
y2 0.0000 0.07032 0.0000
y3 0.0000 0.00000 0.0303

Verifique que al modelo anterior se le puede eliminar una restricción, para que quede justamente identificado.

> A<-matrix(c(1,0,0,
+             0,1,NA,
+             NA,NA,1), 3, 3, byrow = T)
> A
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1   NA
[3,]   NA   NA    1

> svarFiscal <- SVAR(varFiscal, estmethod = "scoring",
+                Bmat = B, Amat = A, max.iter = 200)
The AB-model is just identified. No test possible.
> 
> svarFiscal
 
SVAR Estimation Results:
======================== 
 
 
Estimated A matrix:
         y1     y2     y3
y1  1.00000 0.0000  0.000
y2  0.00000 1.0000 -1.504
y3 -0.03218 0.2932  1.000
 
Estimated B matrix:
       y1      y2      y3
y1 0.3809 0.00000 0.00000
y2 0.0000 0.07038 0.00000

y3 0.0000 0.00000 0.03033

20 ejemplos de Indexación de un vector en R

Entenderemos indexación acceder a una parte de un objeto. Recordemos que, en R, tenemos objetos atómicos (de un solo tipo de clases, por eje...