IPC_peru <- read_excel("C:/CURSOS DEL X CICLO/ELEMENTOS F. Y SERIES TEMPORALES/IPC(2000-2024).xlsx")
## New names:
## • `` -> `...1`
ipc_peru.ts <- ts(IPC_peru$IPC,start = c(2000,1),frequency = 12)
plot(ipc_peru.ts,
     main = 'Indice de Precios al Consumidor (var% mensual)',
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="orange")
grid()

adf.test(ipc_peru.ts)
## Warning in adf.test(ipc_peru.ts): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ipc_peru.ts
## Dickey-Fuller = -4.9279, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
acf(ipc_peru.ts,
     main = 'Función de autocorrelación muestral',
     xlab = 'Rezago',
     ylab = 'ACF',
     lwd = 3)
grid()

#----------------------------------------------------------------------
pacf(ipc_peru.ts,
     main = 'Función de autocorrelación parcial muestral',
     xlab = 'Rezago',
     ylab = 'PACF',
     lwd = 3)
grid()

#-----------------------------------------
#--------------------------------------------------------------
#Seleccion del mejor modelo
modeloautoarima <- auto.arima(ipc_peru.ts)
summary(modeloautoarima)
## Series: ipc_peru.ts 
## ARIMA(0,1,2)(0,0,2)[12] 
## 
## Coefficients:
##           ma1      ma2    sma1    sma2
##       -0.7159  -0.2681  0.1820  0.1095
## s.e.   0.0517   0.0517  0.0607  0.0527
## 
## sigma^2 = 0.09745:  log likelihood = -74.61
## AIC=159.22   AICc=159.43   BIC=177.64
## 
## Training set error measures:
##                      ME      RMSE       MAE      MPE     MAPE      MASE
## Training set 0.01334877 0.3095071 0.2362279 13.47438 205.9634 0.7384536
##                    ACF1
## Training set 0.03338508
plot(modeloautoarima)

forecast(modeloautoarima)
##          Point Forecast       Lo 80     Hi 80      Lo 95     Hi 95
## Aug 2024      0.2926929 -0.10736345 0.6927493 -0.3191405 0.9045264
## Sep 2024      0.2561436 -0.15974040 0.6720276 -0.3798961 0.8921833
## Oct 2024      0.1758687 -0.24006449 0.5918018 -0.4602462 0.8119836
## Nov 2024      0.2242330 -0.19174928 0.6402154 -0.4119570 0.8604231
## Dec 2024      0.3357950 -0.08023645 0.7518265 -0.3004702 0.9720603
## Jan 2025      0.2452060 -0.17087460 0.6612866 -0.3911344 0.8815464
## Feb 2025      0.3412897 -0.07484002 0.7574195 -0.2951258 0.9777053
## Mar 2025      0.4567989  0.04061999 0.8729778 -0.1796918 1.0932896
## Apr 2025      0.2282596 -0.18796838 0.6444877 -0.4083062 0.8648255
## May 2025      0.2220358 -0.19424133 0.6383130 -0.4146051 0.8586768
## Jun 2025      0.1950485 -0.22127775 0.6113748 -0.4416676 0.8317646
## Jul 2025      0.2676396 -0.14873578 0.6840150 -0.3691516 0.9044308
## Aug 2025      0.2839656 -0.13987456 0.7078058 -0.3642420 0.9321732
## Sep 2025      0.2569460 -0.16775823 0.6816502 -0.3925831 0.9064751
## Oct 2025      0.2214234 -0.20334810 0.6461949 -0.4282085 0.8710553
## Nov 2025      0.2392205 -0.18561825 0.6640592 -0.4105143 0.8889553
## Dec 2025      0.2907037 -0.13420231 0.7156096 -0.3591339 0.9405413
## Jan 2026      0.2685031 -0.15647013 0.6934763 -0.3814374 0.9184435
## Feb 2026      0.3205687 -0.10447175 0.7456091 -0.3294746 0.9706119
## Mar 2026      0.3446084 -0.08049921 0.7697160 -0.3055376 0.9947544
## Apr 2026      0.2432359 -0.18193891 0.6684107 -0.4070129 0.8934847
## May 2026      0.2499602 -0.17528181 0.6752022 -0.4003913 0.9003117
## Jun 2026      0.2768039 -0.14850530 0.7021130 -0.3736504 0.9272581
## Jul 2026      0.2824634 -0.14291294 0.7078397 -0.3680936 0.9330203
plot(forecast(modeloautoarima),
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="orange")
grid()

#ARIMA(0,1,2)(0,0,2)[12] aplicando el auto arima el mejor modelo para nuestro caso seria
#el modelo ARIMA sin ninguna autocorrelacion , con un integrado,y dos medias moviles , con estacioanierdad doble tambien en la medias moviles Q=2 , con frecuencia de 12 osea mensual.
#Modelo con 3 factores de autoregresivos con 3 rezagos sin ninguna integracion y tampoco media movil.
modelo2 <- arima(ipc_peru.ts,order = c(3,0,0))
summary(modelo2)
## 
## Call:
## arima(x = ipc_peru.ts, order = c(3, 0, 0))
## 
## Coefficients:
##          ar1     ar2     ar3  intercept
##       0.2869  0.0083  0.0338     0.2498
## s.e.  0.0581  0.0605  0.0582     0.0274
## 
## sigma^2 estimated as 0.1:  log likelihood = -79,  aic = 168
## 
## Training set error measures:
##                        ME      RMSE       MAE      MPE     MAPE      MASE
## Training set 0.0001695038 0.3162247 0.2403059 5.191382 223.5414 0.8013018
##                    ACF1
## Training set -0.0038168
plot(modelo2)

forecast(modelo2)
##          Point Forecast      Lo 80     Hi 80      Lo 95     Hi 95
## Aug 2024      0.2333568 -0.1719014 0.6386151 -0.3864322 0.8531458
## Sep 2024      0.2406658 -0.1809466 0.6622782 -0.4041347 0.8854663
## Oct 2024      0.2466149 -0.1765956 0.6698255 -0.4006298 0.8938596
## Nov 2024      0.2482844 -0.1756775 0.6722462 -0.4001094 0.8966781
## Dec 2024      0.2490603 -0.1750570 0.6731776 -0.3995711 0.8976918
## Jan 2025      0.2494983 -0.1746456 0.6736421 -0.3991738 0.8981703
## Feb 2025      0.2496869 -0.1744633 0.6738371 -0.3989948 0.8983686
## Mar 2025      0.2497709 -0.1743807 0.6739225 -0.3989129 0.8984548
## Apr 2025      0.2498114 -0.1743404 0.6739633 -0.3988729 0.8984957
## May 2025      0.2498301 -0.1743218 0.6739821 -0.3988543 0.8985145
## Jun 2025      0.2498387 -0.1743132 0.6739906 -0.3988457 0.8985231
## Jul 2025      0.2498427 -0.1743093 0.6739946 -0.3988417 0.8985271
## Aug 2025      0.2498445 -0.1743074 0.6739965 -0.3988399 0.8985289
## Sep 2025      0.2498454 -0.1743066 0.6739973 -0.3988391 0.8985298
## Oct 2025      0.2498458 -0.1743062 0.6739977 -0.3988387 0.8985302
## Nov 2025      0.2498459 -0.1743060 0.6739979 -0.3988385 0.8985304
## Dec 2025      0.2498460 -0.1743059 0.6739980 -0.3988384 0.8985305
## Jan 2026      0.2498461 -0.1743059 0.6739980 -0.3988383 0.8985305
## Feb 2026      0.2498461 -0.1743059 0.6739980 -0.3988383 0.8985305
## Mar 2026      0.2498461 -0.1743058 0.6739980 -0.3988383 0.8985305
## Apr 2026      0.2498461 -0.1743058 0.6739980 -0.3988383 0.8985305
## May 2026      0.2498461 -0.1743058 0.6739981 -0.3988383 0.8985305
## Jun 2026      0.2498461 -0.1743058 0.6739981 -0.3988383 0.8985305
## Jul 2026      0.2498461 -0.1743058 0.6739981 -0.3988383 0.8985305
plot(forecast(modelo2))

#Aplicando diferencias a nuestro dato principal 
df_ipcperu <-diff(ipc_peru.ts)
plot(df_ipcperu)

adf.test(df_ipcperu)
## Warning in adf.test(df_ipcperu): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  df_ipcperu
## Dickey-Fuller = -9.6333, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
#Modelo con tres medias moviles ,sin ningun rezago.
modelo3 <- arima(df_ipcperu,order = c(0,0,3))
summary(modelo3)
## 
## Call:
## arima(x = df_ipcperu, order = c(0, 0, 3))
## 
## Coefficients:
##           ma1      ma2      ma3  intercept
##       -0.7196  -0.2007  -0.0797      6e-04
## s.e.   0.0595   0.0685   0.0572      3e-04
## 
## sigma^2 estimated as 0.09907:  log likelihood = -79.89,  aic = 169.78
## 
## Training set error measures:
##                        ME      RMSE       MAE      MPE     MAPE      MASE
## Training set -0.004581987 0.3147526 0.2384919 60.03048 194.9602 0.4789443
##                      ACF1
## Training set -0.002467126
plot(modelo3)

forecast(modelo3)
##          Point Forecast      Lo 80     Hi 80      Lo 95     Hi 95
## Aug 2024   0.0823169905 -0.3217363 0.4863702 -0.5356291 0.7002631
## Sep 2024   0.0183850876 -0.4786031 0.5153733 -0.7416928 0.7784630
## Oct 2024   0.0050471738 -0.4984519 0.5085462 -0.7649882 0.7750825
## Nov 2024   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Dec 2024   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jan 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Feb 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Mar 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Apr 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## May 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jun 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jul 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Aug 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Sep 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Oct 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Nov 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Dec 2025   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jan 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Feb 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Mar 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Apr 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## May 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jun 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
## Jul 2026   0.0006140427 -0.5039079 0.5051360 -0.7709857 0.7722138
plot(forecast(modelo3))

modelo4 <- arima(ipc_peru.ts,order = c(0,0,3))
summary(modelo4)
## 
## Call:
## arima(x = ipc_peru.ts, order = c(0, 0, 3))
## 
## Coefficients:
##          ma1     ma2      ma3  intercept
##       0.2907  0.0894  -0.0018     0.2499
## s.e.  0.0582  0.0568   0.0523     0.0254
## 
## sigma^2 estimated as 0.1002:  log likelihood = -79.3,  aic = 168.6
## 
## Training set error measures:
##                        ME      RMSE      MAE      MPE     MAPE      MASE
## Training set 0.0002388954 0.3165464 0.239593 2.134719 225.2367 0.7989246
##                      ACF1
## Training set 0.0004700467
plot(modelo4)

forecast(modelo4)
##          Point Forecast      Lo 80     Hi 80      Lo 95     Hi 95
## Aug 2024      0.2524312 -0.1532394 0.6581018 -0.3679884 0.8728508
## Sep 2024      0.2507508 -0.1717176 0.6732193 -0.3953589 0.8968606
## Oct 2024      0.2498501 -0.1741736 0.6738737 -0.3986382 0.8983383
## Nov 2024      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Dec 2024      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jan 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Feb 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Mar 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Apr 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## May 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jun 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jul 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Aug 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Sep 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Oct 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Nov 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Dec 2025      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jan 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Feb 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Mar 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Apr 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## May 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jun 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
## Jul 2026      0.2498673 -0.1741570 0.6738916 -0.3986218 0.8983565
plot(forecast(modelo4))

par(mfrow = c(2, 2))
plot(forecast(modeloautoarima),
     main="Modelo ARIMA(0,1,2)(0,0,2)[12]",
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="red")
plot(forecast(modelo2),
     main="Modelo AR(3,0,0)",
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="red")
plot(forecast(modelo3),
     main="Modelo ARMA(0,1,3)",
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="red")
plot(forecast(modelo4),
     main="Modelo MA(3,0,0)",
     xlab = 'Año',
     ylab = 'Variacion [%]',
     lwd = 2,col="red")

par(mfrow = c(2, 2))
plot(modeloautoarima,
     main="Raiz inversa de los polinomios ARIMA(0,1,2)(0,0,2)",
     xlab = 'Reales',
     ylab = 'imaginarios',
     lwd = 3,col="blue")
plot(modelo2,
     main="Raiz inversa de los polinomios AR(3,0,0)",
     xlab = 'Reales',
     ylab = 'imaginarios',
     lwd = 3,col="red")
plot(modelo3,
     main="Raiz inversa de los polinomios ARMA(0,1,3)",
     xlab = 'Reales',
     ylab = 'imaginarios',
     lwd = 3,col="red")
plot(modelo4,
     main="Raiz inversa de los polinomios MA(3,0,0)",
     xlab = 'Reales',
     ylab = 'imaginarios',
     lwd = 3,col="red")

par(mfrow = c(2, 2))
summary(modeloautoarima)#MODELO SARIMA(0,1,2)(0,0,2)[12]
## Series: ipc_peru.ts 
## ARIMA(0,1,2)(0,0,2)[12] 
## 
## Coefficients:
##           ma1      ma2    sma1    sma2
##       -0.7159  -0.2681  0.1820  0.1095
## s.e.   0.0517   0.0517  0.0607  0.0527
## 
## sigma^2 = 0.09745:  log likelihood = -74.61
## AIC=159.22   AICc=159.43   BIC=177.64
## 
## Training set error measures:
##                      ME      RMSE       MAE      MPE     MAPE      MASE
## Training set 0.01334877 0.3095071 0.2362279 13.47438 205.9634 0.7384536
##                    ACF1
## Training set 0.03338508
summary(modelo2)#       MODELO AR(3,0,0)
## 
## Call:
## arima(x = ipc_peru.ts, order = c(3, 0, 0))
## 
## Coefficients:
##          ar1     ar2     ar3  intercept
##       0.2869  0.0083  0.0338     0.2498
## s.e.  0.0581  0.0605  0.0582     0.0274
## 
## sigma^2 estimated as 0.1:  log likelihood = -79,  aic = 168
## 
## Training set error measures:
##                        ME      RMSE       MAE      MPE     MAPE      MASE
## Training set 0.0001695038 0.3162247 0.2403059 5.191382 223.5414 0.8013018
##                    ACF1
## Training set -0.0038168
summary(modelo3)#       MODELO ARMA(0,1,3)
## 
## Call:
## arima(x = df_ipcperu, order = c(0, 0, 3))
## 
## Coefficients:
##           ma1      ma2      ma3  intercept
##       -0.7196  -0.2007  -0.0797      6e-04
## s.e.   0.0595   0.0685   0.0572      3e-04
## 
## sigma^2 estimated as 0.09907:  log likelihood = -79.89,  aic = 169.78
## 
## Training set error measures:
##                        ME      RMSE       MAE      MPE     MAPE      MASE
## Training set -0.004581987 0.3147526 0.2384919 60.03048 194.9602 0.4789443
##                      ACF1
## Training set -0.002467126
summary(modelo4)#       MODELO MA(0,03)
## 
## Call:
## arima(x = ipc_peru.ts, order = c(0, 0, 3))
## 
## Coefficients:
##          ma1     ma2      ma3  intercept
##       0.2907  0.0894  -0.0018     0.2499
## s.e.  0.0582  0.0568   0.0523     0.0254
## 
## sigma^2 estimated as 0.1002:  log likelihood = -79.3,  aic = 168.6
## 
## Training set error measures:
##                        ME      RMSE      MAE      MPE     MAPE      MASE
## Training set 0.0002388954 0.3165464 0.239593 2.134719 225.2367 0.7989246
##                      ACF1
## Training set 0.0004700467
coef(modeloautoarima) # coeficientes del modelo , en este caso se omite el intercepto
##        ma1        ma2       sma1       sma2 
## -0.7159473 -0.2680673  0.1819635  0.1094527
coef(modelo2)
##         ar1         ar2         ar3   intercept 
## 0.286946229 0.008332213 0.033844769 0.249846107
coef(modelo3)
##           ma1           ma2           ma3     intercept 
## -0.7195606327 -0.2006972024 -0.0797419067  0.0006140427
coef(modelo4)
##          ma1          ma2          ma3    intercept 
##  0.290740623  0.089439488 -0.001773618  0.249867320