Los modelos SARIMAX
library(readxl)
library(tseries)
library(stargazer)
library(lmtest)
library(car)
library(strucchange)
library(TSstudio)
library(forecast)
library(urca)
# Base de datos
data_sarimax <- read_excel("data_sarimax.xlsx", sheet = "data3")
head(data_sarimax)
Se declara la serie de tiempo.
# Tasa de crecimiento logaritmica del consumo privado
tlcp <- ts(data_sarimax[ , 2],
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
plot(tlcp)
# Tasa de crecimiento logaritmica del PIB
tly <- ts(data_sarimax[ , 3],
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
plot(tly)
# Tasa de crecimiento logaritmica de la inversion privada
tli <- ts(data_sarimax[ , 4],
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
plot(tli)
# Tasa de crecimiento logaritmica de la inflacion
tlipc <- ts(data_sarimax[ , 5],
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
plot(tlipc)
# Tasa de crecimiento logaritmica de la inflacion
tlr <- ts(data_sarimax[ , 6],
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
plot(tlr)
# Descomposicion de la serie
ts_decompose(tlcp)
Para confirmar la estacionaridad de las series se usan las pruebas de Dickey-Fuller (ADF) y Phillips-Perron (PP) para raíz unitaria, donde:
# Prueba de Dickey-Fuller (ADF)
adf.test(tlcp)
##
## Augmented Dickey-Fuller Test
##
## data: tlcp
## Dickey-Fuller = -3.3181, Lag order = 4, p-value = 0.07183
## alternative hypothesis: stationary
adf.test(tli)
##
## Augmented Dickey-Fuller Test
##
## data: tli
## Dickey-Fuller = -3.0642, Lag order = 4, p-value = 0.1352
## alternative hypothesis: stationary
adf.test(tly)
##
## Augmented Dickey-Fuller Test
##
## data: tly
## Dickey-Fuller = -3.3482, Lag order = 4, p-value = 0.0668
## alternative hypothesis: stationary
adf.test(tlipc)
##
## Augmented Dickey-Fuller Test
##
## data: tlipc
## Dickey-Fuller = -3.6563, Lag order = 4, p-value = 0.03144
## alternative hypothesis: stationary
adf.test(tlr)
##
## Augmented Dickey-Fuller Test
##
## data: tlr
## Dickey-Fuller = -2.6783, Lag order = 4, p-value = 0.2953
## alternative hypothesis: stationary
# Prueba de Phillips-Perron (PP)
pp.test(tlcp)
##
## Phillips-Perron Unit Root Test
##
## data: tlcp
## Dickey-Fuller Z(alpha) = -41.997, Truncation lag parameter = 4, p-value
## = 0.01
## alternative hypothesis: stationary
pp.test(tli)
##
## Phillips-Perron Unit Root Test
##
## data: tli
## Dickey-Fuller Z(alpha) = -29.224, Truncation lag parameter = 4, p-value
## = 0.01
## alternative hypothesis: stationary
pp.test(tly)
##
## Phillips-Perron Unit Root Test
##
## data: tly
## Dickey-Fuller Z(alpha) = -47.2, Truncation lag parameter = 4, p-value =
## 0.01
## alternative hypothesis: stationary
pp.test(tlipc)
##
## Phillips-Perron Unit Root Test
##
## data: tlipc
## Dickey-Fuller Z(alpha) = -22.236, Truncation lag parameter = 4, p-value
## = 0.03704
## alternative hypothesis: stationary
adf.test(tlr)
##
## Augmented Dickey-Fuller Test
##
## data: tlr
## Dickey-Fuller = -2.6783, Lag order = 4, p-value = 0.2953
## alternative hypothesis: stationary
# Prueba KPSS
kpss <- ur.kpss(tlcp)
summary(kpss)
##
## #######################
## # KPSS Unit Root Test #
## #######################
##
## Test is of type: mu with 4 lags.
##
## Value of test-statistic is: 0.3532
##
## Critical value for a significance level of:
## 10pct 5pct 2.5pct 1pct
## critical values 0.347 0.463 0.574 0.739
Las series son estacionarias.
# Muestra
estacionaria <- cbind(tlcp)
muestra_end <- window(estacionaria,
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
muestra_exo <- ts.intersect(tlr, tly)
# Exogenas para la estimacion
muestra_exo1 <- window(muestra_exo,
start = c(1996, 1),
end = c(2022, 4),
freq = 4)
# Exogenas para la proyeccion
muestra_exo2 <- window(muestra_exo,
start = c(1996, 1),
end = c(2023, 4),
freq = 4)
Se identifican los componentes p (rezagos) y q (medias móviles) con ayuda del correlograma.
# Correlograma
ts_cor(tlcp)
# SARIMAX
sarimax <- arima(muestra_end,
order = c(1,1,1),
seasonal = list(order = c(1,1,1),
period = 4),
xreg = muestra_exo1)
summary(sarimax)
##
## Call:
## arima(x = muestra_end, order = c(1, 1, 1), seasonal = list(order = c(1, 1, 1),
## period = 4), xreg = muestra_exo1)
##
## Coefficients:
## ar1 ma1 sar1 sma1 tlr tly
## 0.8117 -1.0000 -0.4223 -1.0000 0.0017 0.9580
## s.e. 0.0677 0.1245 0.0994 0.0743 0.0009 0.0306
##
## sigma^2 estimated as 0.003957: log likelihood = 127.43, aic = -240.86
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.004268667 0.06142853 0.04672377 14.98169 49.02669 0.4250424
## ACF1
## Training set 0.04235502
coeftest(sarimax)
##
## z test of coefficients:
##
## Estimate Std. Error z value Pr(>|z|)
## ar1 0.81174176 0.06773957 11.9833 < 2.2e-16 ***
## ma1 -0.99998139 0.12447010 -8.0339 9.442e-16 ***
## sar1 -0.42229702 0.09942216 -4.2475 2.162e-05 ***
## sma1 -0.99999341 0.07429952 -13.4589 < 2.2e-16 ***
## tlr 0.00168336 0.00091401 1.8417 0.06551 .
## tly 0.95800438 0.03062338 31.2834 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Es necesario que los residuos se comporten como ruido blanco, para ellos usamos la prueba de Dickey-Fuller aumentada, donde:
# Prueba ADF para normalidad
residuals <- resid(sarimax)
adf.test(residuals)
##
## Augmented Dickey-Fuller Test
##
## data: residuals
## Dickey-Fuller = -4.6543, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
Para determinar que un grupo cualquiera de autocorrelaciones sea distinta de cero, es decir, para comprobar si una serie de observaciones en un período de tiempo específico son aleatorias e independientes, se usa la prueba de Ljung-Box, donde:
# Prueba Ljung-Box
checkresiduals(sarimax)
##
## Ljung-Box test
##
## data: Residuals from ARIMA(1,1,1)(1,1,1)[4]
## Q* = 9.6454, df = 4, p-value = 0.04684
##
## Model df: 4. Total lags used: 8
pred4 = predict(sarimax,
newxreg = muestra_exo2)
pred4
## $pred
## Qtr1 Qtr2 Qtr3 Qtr4
## 2023 0.0232799040 0.4107165648 0.4183293578 0.4406131434
## 2024 0.2932805214 0.3926647435 0.4084240887 0.3776277393
## 2025 0.4571016066 0.3570252799 0.3046221191 0.1909575540
## 2026 0.1399470569 0.1309863759 0.1352959455 0.1358450318
## 2027 0.2511897798 0.2824727712 0.2682516181 0.1757228080
## 2028 0.0272552559 -0.1171543984 -0.2025624553 -0.1882261723
## 2029 -0.1942038769 -0.0978754356 -0.0131042305 0.0556602225
## 2030 0.0530659525 0.0145585202 -0.0544564968 -0.0007223341
## 2031 0.0628020783 0.1656602730 0.2292620684 0.2059929556
## 2032 0.1954813780 0.0948335568 0.1231158718 0.1758477877
## 2033 0.2184425734 0.2961852981 0.2455073069 0.1536317563
## 2034 0.1331931587 0.0910128877 0.0794070330 0.0849906934
## 2035 0.0127188845 0.0085220002 -0.0026693892 -0.1243539843
## 2036 -0.4810954832 -0.6116243206 -0.4454271009 -0.2146558928
## 2037 0.2018230472 0.3944573130 0.2454217102 0.1340615884
## 2038 0.1206154803 0.0621407904 0.1400809216 0.1842263600
## 2039 0.1748880978 0.1934709739 0.1202084103 0.0894892586
## 2040 0.0485860757 -0.0352931516 -0.0148860601 -0.0294534995
## 2041 0.0215892863 0.1375677097 0.0883516705 0.1312867038
## 2042 0.1052603606 0.0791846444 0.1484462906 0.0977607880
## 2043 0.1114538112 0.0540915734 0.0562100295 0.1377349335
## 2044 0.1703243323 0.1901344082 0.0892577885 0.0709243664
## 2045 0.0534197683 0.0370523909 0.0971858941 0.0235416014
## 2046 -0.0232809862 -0.0379115727 -0.0557075993 -0.1075817987
## 2047 -0.1790707927 -1.4483349383 -0.6108541706 -0.3117185328
## 2048 -0.2141165632 1.1157225469 0.2410600877 0.1028837465
## 2049 0.1837260617 0.2396576057 0.3418341316 0.2807940340
##
## $se
## Qtr1
## 2023 0.064952
1.12