Instalar y cargar las bibliotecas necesarias

install.packages(“forecast”)

install.packages(“tseries”)

install.packages(“lmtest”)

library(forecast)
## Warning: package 'forecast' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tseries)
## Warning: package 'tseries' was built under R version 4.2.3
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

Cargar datos

library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
PIB <- read_excel("C:/Users/Lenovo/Downloads/PIB.xlsx")
View(PIB)

Conversión de los datos en una de serie temporal

PIB_ts <- ts(PIB, start = c(2001, 1), frequency = 4)
PIB_ts
##          Qtr1     Qtr2     Qtr3     Qtr4
## 2001  9699363  9802413  9809483  9930104
## 2002 10063566 10205818 10274208 10305402
## 2003 10440088 10240791 10464381 10816002
## 2004 11091411 11282549 11403289 11629461
## 2005 11771814 11936392 11951919 12149194
## 2006 12278116 12447026 12592998 12596475
## 2007 12548685 12641374 12821498 12996220
## 2008 13203590 13437956 13689235 13919627
## 2009 13721197 13663730 13579505 13593300
## 2010 13729815 13946256 14175891 14629093
## 2011 14790364 15176741 15409103 15548856
## 2012 15798590 16072842 16196959 16294042
## 2013 16458713 16802240 17131619 17153556
## 2014 17096076 17494063 17736022 17779201
## 2015 17816050 17537769 17492225 17328633
## 2016 17204627 17328097 17310908 17470434
## 2017 17497935 17685968 17819405 17952383
## 2018 17762564 17943194 18080826 18083933
## 2019 17970651 18009165 18075353 17824048
## 2020 17647247 15504941 16454336 16675022
## 2021 16929406 17298695 17367296 17493339
## 2022 17503213 17536480 17834006 18251544

Gráfico de la serie temporal

plot(PIB_ts, main="PIB Trimestral de Ecuador", ylab="PIB", xlab="Año")

Prueba de estacionariedad (Dickey-Fuller)

adf.test(PIB_ts)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  PIB_ts
## Dickey-Fuller = -1.2172, Lag order = 4, p-value = 0.898
## alternative hypothesis: stationary

Primera diferencia

PIB_diff <- diff(PIB_ts)
PIB_diff
##          Qtr1     Qtr2     Qtr3     Qtr4
## 2001            103050     7070   120621
## 2002   133462   142252    68390    31194
## 2003   134686  -199297   223590   351621
## 2004   275409   191138   120740   226172
## 2005   142353   164578    15527   197275
## 2006   128922   168910   145972     3477
## 2007   -47790    92689   180124   174722
## 2008   207370   234366   251279   230392
## 2009  -198430   -57467   -84225    13795
## 2010   136515   216441   229635   453202
## 2011   161271   386377   232362   139753
## 2012   249734   274252   124117    97083
## 2013   164671   343527   329379    21937
## 2014   -57480   397987   241959    43179
## 2015    36849  -278281   -45544  -163592
## 2016  -124006   123470   -17189   159526
## 2017    27501   188033   133437   132978
## 2018  -189819   180630   137632     3107
## 2019  -113282    38514    66188  -251305
## 2020  -176801 -2142306   949395   220686
## 2021   254384   369289    68601   126043
## 2022     9874    33267   297526   417538

Prueba de estacionariedad

adf.test(PIB_diff)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  PIB_diff
## Dickey-Fuller = -3.9248, Lag order = 4, p-value = 0.01682
## alternative hypothesis: stationary

Gráfico la serie diferenciada

plot(PIB_diff, main="PIB Trimestral Diferenciado de Ecuador", ylab="Diferencia del PIB", xlab="Año")

Identificar los parámetros p, d, q mediante ACF y PACF

acf(PIB_diff, main="ACF del PIB Diferenciado")

pacf(PIB_diff, main="PACF del PIB Diferenciado")

Ajustar el modelo ARIMA

modelo <- arima(PIB_ts,order=c(1,1,1), method = "ML")
summary(modelo)
## 
## Call:
## arima(x = PIB_ts, order = c(1, 1, 1), method = "ML")
## 
## Coefficients:
##          ar1      ma1
##       0.9964  -0.9732
## s.e.  0.0151   0.0562
## 
## sigma^2 estimated as 9.203e+10:  log likelihood = -1222.06,  aic = 2450.12
## 
## Training set error measures:
##                    ME     RMSE      MAE       MPE     MAPE      MASE
## Training set 17305.88 301630.7 163525.5 0.1666448 1.100643 0.8445256
##                     ACF1
## Training set -0.05218618

Mostrar la significancia del pvalue del ARIMA

coeftest(modelo)
## 
## z test of coefficients:
## 
##      Estimate Std. Error z value  Pr(>|z|)    
## ar1  0.996407   0.015073  66.108 < 2.2e-16 ***
## ma1 -0.973171   0.056193 -17.318 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Diagnóstico de residuos

checkresiduals(modelo)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(1,1,1)
## Q* = 2.1092, df = 6, p-value = 0.9094
## 
## Model df: 2.   Total lags used: 8

Pronóstico de los próximos 4 trimestres

forecast_PI <- forecast(modelo, h=4)
plot(forecast_PI, main="Pronóstico del PIB Trimestral de Ecuador", ylab="PIB", xlab="Año")

forecast_PI
##         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 2023 Q1       18318172 17929351 18706994 17723521 18912824
## 2023 Q2       18384562 17828187 18940936 17533660 19235463
## 2023 Q3       18450712 17761331 19140093 17396395 19505029
## 2023 Q4       18516625 17711393 19321857 17285129 19748120