Trabajo de Tesis
library(readr)
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
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(urca)
library(ggplot2)
library(kableExtra)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
##
## group_rows
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
Introducción
## Año PIB_real_growth M2_PIB Cred_Priv_PIB Tasa_Interes Inflacion
## 1 1995 0.04948548 0.2300618 0.1465464 21.11417 8.411413
## 2 1996 0.02957780 0.2307178 0.1467052 22.65833 11.056917
## 3 1997 0.04364090 0.2555227 0.1539221 18.69500 9.232903
## 4 1998 0.04993528 0.2499522 0.1704884 16.55167 6.613461
## 5 1999 0.03847062 0.2472980 0.1781391 19.49083 5.213611
## 6 2000 0.03608869 0.2637679 0.1765685 20.88083 5.977577
# Crear objeto ts excluyendo la columna "Año"
datos_ts <- ts(datos[,-1], start = 1995, frequency = 1)
datos_ts <- datos_ts[1:29, ] # 1995–2023
adf_resultados_k1 <- tibble(
Variable = colnames(datos)[-1],
p_value_nivel = sapply(datos[,-1], function(x) adf.test(x, k = 1)$p.value),
Resultado_nivel = sapply(datos[,-1], function(x) ifelse(adf.test(x, k = 1)$p.value < 0.05, "I(0)", "I(1)")),
p_value_diff = sapply(datos[,-1], function(x) adf.test(diff(x), k = 1)$p.value),
Resultado_diff = sapply(datos[,-1], function(x) ifelse(adf.test(diff(x), k = 1)$p.value < 0.05, "I(0)", "I(1)"))
)
## Warning in adf.test(x, k = 1): p-value smaller than printed p-value
## Warning in adf.test(x, k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
## Warning in adf.test(diff(x), k = 1): p-value smaller than printed p-value
kable(adf_resultados_k1, caption = "Pruebas ADF en niveles y diferencias (con k = 1)") %>%
kable_styling(full_width = FALSE)
Pruebas ADF en niveles y diferencias (con k = 1)
|
Variable
|
p_value_nivel
|
Resultado_nivel
|
p_value_diff
|
Resultado_diff
|
|
PIB_real_growth
|
0.0100000
|
I(0)
|
0.0100000
|
I(0)
|
|
M2_PIB
|
0.4469539
|
I(1)
|
0.0410911
|
I(0)
|
|
Cred_Priv_PIB
|
0.1977874
|
I(1)
|
0.0448258
|
I(0)
|
|
Tasa_Interes
|
0.0687351
|
I(1)
|
0.0100000
|
I(0)
|
|
Inflacion
|
0.0721153
|
I(1)
|
0.0100000
|
I(0)
|
datos_long <- datos %>%
pivot_longer(cols = -Año, names_to = "Variable", values_to = "Valor")
ggplot(datos_long, aes(x = Año, y = Valor)) +
geom_line() +
facet_wrap(~ Variable, scales = "free_y") +
theme_minimal() +
labs(title = "Series originales (niveles)", x = "Año", y = "")

datos_diff <- as.data.frame(apply(datos[,-1], 2, diff))
datos_diff$Año <- datos$Año[-1]
datos_diff_long <- datos_diff %>%
pivot_longer(cols = -Año, names_to = "Variable", values_to = "Valor")
ggplot(datos_diff_long, aes(x = Año, y = Valor)) +
geom_line() +
facet_wrap(~ Variable, scales = "free_y") +
theme_minimal() +
labs(title = "Series diferenciadas (primera diferencia)", x = "Año", y = "")

# -----------------------------------------------
# 📌 PASO 2: Construir base "mixta"
# -----------------------------------------------
# Diferenciar solo las variables I(1): M2_PIB y Cred_Priv_PIB
# Conservar las I(0): PIB_real_growth, Tasa_Interes, Inflacion
datos_mixto <- cbind(
PIB_real_growth = datos_ts[2:nrow(datos_ts), "PIB_real_growth"], # I(0)
dM2_PIB = diff(datos_ts[, "M2_PIB"]), # I(1)
dCred_Priv_PIB = diff(datos_ts[, "Cred_Priv_PIB"]), # I(1)
dTasa_Interes = diff(datos_ts[, "Tasa_Interes"]), # I(1)
dInflacion = diff(datos_ts[, "Inflacion"]) # I(1)
)
# -----------------------------------------------
# 📌 PASO 3: Selección del número de rezagos
# -----------------------------------------------
VARselect(datos_mixto, lag.max = 4, type = "const")
## Warning in log(sigma.det): NaNs produced
## Warning in log(sigma.det): NaNs produced
## Warning in log(sigma.det): NaNs produced
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 3 3 3 4
##
## $criteria
## 1 2 3 4
## AIC(n) -2.425186e+01 -2.493237e+01 -2.779033e+01 NaN
## HQ(n) -2.386119e+01 -2.421614e+01 -2.674854e+01 NaN
## SC(n) -2.277930e+01 -2.223266e+01 -2.386348e+01 NaN
## FPE(n) 3.097826e-11 2.148407e-11 3.391303e-12 -2.083953e-43
modelo_var <- VAR(datos_mixto, p = 3, type = "const") # Ajustar p según resultado anterior
summary(modelo_var)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: PIB_real_growth, dM2_PIB, dCred_Priv_PIB, dTasa_Interes, dInflacion
## Deterministic variables: const
## Sample size: 25
## Log Likelihood: 234.665
## Roots of the characteristic polynomial:
## 0.9214 0.9214 0.9099 0.9099 0.8239 0.8027 0.8027 0.792 0.7829 0.7829 0.7497 0.7497 0.7078 0.7078 0.04336
## Call:
## VAR(y = datos_mixto, p = 3, type = "const")
##
##
## Estimation results for equation PIB_real_growth:
## ================================================
## PIB_real_growth = PIB_real_growth.l1 + dM2_PIB.l1 + dCred_Priv_PIB.l1 + dTasa_Interes.l1 + dInflacion.l1 + PIB_real_growth.l2 + dM2_PIB.l2 + dCred_Priv_PIB.l2 + dTasa_Interes.l2 + dInflacion.l2 + PIB_real_growth.l3 + dM2_PIB.l3 + dCred_Priv_PIB.l3 + dTasa_Interes.l3 + dInflacion.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## PIB_real_growth.l1 -0.987667 0.353277 -2.796 0.020858 *
## dM2_PIB.l1 -0.067993 0.316403 -0.215 0.834642
## dCred_Priv_PIB.l1 1.148533 0.359060 3.199 0.010854 *
## dTasa_Interes.l1 0.011876 0.004400 2.699 0.024438 *
## dInflacion.l1 0.001899 0.001651 1.150 0.279789
## PIB_real_growth.l2 -0.648112 0.288678 -2.245 0.051413 .
## dM2_PIB.l2 0.670676 0.349103 1.921 0.086898 .
## dCred_Priv_PIB.l2 -0.171461 0.396799 -0.432 0.675829
## dTasa_Interes.l2 -0.006888 0.002966 -2.322 0.045333 *
## dInflacion.l2 0.001128 0.001611 0.700 0.501644
## PIB_real_growth.l3 -0.978981 0.366137 -2.674 0.025463 *
## dM2_PIB.l3 -0.589538 0.296026 -1.992 0.077608 .
## dCred_Priv_PIB.l3 1.267762 0.351855 3.603 0.005721 **
## dTasa_Interes.l3 -0.001721 0.002781 -0.619 0.551428
## dInflacion.l3 0.001424 0.001480 0.962 0.361180
## const 0.105175 0.021568 4.876 0.000876 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01041 on 9 degrees of freedom
## Multiple R-Squared: 0.8715, Adjusted R-squared: 0.6573
## F-statistic: 4.068 on 15 and 9 DF, p-value: 0.01953
##
##
## Estimation results for equation dM2_PIB:
## ========================================
## dM2_PIB = PIB_real_growth.l1 + dM2_PIB.l1 + dCred_Priv_PIB.l1 + dTasa_Interes.l1 + dInflacion.l1 + PIB_real_growth.l2 + dM2_PIB.l2 + dCred_Priv_PIB.l2 + dTasa_Interes.l2 + dInflacion.l2 + PIB_real_growth.l3 + dM2_PIB.l3 + dCred_Priv_PIB.l3 + dTasa_Interes.l3 + dInflacion.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## PIB_real_growth.l1 1.3692736 0.4763626 2.874 0.01834 *
## dM2_PIB.l1 1.1401976 0.4266406 2.673 0.02552 *
## dCred_Priv_PIB.l1 -2.2501547 0.4841604 -4.648 0.00121 **
## dTasa_Interes.l1 -0.0154452 0.0059334 -2.603 0.02859 *
## dInflacion.l1 -0.0015024 0.0022265 -0.675 0.51678
## PIB_real_growth.l2 0.7305977 0.3892559 1.877 0.09327 .
## dM2_PIB.l2 -0.9918462 0.4707339 -2.107 0.06438 .
## dCred_Priv_PIB.l2 0.9681971 0.5350471 1.810 0.10381
## dTasa_Interes.l2 0.0056107 0.0039998 1.403 0.19423
## dInflacion.l2 0.0004399 0.0021721 0.203 0.84402
## PIB_real_growth.l3 0.3541112 0.4937024 0.717 0.49141
## dM2_PIB.l3 -0.0328661 0.3991636 -0.082 0.93618
## dCred_Priv_PIB.l3 -1.6496742 0.4744450 -3.477 0.00697 **
## dTasa_Interes.l3 -0.0077839 0.0037505 -2.075 0.06777 .
## dInflacion.l3 -0.0023490 0.0019960 -1.177 0.26945
## const -0.0526229 0.0290830 -1.809 0.10383
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01403 on 9 degrees of freedom
## Multiple R-Squared: 0.8449, Adjusted R-squared: 0.5865
## F-statistic: 3.27 on 15 and 9 DF, p-value: 0.03892
##
##
## Estimation results for equation dCred_Priv_PIB:
## ===============================================
## dCred_Priv_PIB = PIB_real_growth.l1 + dM2_PIB.l1 + dCred_Priv_PIB.l1 + dTasa_Interes.l1 + dInflacion.l1 + PIB_real_growth.l2 + dM2_PIB.l2 + dCred_Priv_PIB.l2 + dTasa_Interes.l2 + dInflacion.l2 + PIB_real_growth.l3 + dM2_PIB.l3 + dCred_Priv_PIB.l3 + dTasa_Interes.l3 + dInflacion.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## PIB_real_growth.l1 0.3808273 0.3673991 1.037 0.3270
## dM2_PIB.l1 0.3492152 0.3290506 1.061 0.3162
## dCred_Priv_PIB.l1 -0.4352672 0.3734133 -1.166 0.2737
## dTasa_Interes.l1 -0.0091684 0.0045762 -2.004 0.0761 .
## dInflacion.l1 -0.0006833 0.0017172 -0.398 0.7000
## PIB_real_growth.l2 -0.1221814 0.3002173 -0.407 0.6935
## dM2_PIB.l2 -0.3394504 0.3630580 -0.935 0.3742
## dCred_Priv_PIB.l2 0.5524784 0.4126602 1.339 0.2135
## dTasa_Interes.l2 0.0024891 0.0030849 0.807 0.4405
## dInflacion.l2 0.0005929 0.0016753 0.354 0.7316
## PIB_real_growth.l3 -0.8558459 0.3807726 -2.248 0.0512 .
## dM2_PIB.l3 -0.4458237 0.3078587 -1.448 0.1815
## dCred_Priv_PIB.l3 -0.2467382 0.3659202 -0.674 0.5171
## dTasa_Interes.l3 -0.0088229 0.0028926 -3.050 0.0138 *
## dInflacion.l3 -0.0021449 0.0015394 -1.393 0.1970
## const 0.0317608 0.0224306 1.416 0.1905
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.01082 on 9 degrees of freedom
## Multiple R-Squared: 0.7743, Adjusted R-squared: 0.3981
## F-statistic: 2.058 on 15 and 9 DF, p-value: 0.1378
##
##
## Estimation results for equation dTasa_Interes:
## ==============================================
## dTasa_Interes = PIB_real_growth.l1 + dM2_PIB.l1 + dCred_Priv_PIB.l1 + dTasa_Interes.l1 + dInflacion.l1 + PIB_real_growth.l2 + dM2_PIB.l2 + dCred_Priv_PIB.l2 + dTasa_Interes.l2 + dInflacion.l2 + PIB_real_growth.l3 + dM2_PIB.l3 + dCred_Priv_PIB.l3 + dTasa_Interes.l3 + dInflacion.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## PIB_real_growth.l1 14.06480 22.44197 0.627 0.54641
## dM2_PIB.l1 -8.98941 20.09951 -0.447 0.66527
## dCred_Priv_PIB.l1 21.36186 22.80933 0.937 0.37344
## dTasa_Interes.l1 0.96505 0.27953 3.452 0.00725 **
## dInflacion.l1 0.09850 0.10489 0.939 0.37222
## PIB_real_growth.l2 1.59205 18.33827 0.087 0.93272
## dM2_PIB.l2 -6.66535 22.17679 -0.301 0.77058
## dCred_Priv_PIB.l2 -31.13868 25.20666 -1.235 0.24798
## dTasa_Interes.l2 -0.79046 0.18843 -4.195 0.00232 **
## dInflacion.l2 0.01379 0.10233 0.135 0.89580
## PIB_real_growth.l3 0.23798 23.25886 0.010 0.99206
## dM2_PIB.l3 -10.61588 18.80504 -0.565 0.58619
## dCred_Priv_PIB.l3 13.85312 22.35163 0.620 0.55077
## dTasa_Interes.l3 0.45770 0.17669 2.590 0.02919 *
## dInflacion.l3 0.13199 0.09403 1.404 0.19398
## const -0.33592 1.37013 -0.245 0.81182
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.661 on 9 degrees of freedom
## Multiple R-Squared: 0.8418, Adjusted R-squared: 0.5781
## F-statistic: 3.193 on 15 and 9 DF, p-value: 0.04182
##
##
## Estimation results for equation dInflacion:
## ===========================================
## dInflacion = PIB_real_growth.l1 + dM2_PIB.l1 + dCred_Priv_PIB.l1 + dTasa_Interes.l1 + dInflacion.l1 + PIB_real_growth.l2 + dM2_PIB.l2 + dCred_Priv_PIB.l2 + dTasa_Interes.l2 + dInflacion.l2 + PIB_real_growth.l3 + dM2_PIB.l3 + dCred_Priv_PIB.l3 + dTasa_Interes.l3 + dInflacion.l3 + const
##
## Estimate Std. Error t value Pr(>|t|)
## PIB_real_growth.l1 -40.7650 64.6798 -0.630 0.5442
## dM2_PIB.l1 -34.6113 57.9286 -0.597 0.5649
## dCred_Priv_PIB.l1 83.1239 65.7386 1.264 0.2378
## dTasa_Interes.l1 -0.8531 0.8056 -1.059 0.3172
## dInflacion.l1 -0.9701 0.3023 -3.209 0.0107 *
## PIB_real_growth.l2 33.4071 52.8526 0.632 0.5431
## dM2_PIB.l2 112.4048 63.9156 1.759 0.1125
## dCred_Priv_PIB.l2 -61.4876 72.6479 -0.846 0.4193
## dTasa_Interes.l2 1.3678 0.5431 2.519 0.0328 *
## dInflacion.l2 -0.7577 0.2949 -2.569 0.0302 *
## PIB_real_growth.l3 9.1423 67.0342 0.136 0.8945
## dM2_PIB.l3 28.9032 54.1979 0.533 0.6067
## dCred_Priv_PIB.l3 -2.6939 64.4195 -0.042 0.9676
## dTasa_Interes.l3 -1.0086 0.5092 -1.981 0.0790 .
## dInflacion.l3 -0.2556 0.2710 -0.943 0.3702
## const -2.0332 3.9489 -0.515 0.6190
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.905 on 9 degrees of freedom
## Multiple R-Squared: 0.8032, Adjusted R-squared: 0.4752
## F-statistic: 2.449 on 15 and 9 DF, p-value: 0.08872
##
##
##
## Covariance matrix of residuals:
## PIB_real_growth dM2_PIB dCred_Priv_PIB dTasa_Interes
## PIB_real_growth 1.083e-04 -7.011e-05 3.900e-05 0.0006759
## dM2_PIB -7.011e-05 1.969e-04 4.258e-05 0.0036119
## dCred_Priv_PIB 3.900e-05 4.258e-05 1.171e-04 0.0035118
## dTasa_Interes 6.759e-04 3.612e-03 3.512e-03 0.4369410
## dInflacion -2.691e-03 -8.879e-03 -1.110e-02 -0.1997499
## dInflacion
## PIB_real_growth -0.002691
## dM2_PIB -0.008879
## dCred_Priv_PIB -0.011105
## dTasa_Interes -0.199750
## dInflacion 3.629433
##
## Correlation matrix of residuals:
## PIB_real_growth dM2_PIB dCred_Priv_PIB dTasa_Interes dInflacion
## PIB_real_growth 1.00000 -0.4802 0.3464 0.09826 -0.1358
## dM2_PIB -0.48023 1.0000 0.2804 0.38943 -0.3322
## dCred_Priv_PIB 0.34638 0.2804 1.0000 0.49095 -0.5386
## dTasa_Interes 0.09826 0.3894 0.4909 1.00000 -0.1586
## dInflacion -0.13575 -0.3322 -0.5386 -0.15862 1.0000
# -----------------------------------------------
# 📌 PASO 4: Causalidad de Granger
# -----------------------------------------------
causality(modelo_var, cause = "dM2_PIB")
## $Granger
##
## Granger causality H0: dM2_PIB do not Granger-cause PIB_real_growth
## dCred_Priv_PIB dTasa_Interes dInflacion
##
## data: VAR object modelo_var
## F-Test = 1.3676, df1 = 12, df2 = 45, p-value = 0.2168
##
##
## $Instant
##
## H0: No instantaneous causality between: dM2_PIB and PIB_real_growth
## dCred_Priv_PIB dTasa_Interes dInflacion
##
## data: VAR object modelo_var
## Chi-squared = 8.9613, df = 4, p-value = 0.06207
causality(modelo_var, cause = "dCred_Priv_PIB")
## $Granger
##
## Granger causality H0: dCred_Priv_PIB do not Granger-cause
## PIB_real_growth dM2_PIB dTasa_Interes dInflacion
##
## data: VAR object modelo_var
## F-Test = 3.905, df1 = 12, df2 = 45, p-value = 0.0004064
##
##
## $Instant
##
## H0: No instantaneous causality between: dCred_Priv_PIB and
## PIB_real_growth dM2_PIB dTasa_Interes dInflacion
##
## data: VAR object modelo_var
## Chi-squared = 8.759, df = 4, p-value = 0.06741
# -----------------------------------------------
# 📌 PASO 5: Impulso-respuesta
# -----------------------------------------------
irf_m2 <- irf(modelo_var, impulse = "dM2_PIB", response = "PIB_real_growth", boot = TRUE)
plot(irf_m2)

irf_cred <- irf(modelo_var, impulse = "dCred_Priv_PIB", response = "PIB_real_growth", boot = TRUE)
plot(irf_cred)

# Autocorrelación de los residuos (Prueba de Portmanteau)
serial.test(modelo_var, lags.pt = 12, type = "PT.asym")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object modelo_var
## Chi-squared = 245.93, df = 225, p-value = 0.1614
# Normalidad de los residuos (Jarque-Bera)
normality.test(modelo_var)
## $JB
##
## JB-Test (multivariate)
##
## data: Residuals of VAR object modelo_var
## Chi-squared = 15.869, df = 10, p-value = 0.1034
##
##
## $Skewness
##
## Skewness only (multivariate)
##
## data: Residuals of VAR object modelo_var
## Chi-squared = 9.0708, df = 5, p-value = 0.1063
##
##
## $Kurtosis
##
## Kurtosis only (multivariate)
##
## data: Residuals of VAR object modelo_var
## Chi-squared = 6.7981, df = 5, p-value = 0.2361
# Estabilidad del modelo (raíz del polinomio característica)
par(mar = c(4, 4, 2, 2)) # Ajusta los márgenes
plot(stability(modelo_var, type = "OLS-CUSUM"))

# Verificación formal de estabilidad
roots <- roots(modelo_var)
all(Mod(roots) < 1) # Debe devolver TRUE si el modelo es estable
## [1] TRUE