ARIMAS

library(tidyverse)
library(fpp3)
#|echo: false 
aus_production |> autoplot(Electricity)

aus_production
# A tsibble: 218 x 7 [1Q]
   Quarter  Beer Tobacco Bricks Cement Electricity   Gas
     <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
 1 1956 Q1   284    5225    189    465        3923     5
 2 1956 Q2   213    5178    204    532        4436     6
 3 1956 Q3   227    5297    208    561        4806     7
 4 1956 Q4   308    5681    197    570        4418     6
 5 1957 Q1   262    5577    187    529        4339     5
 6 1957 Q2   228    5651    214    604        4811     7
 7 1957 Q3   236    5317    227    603        5259     7
 8 1957 Q4   320    6152    222    582        4735     6
 9 1958 Q1   272    5758    199    554        4608     5
10 1958 Q2   233    5641    229    620        5196     7
# ℹ 208 more rows
autoplot(aus_production, Gas) +
  labs(title = "Serie Original de Producción de Gas", y = "Producción")

library(urca)  # Para pruebas de raíz unitaria

# Convertimos la serie en un objeto ts
gas_ts <- ts(aus_production$Gas, start = c(1956,1), frequency = 4)

# Aplicamos la prueba ADF
adf_test <- ur.df(gas_ts, type = "drift", selectlags = "AIC")
summary(adf_test)

############################################### 
# Augmented Dickey-Fuller Test Unit Root Test # 
############################################### 

Test regression drift 


Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
    Min      1Q  Median      3Q     Max 
-44.678  -8.479  -2.883  12.636  54.097 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)  
(Intercept)  4.084027   2.234202   1.828   0.0690 .
z.lag.1     -0.030465   0.018068  -1.686   0.0932 .
z.diff.lag  -0.003119   0.069049  -0.045   0.9640  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 19.82 on 213 degrees of freedom
Multiple R-squared:  0.01352,   Adjusted R-squared:  0.004255 
F-statistic: 1.459 on 2 and 213 DF,  p-value: 0.2347


Value of test-statistic is: -1.6861 1.7429 

Critical values for test statistics: 
      1pct  5pct 10pct
tau2 -3.46 -2.88 -2.57
phi1  6.52  4.63  3.81
# Primera diferenciación
diff_gas <- diff(gas_ts, differences = 1)

# Convertir diff_gas a un tsibble
diff_gas_tsibble <- as_tsibble(diff_gas)

# Graficar utilizando autoplot
autoplot(diff_gas_tsibble) +
  labs(title = "Serie Diferenciada (1ra diferencia)", y = "Diferenciada")
Plot variable not specified, automatically selected `.vars = value`

library(forecast)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
ggAcf(diff_gas) + labs(title = "ACF de la Serie Diferenciada")

ggPacf(diff_gas) + labs(title = "PACF de la Serie Diferenciada")

fit <- arima(gas_ts, order = c(2, 1, 2), method="ML")
Warning in arima(gas_ts, order = c(2, 1, 2), method = "ML"): possible
convergence problem: optim gave code = 1
summary(fit)

Call:
arima(x = gas_ts, order = c(2, 1, 2), method = "ML")

Coefficients:
          ar1  ar2      ma1      ma2
      -0.0164   -1  -0.1521  -0.0295
s.e.   0.0242    0   0.0463   0.0749

sigma^2 estimated as 42.46:  log likelihood = -715.64,  aic = 1441.27

Training set error measures:
                   ME     RMSE      MAE      MPE     MAPE     MASE       ACF1
Training set 2.459104 6.500893 4.556671 3.654972 5.855452 0.313208 -0.2681604
best_model <- auto.arima(gas_ts, seasonal = FALSE)
summary(best_model)
Series: gas_ts 
ARIMA(0,1,5) with drift 

Coefficients:
          ma1      ma2     ma3     ma4      ma5   drift
      -0.4718  -0.6738  0.6918  0.4820  -0.7439  1.0395
s.e.   0.0402   0.0498  0.0460  0.0517   0.0456  0.1719

sigma^2 = 77.43:  log likelihood = -779.6
AIC=1573.2   AICc=1573.74   BIC=1596.86

Training set error measures:
                      ME     RMSE     MAE       MPE     MAPE     MASE
Training set -0.05737316 8.657002 6.63741 -10.15849 15.58445 1.190617
                   ACF1
Training set 0.03914338
forecast(fit, h = 8) %>%
  autoplot() +
  labs(title = "Pronóstico con ARIMA")