library(readr)
Volumen_Petroleo <- read_csv("C:/Users/mario/Downloads/Volumen_Petroleo.csv")
## New names:
## Rows: 106 Columns: 2
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (1): ...1 dbl (1): Barriles_Petroleo
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
View(Volumen_Petroleo)
#Convertir la base de datos a una serie
serie_final = ts(Volumen_Petroleo$Barriles_Petroleo,freq=12,start=c(2015,1))
plot(serie_final, main = "EvoluciĂ³n Volumen Producido de PetrĂ³leo")

#Podemos observar una tendencia negativa en la grĂ¡fica y un decrecimiento a lo largo de los Ăºltimos 9 años, ademĂ¡s que para los Ăºltimos dos meses no existĂ­an datos.

#Luego descomponemos la serie
plot(decompose(serie_final))

#Instalamos paquetes "urca" y "tseries"
library(urca)
## Warning: package 'urca' was built under R version 4.3.3
library(tseries)
## Warning: package 'tseries' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
#Se realiza el test de Dickey-Fuller, donde se tiene como hipĂ³tesis nula la No Estacionariedad
df_test = adf.test (serie_final)
print(df_test)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  serie_final
## Dickey-Fuller = -1.0012, Lag order = 4, p-value = 0.9343
## alternative hypothesis: stationary

#El test nos muestra que No se rechaza la hipĂ³tesis nula, lo que supone transformar la serie.

#Aplicamos logaritmos
ln_serie_final = log(serie_final)
plot(ln_serie_final, main = "logaritmo natural de serie")

#El resultado no nos muestra una diferencia de cambio en la serie de tiempo, por lo tanto se procede a aplicar primeras diferencias.

diferencia=diff(serie_final)
print(diferencia)
##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 2015          -4506765  6605955 -5851786  2994724 -1635783  3025877  -527845
## 2016  -499652 -5805904  4500100 -3421999  2111251 -2070001  1539107  -425833
## 2017  -688151 -6146522  6079522 -2190717  1292382 -2417928  1284545 -1746579
## 2018   887133 -6485290  4248581 -1053883  1258081 -2517046  1699205 -1069051
## 2019 -2745578 -2525643  4467204 -2050350  1257669 -1275921  1341282   237647
## 2020   395059 -3536147  3523322 -2968086  -736803 -2514357  1155764  1225669
## 2021   125308 -4119348  5215359 -1869591  1345560 -1119415  1723578 -1267634
## 2022   301006 -5052923  4896202 -1980447  1538084 -1119566  1621444  -353117
## 2023   107273 -4484299  4725473 -1117822  1537570 -1469334   642017   659155
##           Sep      Oct      Nov      Dec
## 2015 -1770340  2505295 -2329323  2218612
## 2016 -3050473  1795585 -3037721   933399
## 2017 -7865832  7239294 -2998145  1888287
## 2018 -1598935  -147337 -3222811  1988438
## 2019  -968326    82640  -409364  1751921
## 2020  -993658  1267477 -1401464  1772938
## 2021    50774  1095433 -1857811  1654270
## 2022 -1759694  1844032 -1463046 -2364317
## 2023 -1719802   906026
plot(diferencia, main="Primeras Diferencias")

#Se reliza la prueba de Dickey-Fuller a las primeras diferencias
df_test_2 = adf.test (diferencia)
## Warning in adf.test(diferencia): p-value smaller than printed p-value
print(df_test_2)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diferencia
## Dickey-Fuller = -4.9039, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary

#El resultado nos muestra que se puede rechazar la HipĂ³tesis nula, por lo que, ya no existe estacionariedad en la serie de diferencias y ya se puede modelar.

acf(diferencia)

pacf(diferencia)

#El modelo es un ARIMA el cual podrĂ­a tener una estructura del tipo (1,2,1).

library(TSA)
## Warning: package 'TSA' was built under R version 4.3.3
## 
## Attaching package: 'TSA'
## The following object is masked from 'package:readr':
## 
##     spec
## The following objects are masked from 'package:stats':
## 
##     acf, arima
## The following object is masked from 'package:utils':
## 
##     tar
library(seasonal)
## Warning: package 'seasonal' was built under R version 4.3.3
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 methods overwritten by 'forecast':
##   method       from
##   fitted.Arima TSA 
##   plot.Arima   TSA
seleccion = auto.arima(serie_final)
summary(seleccion)
## Series: serie_final 
## ARIMA(1,1,0)(0,1,1)[12] 
## 
## Coefficients:
##           ar1     sma1
##       -0.3119  -0.7784
## s.e.   0.1000   0.1424
## 
## sigma^2 = 1.937e+12:  log likelihood = -1452.09
## AIC=2910.17   AICc=2910.44   BIC=2917.77
## 
## Training set error measures:
##                    ME    RMSE      MAE        MPE     MAPE      MASE
## Training set 10085.53 1289605 894780.2 0.06097708 1.763969 0.2463422
##                     ACF1
## Training set -0.01581284

¡El modelo es un ARIMA estacional! Ahora debemos observar si los residuos del modelo se comportan como Ruido Blanco

#AnĂ¡lisis de correlaciĂ³n entre los residuos
Box.test(seleccion$residuals, lag = 12, type = "Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  seleccion$residuals
## X-squared = 8.571, df = 12, p-value = 0.7391
# Los residuos no estĂ¡n correlacionados.
#AnĂ¡lisis de normalidad de los residuos
shapiro.test(seleccion$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  seleccion$residuals
## W = 0.94803, p-value = 0.0004045
# Los residuos se distribuyen como una normal.
hist(seleccion$residuals)

forecast_2=forecast(seleccion,level= c(95), h=2)
plot(forecast_2)

print(forecast_2)
##          Point Forecast    Lo 95    Hi 95
## Nov 2023       42407074 39669193 45144955
## Dec 2023       43124312 39800908 46447716
forecast_8=forecast(seleccion,level= c(95), h=8)
plot(forecast_8)

print(forecast_8)
##          Point Forecast    Lo 95    Hi 95
## Nov 2023       42407074 39669193 45144955
## Dec 2023       43124312 39800908 46447716
## Jan 2024       43004830 39046625 46963036
## Feb 2024       38413963 33949845 42878081
## Mar 2024       43221856 38292677 48151035
## Apr 2024       41100827 35749978 46451676
## May 2024       42404899 36662351 48147447
## Jun 2024       40764648 34655733 46873564