Tarea Sarima

Author

Diego Lozoya Morales - José Armando Melchor Soto

Published

March 22, 2024

#LIBRERÍAS ## pkgs

TAREA

Train

h02 <- as_tsibble(fpp2::h02)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
h02 %>% autoplot(value) + ylab("Cost") + xlab("Year")+ggtitle("Serie NO estacionaria") 

En este momento no es estacionaria, asi que tendremos que aplicar diferencias, para que se pueda hacer estacionaria.

h02_train <- h02 |> 
  filter_index(. ~ "2006 jun.")

h02_train
# A tsibble: 180 x 2 [1M]
       index value
       <mth> <dbl>
 1 1991 jul. 0.430
 2 1991 ago. 0.401
 3 1991 sep. 0.432
 4 1991 oct. 0.493
 5 1991 nov. 0.502
 6 1991 dic. 0.603
 7 1992 ene. 0.660
 8 1992 feb. 0.336
 9 1992 mar. 0.351
10 1992 abr. 0.380
# ℹ 170 more rows

DIFERENCIAS ESTACIONALES

h02 |> 
  features(log(value), unitroot_ndiffs) 
# A tibble: 1 × 1
  ndiffs
   <int>
1      1
h02 |> 
  features(log(value), unitroot_nsdiffs) 
# A tibble: 1 × 1
  nsdiffs
    <int>
1       1

PRUEBAS DE RAIZ UNITARIA

h022 <- h02 %>% 
  mutate(diff_value = difference(value))


features_original <- h02 %>%
  features(value, unitroot_kpss)
features_original
# A tibble: 1 × 2
  kpss_stat kpss_pvalue
      <dbl>       <dbl>
1      2.60        0.01
differences_required <- h02 %>%
  features(value, unitroot_ndiffs)
differences_required
# A tibble: 1 × 1
  ndiffs
   <int>
1      1
h02_train |> 
  gg_tsdisplay(log(difference(difference(value, 12),1)), plot_type = "partial")

Modelos

h02_fit <- h02_train %>%
  model(
    arima412_211 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(2,1,1) + 1),
    arima312_211 = ARIMA(log(value) ~ pdq(3,1,2) + PDQ(2,1,1) + 1),
    arima412_011 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(0,1,1) + 1),
    arima412_210 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(2,1,0) + 1),
    arima412_110 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(1,1,0) + 1),
    arima412_010 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(0,1,0)),
    
  )
Warning: Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
glance(h02_fit) |> 
  arrange(AICc)
# A tibble: 6 × 8
  .model        sigma2 log_lik   AIC  AICc   BIC ar_roots   ma_roots  
  <chr>          <dbl>   <dbl> <dbl> <dbl> <dbl> <list>     <list>    
1 arima312_211 0.00336    237. -455. -453. -423. <cpl [27]> <cpl [14]>
2 arima412_211 0.00347    236. -450. -448. -415. <cpl [28]> <cpl [14]>
3 arima412_011 0.00362    232. -446. -445. -418. <cpl [4]>  <cpl [14]>
4 arima412_210 0.00377    230. -440. -439. -409. <cpl [28]> <cpl [2]> 
5 arima412_110 0.00389    227. -436. -435. -408. <cpl [16]> <cpl [2]> 
6 arima412_010 0.00408    223. -431. -431. -410. <cpl [4]>  <cpl [2]> 

Entrenamiento

h02_fc <- h02_fit |> 
  forecast(h = "2 years") 

h02_fc
# A fable: 144 x 4 [1M]
# Key:     .model [6]
   .model           index                value .mean
   <chr>            <mth>               <dist> <dbl>
 1 arima412_211 2006 jul.   t(N(-0.1, 0.0035)) 0.904
 2 arima412_211 2006 ago. t(N(0.0043, 0.0037)) 1.01 
 3 arima412_211 2006 sep.  t(N(0.071, 0.0047)) 1.08 
 4 arima412_211 2006 oct.  t(N(0.082, 0.0059)) 1.09 
 5 arima412_211 2006 nov.   t(N(0.12, 0.0066)) 1.14 
 6 arima412_211 2006 dic.   t(N(0.17, 0.0075)) 1.19 
 7 arima412_211 2007 ene.   t(N(0.18, 0.0083)) 1.21 
 8 arima412_211 2007 feb.  t(N(-0.49, 0.0091)) 0.614
 9 arima412_211 2007 mar.  t(N(-0.33, 0.0099)) 0.725
10 arima412_211 2007 abr.   t(N(-0.38, 0.011)) 0.689
# ℹ 134 more rows
h02_fc |> 
  autoplot(h02 |> filter_index("2006 Jun." ~ .), size = 1) +
  facet_wrap(~ .model, ncol = 2) +
  theme(legend.position = "none")

h02_fc |> 
  autoplot(h02 |> filter_index("2006 Jun." ~ .), size = 1)

Error de pronóstico

h02_fc |> 
  accuracy(h02) |> 
  select(.model, .type, MAPE, RMSE, MAE, MASE) |> 
  arrange(MAPE)
# A tibble: 6 × 6
  .model       .type  MAPE   RMSE    MAE  MASE
  <chr>        <chr> <dbl>  <dbl>  <dbl> <dbl>
1 arima412_110 Test   5.96 0.0689 0.0519 0.869
2 arima312_211 Test   6.08 0.0654 0.0521 0.871
3 arima412_211 Test   6.10 0.0642 0.0512 0.856
4 arima412_210 Test   6.15 0.0652 0.0510 0.853
5 arima412_011 Test   6.19 0.0690 0.0544 0.911
6 arima412_010 Test   6.51 0.0691 0.0555 0.929

Prónostico

h02_fit_fut <- h02_train |> 
  model(
    arima412_110 = ARIMA(log(value) ~ pdq(4,1,2) + PDQ(1,1,0) + 1)
  )
Warning: Model specification induces a quadratic or higher order polynomial trend. 
This is generally discouraged, consider removing the constant or reducing the number of differences.
h02_fc_fut <- h02_fit_fut |> 
  forecast(h = "2 years")

h02_fc_fut |> 
  autoplot(h02_train)