Está en la página 1de 24

VAR en R

Federico Medina

26/11/2019
############################################################################
#
# MODELOS VAR EN R
#
# Macroeconometría, Universidad Externado
# Carlos Velásquez
#
#---------------------------------------------------------------------------
#
# Encadenamientos entre ramas económicas a 2017 y pronósticos
#
############################################################################

# Instalar y activar paquete que permite leer archivos de EViews y otros


formatos (binarios)
library(hexView)

# Importar datos
datos = readEViews(file.choose())

## Warning in readEViews(file.choose()): Skipping boilerplate variable

## Warning in readEViews(file.choose()): Skipping boilerplate variable

View(datos)
attach(datos)

# Definir como serie de tiempo el PIB de los sectores


Comercio = ts(COMERCIO[-(71:72)],start = c(2000,1),frequency = 4)
Industria = ts(INDUSTRIAS[-(71:72)],start = c(2000,1),frequency = 4)
Financiero = ts(FINANCIEROS[-(71:72)],start = c(2000,1),frequency = 4)
Transporte = ts(TRANSPORTE[-(71:72)],start = c(2000,1),frequency = 4)

# Gráfico de las series


ts.plot(Financiero,Comercio,Industria,Transporte,col=1:4,main="Series en
niveles")
legend(legend = c("Financiero","Comercio","Industria","Transporte"),
"topleft",lty = 1,col = 1:4)
# Gráfico estético con la librería ggplot2
library(ggplot2)

# Reorganizar datos, todos los valores en una sola columna


datos.graf = data.frame(fecha=rep(time(Financiero),4),
valor=c(Financiero,Comercio,Industria,Transporte),

Sector=c(rep("Financiero",70),rep("Comercio",70),rep("Industria",70),rep("Tra
nsporte",70)))
ggplot(datos.graf,mapping = aes(x = fecha,y = valor))+
geom_line(aes(color = Sector),size=1)+
scale_color_manual(values = c("#00AFBB","#E7B800","#FC4E07","blue"))+
ggtitle("Series en niveles")+theme_light()
# Todas las series están en niveles y muestran una tendencia.
# Estimar un VAR en niveles no es erróneo, pues no se hará inferencia
estadística sobre los coeficientes
# Sin embargo, al obtener los impulsos respuesta la interpretación se hará
compleja
# Es mejor convertirlas a estacionarias

## Estacionariedad
# Diferencias logarítmicas de las series
dlComercio = diff(log(Comercio))
dlIndustria = diff(log(Industria))
dlFinanciero = diff(log(Financiero))
dlTransporte = diff(log(Transporte))
plot(cbind(dlFinanciero,dlComercio,dlIndustria,dlTransporte),
nc=2,main="Diferencia logarítmica de las series")
# aparentemente son estacionarias, corroborarlo para Financiero
library(tseries)

## Registered S3 method overwritten by 'xts':


## method from
## as.zoo.xts zoo

## Registered S3 method overwritten by 'quantmod':


## method from
## as.zoo.data.frame zoo

pp.test(dlFinanciero,type = "Z(t_alpha)",lshort = T) # parámetro de


truncamiento corto

## Warning in pp.test(dlFinanciero, type = "Z(t_alpha)", lshort = T): p-value


## smaller than printed p-value

##
## Phillips-Perron Unit Root Test
##
## data: dlFinanciero
## Dickey-Fuller Z(t_alpha) = -7.7062, Truncation lag parameter = 3,
## p-value = 0.01
## alternative hypothesis: stationary

pp.test(dlFinanciero,type = "Z(t_alpha)",lshort = F) # parámetro de


truncamiento largo
## Warning in pp.test(dlFinanciero, type = "Z(t_alpha)", lshort = F): p-value
## smaller than printed p-value

##
## Phillips-Perron Unit Root Test
##
## data: dlFinanciero
## Dickey-Fuller Z(t_alpha) = -7.7762, Truncation lag parameter = 10,
## p-value = 0.01
## alternative hypothesis: stationary

# se rechaza hipótesis nula de existencia de raíz unitaria, es estacionaria

## Identificación del VAR, determinación de rezagos óptimos


library(vars)

## Loading required package: MASS

## Loading required package: strucchange

## Loading required package: zoo

##
## Attaching package: 'zoo'

## The following objects are masked from 'package:base':


##
## as.Date, as.Date.numeric

## Loading required package: sandwich

## Loading required package: urca

## Loading required package: lmtest

# Utilizando los criterios de información (parsimonia)


VARselect(y = cbind(dlFinanciero,dlComercio,dlIndustria,dlTransporte),
type = "const") # se incluye una constante como exógena

## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 10 1 1 1
##
## $criteria
## 1 2 3 4
## AIC(n) -3.731409e+01 -3.709321e+01 -3.720264e+01 -3.687746e+01
## HQ(n) -3.703918e+01 -3.659837e+01 -3.648787e+01 -3.594276e+01
## SC(n) -3.660984e+01 -3.582556e+01 -3.537159e+01 -3.448301e+01
## FPE(n) 6.243158e-17 7.848593e-17 7.175675e-17 1.031564e-16
## 5 6 7 8
## AIC(n) -3.673385e+01 -3.674555e+01 -3.665777e+01 -3.695364e+01
## HQ(n) -3.557922e+01 -3.537099e+01 -3.506329e+01 -3.513923e+01
## SC(n) -3.377600e+01 -3.322430e+01 -3.257312e+01 -3.230559e+01
## FPE(n) 1.268480e-16 1.382385e-16 1.743585e-16 1.596664e-16
## 9 10
## AIC(n) -3.725432e+01 -3.741402e+01
## HQ(n) -3.521998e+01 -3.515976e+01
## SC(n) -3.204287e+01 -3.163917e+01
## FPE(n) 1.589329e-16 2.069300e-16

# 3 criterios indican 1 rezago, el AIC sugiere 10

## Estimación
VAR1 = VAR(y = cbind(dlFinanciero,dlComercio,dlIndustria,dlTransporte),
type = "const",p = 1)
summary(VAR1) # los coeficientes no se interpretan, es el resultado del VAR
en forma reducida

##
## VAR Estimation Results:
## =========================
## Endogenous variables: dlFinanciero, dlComercio, dlIndustria, dlTransporte
## Deterministic variables: const
## Sample size: 68
## Log Likelihood: 889.529
## Roots of the characteristic polynomial:
## 0.351 0.351 0.2065 0.2065
## Call:
## VAR(y = cbind(dlFinanciero, dlComercio, dlIndustria, dlTransporte),
## p = 1, type = "const")
##
##
## Estimation results for equation dlFinanciero:
## =============================================
## dlFinanciero = dlFinanciero.l1 + dlComercio.l1 + dlIndustria.l1 +
dlTransporte.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## dlFinanciero.l1 0.024317 0.141947 0.171 0.865
## dlComercio.l1 0.052385 0.090527 0.579 0.565
## dlIndustria.l1 -0.047902 0.045949 -1.043 0.301
## dlTransporte.l1 0.107197 0.067968 1.577 0.120
## const 0.009537 0.001514 6.297 3.3e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.006079 on 63 degrees of freedom
## Multiple R-Squared: 0.07083, Adjusted R-squared: 0.01183
## F-statistic: 1.201 on 4 and 63 DF, p-value: 0.3194
##
##
## Estimation results for equation dlComercio:
## ===========================================
## dlComercio = dlFinanciero.l1 + dlComercio.l1 + dlIndustria.l1 +
dlTransporte.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## dlFinanciero.l1 -0.169852 0.244708 -0.694 0.490172
## dlComercio.l1 0.076511 0.156063 0.490 0.625654
## dlIndustria.l1 -0.071182 0.079213 -0.899 0.372282
## dlTransporte.l1 0.213524 0.117173 1.822 0.073158 .
## const 0.009350 0.002611 3.581 0.000666 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01048 on 63 degrees of freedom
## Multiple R-Squared: 0.06699, Adjusted R-squared: 0.007747
## F-statistic: 1.131 on 4 and 63 DF, p-value: 0.3501
##
##
## Estimation results for equation dlIndustria:
## ============================================
## dlIndustria = dlFinanciero.l1 + dlComercio.l1 + dlIndustria.l1 +
dlTransporte.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## dlFinanciero.l1 -1.407697 0.399104 -3.527 0.000790 ***
## dlComercio.l1 1.049745 0.254528 4.124 0.000111 ***
## dlIndustria.l1 -0.369398 0.129191 -2.859 0.005751 **
## dlTransporte.l1 -0.057180 0.191102 -0.299 0.765763
## const 0.014606 0.004258 3.430 0.001070 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01709 on 63 degrees of freedom
## Multiple R-Squared: 0.3357, Adjusted R-squared: 0.2936
## F-statistic: 7.96 on 4 and 63 DF, p-value: 2.933e-05
##
##
## Estimation results for equation dlTransporte:
## =============================================
## dlTransporte = dlFinanciero.l1 + dlComercio.l1 + dlIndustria.l1 +
dlTransporte.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## dlFinanciero.l1 -0.758103 0.330215 -2.296 0.0250 *
## dlComercio.l1 0.580789 0.210594 2.758 0.0076 **
## dlIndustria.l1 -0.010162 0.106892 -0.095 0.9246
## dlTransporte.l1 -0.106520 0.158116 -0.674 0.5030
## const 0.015034 0.003523 4.267 6.78e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01414 on 63 degrees of freedom
## Multiple R-Squared: 0.1336, Adjusted R-squared: 0.07862
## F-statistic: 2.429 on 4 and 63 DF, p-value: 0.05682
##
##
##
## Covariance matrix of residuals:
## dlFinanciero dlComercio dlIndustria dlTransporte
## dlFinanciero 3.695e-05 3.287e-05 4.961e-05 4.147e-05
## dlComercio 3.287e-05 1.098e-04 8.742e-05 8.310e-05
## dlIndustria 4.961e-05 8.742e-05 2.921e-04 1.297e-04
## dlTransporte 4.147e-05 8.310e-05 1.297e-04 2.000e-04
##
## Correlation matrix of residuals:
## dlFinanciero dlComercio dlIndustria dlTransporte
## dlFinanciero 1.0000 0.5160 0.4775 0.4824
## dlComercio 0.5160 1.0000 0.4880 0.5607
## dlIndustria 0.4775 0.4880 1.0000 0.5366
## dlTransporte 0.4824 0.5607 0.5366 1.0000

## Estabilidad del sistema, raíces del polinomio característico


roots(VAR1,modulus = F) # raíces

## [1] -0.3361277+0.1011135i -0.3361277-0.1011135i 0.1485831+0.1434318i


## [4] 0.1485831-0.1434318i

roots(VAR1) # módulo

## [1] 0.3510069 0.3510069 0.2065178 0.2065178

plot(roots(VAR1,modulus = F),xlim=c(-1.1,1.1),ylim=c(-1.1,1.1),asp=1)
lines(sin(seq(-6,6,0.001)),cos(seq(-6,6,0.001)))
## Evaluar autocorrelación serial de los errores (ruido blanco)

# Prueba Portmanteau, se crea una función para resumir resultados


Portmanteau.var = function(modelo,rezago.max){
Estad=c()
p.valor=c()
Estad.ajus=c()
p.valor.=c()
for (i in 1:rezago.max) {
Estad[i]=serial.test(x = modelo,lags.pt = i,type = "PT.asymptotic")
$serial$statistic
p.valor[i]=serial.test(x = modelo,lags.pt = i,type = "PT.asymptotic")
$serial$p.value
Estad.ajus[i]=serial.test(x = modelo,lags.pt = i,type = "PT.adjusted")
$serial$statistic
p.valor.[i]=serial.test(x = modelo,lags.pt = i,type = "PT.adjusted")
$serial$p.value
}
resultado=cbind(Lag=1:rezago.max,Estad,p.valor,Estad.ajus,p.valor.)
print("Prueba Portmanteau")
return(resultado)
} # crear función
Portmanteau.var(modelo = VAR1,rezago.max = 8)

## [1] "Prueba Portmanteau"


## Lag Estad p.valor Estad.ajus p.valor.
## [1,] 1 4.38060 0.0000000 4.445982 0.0000000
## [2,] 2 14.25735 0.5795484 14.622027 0.5524762
## [3,] 3 37.09429 0.2456538 38.512982 0.1985602
## [4,] 4 45.49589 0.5760317 47.439680 0.4957063
## [5,] 5 64.15064 0.4711965 67.574971 0.3560686
## [6,] 6 73.08385 0.6950608 77.372677 0.5624348
## [7,] 7 89.20631 0.6750619 95.345260 0.4996792
## [8,] 8 107.77613 0.5952780 116.391053 0.3691672

# no se debe observar el resultado del rezago equivalente al orden del modelo


(1)
# la prueba sugiere ausencia de autocorrelación serial

# Prueba Breush Godfrey LM, se crea una función para resumir resultados
LMBreush.var = function(modelo,rezago.max){
Estadístico=c()
p.valor=c()
for (i in 1:rezago.max) {
Estadístico[i]=serial.test(x = modelo,lags.bg = i,type = "BG")
$serial$statistic
p.valor[i]=serial.test(x = modelo,lags.bg = i,type = "BG")$serial$p.value
}
resultado=cbind(Lag=1:rezago.max,Estadístico,p.valor)
print("Prueba LM Breush Godfrey")
return(resultado)
} # crear función
LMBreush.var(modelo = VAR1,rezago.max = 8)

## [1] "Prueba LM Breush Godfrey"

## Lag Estadístico p.valor


## [1,] 1 18.93425 0.27208752
## [2,] 2 38.33497 0.20409172
## [3,] 3 65.41256 0.04795794
## [4,] 4 83.47404 0.05155601
## [5,] 5 95.91437 0.10838822
## [6,] 6 107.91550 0.19095576
## [7,] 7 129.99270 0.11756687
## [8,] 8 152.45340 0.06921608

# Esta prueba sugiere autocorrelación de orden 3

## Evaluar normalidad
normality.test(VAR1) # Únicamente resultados multivariados

## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 3.6045, df = 8, p-value = 0.8909
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 1.4805, df = 4, p-value = 0.8301
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 2.124, df = 4, p-value = 0.713

normality.test(VAR1,multivariate.only = F) # resultados ubivariados para JB

## $dlFinanciero
##
## JB-Test (univariate)
##
## data: Residual of dlFinanciero equation
## Chi-squared = 0.80773, df = 2, p-value = 0.6677
##
##
## $dlComercio
##
## JB-Test (univariate)
##
## data: Residual of dlComercio equation
## Chi-squared = 0.75214, df = 2, p-value = 0.6866
##
##
## $dlIndustria
##
## JB-Test (univariate)
##
## data: Residual of dlIndustria equation
## Chi-squared = 2.3229, df = 2, p-value = 0.313
##
##
## $dlTransporte
##
## JB-Test (univariate)
##
## data: Residual of dlTransporte equation
## Chi-squared = 2.353, df = 2, p-value = 0.3084
##
##
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 3.6045, df = 8, p-value = 0.8909
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 1.4805, df = 4, p-value = 0.8301
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object VAR1
## Chi-squared = 2.124, df = 4, p-value = 0.713

# no se rechaza la hipótesis nula de normalidad (p.valor del JB=0.89)

## Impulsos respuesta
plot(irf(VAR1,n.ahead = 10))
# Impulsos respuesta; choques en el sector financiero
plot(irf(VAR1,n.ahead = 10,impulse = "dlFinanciero"))
# Impulsos respuesta; choques en el sector comercio
plot(irf(VAR1,n.ahead = 10,impulse = "dlComercio",
response = c("dlComercio","dlIndustria","dlTransporte")))
# Impulsos respuesta; choques en el sector industria
plot(irf(VAR1,n.ahead = 10,impulse = "dlIndustria",
response = c("dlIndustria","dlTransporte")))

# Impulsos respuesta; choques en el sector transporte


plot(irf(VAR1,n.ahead = 10,impulse = "dlTransporte",
response = c("dlTransporte")))
## Impulsos respuesta acumulados; choques en el sector comercio
plot(irf(VAR1,n.ahead = 10,cumulative = T,impulse = "dlComercio", # se
agrega el argumento cumulative=TRUE
response = c("dlComercio","dlIndustria","dlTransporte")))
## Descomposición de la varianza del error de pronóstico
# "forecast error variance decomposition"
fevd(VAR1,n.ahead = 10)

## $dlFinanciero
## dlFinanciero dlComercio dlIndustria dlTransporte
## [1,] 1.0000000 0.00000000 0.000000000 0.00000000
## [2,] 0.9483938 0.01580361 0.002432490 0.03337008
## [3,] 0.9461713 0.01718648 0.003281686 0.03336055
## [4,] 0.9457594 0.01751948 0.003383578 0.03333754
## [5,] 0.9457176 0.01754061 0.003403023 0.03333873
## [6,] 0.9457096 0.01754451 0.003405280 0.03334061
## [7,] 0.9457082 0.01754542 0.003405508 0.03334088
## [8,] 0.9457080 0.01754550 0.003405529 0.03334094
## [9,] 0.9457080 0.01754551 0.003405530 0.03334095
## [10,] 0.9457080 0.01754551 0.003405530 0.03334095
##
## $dlComercio
## dlFinanciero dlComercio dlIndustria dlTransporte
## [1,] 0.2662675 0.7337325 0.0000000000 0.00000000
## [2,] 0.2498864 0.7047831 0.0006376473 0.04469288
## [3,] 0.2493140 0.7042633 0.0014539879 0.04496869
## [4,] 0.2494421 0.7040229 0.0015835770 0.04495142
## [5,] 0.2494347 0.7040079 0.0016089850 0.04494850
## [6,] 0.2494350 0.7040028 0.0016126584 0.04494955
## [7,] 0.2494349 0.7040022 0.0016130785 0.04494980
## [8,] 0.2494349 0.7040021 0.0016131218 0.04494987
## [9,] 0.2494349 0.7040021 0.0016131252 0.04494988
## [10,] 0.2494349 0.7040021 0.0016131254 0.04494988
##
## $dlIndustria
## dlFinanciero dlComercio dlIndustria dlTransporte
## [1,] 0.2279834 0.0795853 0.6924313 0.0000000000
## [2,] 0.2552341 0.1857977 0.5580640 0.0009042535
## [3,] 0.2523892 0.1946199 0.5494267 0.0035642456
## [4,] 0.2523748 0.1960038 0.5468217 0.0047997953
## [5,] 0.2523075 0.1962976 0.5463802 0.0050147334
## [6,] 0.2522965 0.1963271 0.5463238 0.0050525736
## [7,] 0.2522951 0.1963303 0.5463169 0.0050576833
## [8,] 0.2522949 0.1963306 0.5463163 0.0050582542
## [9,] 0.2522949 0.1963306 0.5463162 0.0050583090
## [10,] 0.2522949 0.1963306 0.5463162 0.0050583130
##
## $dlTransporte
## dlFinanciero dlComercio dlIndustria dlTransporte
## [1,] 0.2327355 0.1324652 0.05988114 0.5749182
## [2,] 0.2268368 0.2096109 0.05366646 0.5098859
## [3,] 0.2264106 0.2096211 0.05370056 0.5102677
## [4,] 0.2264181 0.2096079 0.05367654 0.5102974
## [5,] 0.2264131 0.2096153 0.05367638 0.5102952
## [6,] 0.2264129 0.2096150 0.05367673 0.5102954
## [7,] 0.2264129 0.2096150 0.05367683 0.5102953
## [8,] 0.2264129 0.2096150 0.05367685 0.5102953
## [9,] 0.2264129 0.2096150 0.05367685 0.5102953
## [10,] 0.2264129 0.2096150 0.05367685 0.5102953

fevd(VAR1,n.ahead = 10)$dlIndustria*100 # Descomposición de varianza de la


industria

## dlFinanciero dlComercio dlIndustria dlTransporte


## [1,] 22.79834 7.95853 69.24313 0.00000000
## [2,] 25.52341 18.57977 55.80640 0.09042535
## [3,] 25.23892 19.46199 54.94267 0.35642456
## [4,] 25.23748 19.60038 54.68217 0.47997953
## [5,] 25.23075 19.62976 54.63802 0.50147334
## [6,] 25.22965 19.63271 54.63238 0.50525736
## [7,] 25.22951 19.63303 54.63169 0.50576833
## [8,] 25.22949 19.63306 54.63163 0.50582542
## [9,] 25.22949 19.63306 54.63162 0.50583090
## [10,] 25.22949 19.63306 54.63162 0.50583130

fevd(VAR1,n.ahead = 10)$dlTransporte*100 # Descomposición de varianza del


transporte

## dlFinanciero dlComercio dlIndustria dlTransporte


## [1,] 23.27355 13.24652 5.988114 57.49182
## [2,] 22.68368 20.96109 5.366646 50.98859
## [3,] 22.64106 20.96211 5.370056 51.02677
## [4,] 22.64181 20.96079 5.367654 51.02974
## [5,] 22.64131 20.96153 5.367638 51.02952
## [6,] 22.64129 20.96150 5.367673 51.02954
## [7,] 22.64129 20.96150 5.367683 51.02953
## [8,] 22.64129 20.96150 5.367685 51.02953
## [9,] 22.64129 20.96150 5.367685 51.02953
## [10,] 22.64129 20.96150 5.367685 51.02953

## Descomposición histórica
library(svars)

## Registered S3 methods overwritten by 'forecast':


## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff

## Registered S3 method overwritten by 'svars':


## method from
## stability.varest vars

library(reshape2)
desc=hd(id.dc(VAR1),series = 4)$hidec
plot(hd(id.dc(VAR1),series = 4))

desc=ts(desc,start=c(2000,3),frequency=4)
desc1 = data.frame(Fecha=time(desc),
Financiero=dlTransporte[-1]*desc[,3]/desc[,2],
Comercio=dlTransporte[-1]*desc[,4]/desc[,2],
Industria=dlTransporte[-1]*desc[,5]/desc[,2],
Transporte=dlTransporte[-1]*desc[,6]/desc[,2])
desc1=melt(desc1,id.vars = 1)
names(desc1)=c("Fecha","Sector","Y")
trans=data.frame(Fecha=time(desc),Y=dlTransporte[-
1],Sector=rep("(%)dlTransporte",68))
ggplot(desc1,aes(x = Fecha,y = Y,fill=Sector))+
geom_bar(stat = "identity",position = "stack")+
geom_line(mapping = aes(x = Fecha,y = Y),data = trans,lwd=1.2)

## Don't know how to automatically pick scale for object of type ts.
Defaulting to continuous.

## Don't know how to automatically pick scale for object of type ts.
Defaulting to continuous.

## Causalidad de Granger
# Conjunta
causality(VAR1,cause = c("dlComercio","dlIndustria","dlTransporte"))$Granger

##
## Granger causality H0: dlComercio dlIndustria dlTransporte do not
## Granger-cause dlFinanciero
##
## data: VAR object VAR1
## F-Test = 1.2229, df1 = 3, df2 = 252, p-value = 0.3019
causality(VAR1,cause = c("dlFinanciero","dlIndustria","dlTransporte"))
$Granger

##
## Granger causality H0: dlFinanciero dlIndustria dlTransporte do
## not Granger-cause dlComercio
##
## data: VAR object VAR1
## F-Test = 1.1697, df1 = 3, df2 = 252, p-value = 0.3218

causality(VAR1,cause = c("dlFinanciero","dlComercio","dlTransporte"))$Granger

##
## Granger causality H0: dlFinanciero dlComercio dlTransporte do not
## Granger-cause dlIndustria
##
## data: VAR object VAR1
## F-Test = 7.3134, df1 = 3, df2 = 252, p-value = 0.0001014

causality(VAR1,cause = c("dlFinanciero","dlComercio","dlIndustria"))$Granger

##
## Granger causality H0: dlFinanciero dlComercio dlIndustria do not
## Granger-cause dlTransporte
##
## data: VAR object VAR1
## F-Test = 3.1956, df1 = 3, df2 = 252, p-value = 0.02411

# Individual
grangertest(dlComercio~dlIndustria,order = 1)

## Granger causality test


##
## Model 1: dlComercio ~ Lags(dlComercio, 1:1) + Lags(dlIndustria, 1:1)
## Model 2: dlComercio ~ Lags(dlComercio, 1:1)
## Res.Df Df F Pr(>F)
## 1 65
## 2 66 -1 0.0413 0.8395

grangertest(dlIndustria~dlComercio,order = 1)

## Granger causality test


##
## Model 1: dlIndustria ~ Lags(dlIndustria, 1:1) + Lags(dlComercio, 1:1)
## Model 2: dlIndustria ~ Lags(dlIndustria, 1:1)
## Res.Df Df F Pr(>F)
## 1 65
## 2 66 -1 7.3579 0.008536 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

grangertest(dlTransporte~dlComercio,order = 1)
## Granger causality test
##
## Model 1: dlTransporte ~ Lags(dlTransporte, 1:1) + Lags(dlComercio, 1:1)
## Model 2: dlTransporte ~ Lags(dlTransporte, 1:1)
## Res.Df Df F Pr(>F)
## 1 65
## 2 66 -1 4.0032 0.04959 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# Pronósticos
predict(VAR1,n.ahead = 2)

## $dlFinanciero
## fcst lower upper CI
## [1,] 0.01059845 -0.001316248 0.02251314 0.01191470
## [2,] 0.01099982 -0.001332884 0.02333253 0.01233271
##
## $dlComercio
## fcst lower upper CI
## [1,] 0.00852278 -0.01201743 0.02906299 0.02054021
## [2,] 0.01015102 -0.01107570 0.03137773 0.02122672
##
## $dlIndustria
## fcst lower upper CI
## [1,] 0.018073576 -0.01542620 0.05157335 0.03349978
## [2,] 0.001090267 -0.03887256 0.04105309 0.03996283
##
## $dlTransporte
## fcst lower upper CI
## [1,] 0.01515512 -0.01256229 0.04287253 0.02771741
## [2,] 0.01015155 -0.01944692 0.03975001 0.02959846

plot(predict(VAR1,n.ahead = 2),nc=2)
fanchart(predict(VAR1,n.ahead = 2),nc = 2)

También podría gustarte