Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
#
############################################################################
# Importar datos
datos = readEViews(file.choose())
View(datos)
attach(datos)
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)
##
## 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
##
## 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
##
## Attaching package: 'zoo'
## $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
## 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
roots(VAR1) # módulo
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 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)
## 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
## $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
## 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")))
## $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
## Descomposición histórica
library(svars)
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)
grangertest(dlIndustria~dlComercio,order = 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)