Ejemplo modelo VAR

WILSON SANDOVAL

Cargar las librerias

library(vars)
library(devtools)
library(tseries)
#library(TSA)
library(urca)
library(highcharter)

.

Cargar los datos y darles formato de series de tiempo

datos <- read.csv("/cloud/project/VAR/blanchQua.csv")
gdp <- ts(datos$GDP, start = c(1948, 2), freq = 4) # gdp
une <- ts(datos$U, start = c(1948, 2), freq = 4)   # unemployment
plot(cbind(gdp, une), main="PIB y dESEMPLEO" )

hchart(cbind(gdp, une))

Pruebas de raĆ­z unitaria

verificar si hay una raĆ­z unitaria, donde notamos que podemos rechazar nulos de raĆ­z unitaria al usar la prueba Dickey-Fuller

adf.une <- ur.df(une, type = "trend", selectlags = "BIC")
summary(adf.une)   # La series es estacionaria
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.70448 -0.21077 -0.04731  0.19844  1.03067 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.0059997  0.0536401   0.112    0.911    
## z.lag.1     -0.0972035  0.0204701  -4.749 4.67e-06 ***
## tt          -0.0001167  0.0005838  -0.200    0.842    
## z.diff.lag   0.6846610  0.0595395  11.499  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3311 on 153 degrees of freedom
## Multiple R-squared:  0.4776, Adjusted R-squared:  0.4674 
## F-statistic: 46.63 on 3 and 153 DF,  p-value: < 2.2e-16
## 
## 
## Value of test-statistic is: -4.7486 7.5583 11.3199 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47

La serie de tiempo del desempleo es estacionaria.

adf.gdp <- ur.df(gdp, type = "trend", selectlags = "BIC")
summary(adf.une)   
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression trend 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.70448 -0.21077 -0.04731  0.19844  1.03067 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.0059997  0.0536401   0.112    0.911    
## z.lag.1     -0.0972035  0.0204701  -4.749 4.67e-06 ***
## tt          -0.0001167  0.0005838  -0.200    0.842    
## z.diff.lag   0.6846610  0.0595395  11.499  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3311 on 153 degrees of freedom
## Multiple R-squared:  0.4776, Adjusted R-squared:  0.4674 
## F-statistic: 46.63 on 3 and 153 DF,  p-value: < 2.2e-16
## 
## 
## Value of test-statistic is: -4.7486 7.5583 11.3199 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2  6.22  4.75  4.07
## phi3  8.43  6.49  5.47
plot(adf.gdp)

La serie de tiempo del PIB es estacionaria.

Selección y estimación del modelo

Para crear un objeto bivariado para las dos series de tiempo que modelaremos, podemos simplemente unir en columna los dos objetos existentes.

dat.bv <- cbind(gdp, une)
colnames(dat.bv) <- c("gdp", "une")
info.bv <- VARselect(dat.bv, lag.max = 12, type = "const")
info.bv
## $selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      2      2      2      2 
## 
## $criteria
##                  1           2           3           4          5           6
## AIC(n) -2.87304917 -2.97227231 -2.97044481 -2.95619395 -2.9179975 -2.92377526
## HQ(n)  -2.82345549 -2.88961617 -2.85472622 -2.80741291 -2.7361540 -2.70886931
## SC(n)  -2.75099070 -2.76884152 -2.68564170 -2.59001853 -2.4704498 -2.39485521
## FPE(n)  0.05652695  0.05118955  0.05128789  0.05203247  0.0540721  0.05378026
##                  7          8           9          10          11          12
## AIC(n) -2.88043354 -2.8338250 -2.80225128 -2.81189842 -2.81480573 -2.82165222
## HQ(n)  -2.63246514 -2.5527942 -2.48815796 -2.46474265 -2.43458751 -2.40837154
## SC(n)  -2.27014118 -2.1421604 -2.02921428 -1.95748911 -1.87902411 -1.80449828
## FPE(n)  0.05619048  0.0589099  0.06085003  0.06032776  0.06022773  0.05990607
info.bv$selection   
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      2      2      2      2

Todos los criterios de información sugieren el uso de 2 rezagos sería apropiado, \(p=2\)

modelo <- VAR(dat.bv, p = 2 ,type = "const", season = NULL, exog = NULL)
summary(modelo)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: gdp, une 
## Deterministic variables: const 
## Sample size: 157 
## Log Likelihood: -211.593 
## Roots of the characteristic polynomial:
## 0.8361 0.8361 0.1014 0.1014
## Call:
## VAR(y = dat.bv, p = 2, type = "const", exogen = NULL)
## 
## 
## Estimation results for equation gdp: 
## ==================================== 
## gdp = gdp.l1 + une.l1 + gdp.l2 + une.l2 + const 
## 
##        Estimate Std. Error t value Pr(>|t|)   
## gdp.l1  0.06905    0.10782   0.640  0.52289   
## une.l1 -0.61530    0.30723  -2.003  0.04699 * 
## gdp.l2  0.04741    0.09098   0.521  0.60309   
## une.l2  0.90193    0.31426   2.870  0.00469 **
## const  -0.01570    0.07684  -0.204  0.83833   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.9613 on 152 degrees of freedom
## Multiple R-Squared: 0.2687,  Adjusted R-squared: 0.2495 
## F-statistic: 13.97 on 4 and 152 DF,  p-value: 1e-09 
## 
## 
## Estimation results for equation une: 
## ==================================== 
## une = gdp.l1 + une.l1 + gdp.l2 + une.l2 + const 
## 
##         Estimate Std. Error t value Pr(>|t|)    
## gdp.l1 -0.119156   0.035975  -3.312 0.001157 ** 
## une.l1  1.333482   0.102510  13.008  < 2e-16 ***
## gdp.l2 -0.029883   0.030358  -0.984 0.326497    
## une.l2 -0.416951   0.104853  -3.977 0.000108 ***
## const  -0.007501   0.025639  -0.293 0.770243    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.3207 on 152 degrees of freedom
## Multiple R-Squared: 0.9429,  Adjusted R-squared: 0.9414 
## F-statistic: 628.1 on 4 and 152 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##         gdp     une
## gdp  0.9240 -0.2022
## une -0.2022  0.1029
## 
## Correlation matrix of residuals:
##         gdp     une
## gdp  1.0000 -0.6558
## une -0.6558  1.0000
roots(modelo)
## [1] 0.8360669 0.8360669 0.1013916 0.1013916

Evaluación del Modelo

bv.serial <- serial.test(modelo, lags.pt = 12, type = "PT.asymptotic")
bv.serial
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object modelo
## Chi-squared = 46.579, df = 40, p-value = 0.22

\(p\)-valor mayor 0.05 indica ausencia de correlación serial

bv.arch <- arch.test(modelo, lags.multi = 12, multivariate.only = TRUE)
bv.arch
## 
##  ARCH (multivariate)
## 
## data:  Residuals of VAR object modelo
## Chi-squared = 108.4, df = 108, p-value = 0.4712

\(p\)-valor mayor al 5% indicates ausencia de heterocedasticidad

bv.norm <- normality.test(modelo, multivariate.only = FALSE)
bv.norm
## $gdp
## 
##  JB-Test (univariate)
## 
## data:  Residual of gdp equation
## Chi-squared = 1.0976, df = 2, p-value = 0.5776
## 
## 
## $une
## 
##  JB-Test (univariate)
## 
## data:  Residual of une equation
## Chi-squared = 16.473, df = 2, p-value = 0.0002648
## 
## 
## $JB
## 
##  JB-Test (multivariate)
## 
## data:  Residuals of VAR object modelo
## Chi-squared = 2.3194, df = 4, p-value = 0.6772
## 
## 
## $Skewness
## 
##  Skewness only (multivariate)
## 
## data:  Residuals of VAR object modelo
## Chi-squared = 2.1157, df = 2, p-value = 0.3472
## 
## 
## $Kurtosis
## 
##  Kurtosis only (multivariate)
## 
## data:  Residuals of VAR object modelo
## Chi-squared = 0.20376, df = 2, p-value = 0.9031

p-value indica que los residuales son normales

aplicar una prueba CUSUM.

bv.cusum <- stability(modelo, type = "OLS-CUSUM")
plot(bv.cusum)

No parece haber una ruptura en los respectivos intervalos de confianza.

Causalidad de Granger, IRF y descomposiciones de varianza

bv.cause.gdp <- causality(modelo, cause = "gdp")
bv.cause.gdp
## $Granger
## 
##  Granger causality H0: gdp do not Granger-cause une
## 
## data:  VAR object modelo
## F-Test = 5.5723, df1 = 2, df2 = 304, p-value = 0.0042
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: gdp and une
## 
## data:  VAR object modelo
## Chi-squared = 47.214, df = 1, p-value = 6.364e-12

Segun los resultados el crecimiento trimestral de pdp causa Granger el crecimiento trimestral de une, al menos en nuestro periodo de estimación.

bv.cause.une <- causality(modelo, cause = "une")
bv.cause.une
## $Granger
## 
##  Granger causality H0: une do not Granger-cause gdp
## 
## data:  VAR object modelo
## F-Test = 12.821, df1 = 2, df2 = 304, p-value = 4.511e-06
## 
## 
## $Instant
## 
##  H0: No instantaneous causality between: une and gdp
## 
## data:  VAR object modelo
## Chi-squared = 47.214, df = 1, p-value = 6.364e-12

El crecimiento trimestral de los desempleo causa Granger el crecimiento trimestral del gdp.

Para generar funciones de respuesta a impulsos para describir la respuesta del producto a un shock de desempleo, procedemos de la siguiente manera:

irf.gdp <- irf(modelo, impulse = "une", response = "gdp",n.ahead = 40, boot = TRUE)
plot(irf.gdp) 

Un choque positivo al desempleo tiene un efecto negativo en la salida (es decir, un menor poder adquisitivo).

Para considerar la respuesta del desempleo al shock de salida,

irf.une <- irf(modelo, impulse = "gdp", response = "une",n.ahead = 40, boot = TRUE)
plot(irf.une) # reponse of unemployment to gdp shock

# positive shock to output decreases unemployment by large and persistent amount

AquĆ­ observamos que un shock positivo en GDP disminuye el desempleo en una cantidad relativamente grande y persistente.

irf.une_un <- irf(modelo, impulse = "une", response = "une",n.ahead = 40, boot = TRUE)
plot(irf.une, ylab = "unemployment", main = "Shock from GDP", xlab = "")

plot(irf.une_un, ylab = "unemployment", main = "Shock from unemployment")

bv.vardec <- fevd(modelo, n.ahead = 10)
plot(bv.vardec)

gdp estĆ” determinado en gran medida por las perturbaciones del pib, mientras que el desempleo estĆ” influenciado por ambas perturbaciones

Pronósticos

Para pronosticar hacia adelante podemos usar el comando \(\textit{predict}\), donde en este caso pronosticamos 8 pasos adelante. También buscamos utilizar intervalos de confianza del 95% para el pronóstico

predictions <- predict(modelo, n.ahead = 8, ci = 0.95)
plot(predictions, names = "gdp")

plot(predictions, names = "une")

fanchart(predictions, names = "gdp")

fanchart(predictions, names = "une")