Ejercicio SARIMA

Author

AGHG Y JAFL

library(tidyverse)
library(fpp3)
library(patchwork)

Ejercicio SARIMA

h02 <- PBS |> 
  filter(ATC2 == "H02") |> 
  summarise(Cost = sum(Cost)/1e6)

h02
# A tsibble: 204 x 2 [1M]
       Month  Cost
       <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
# ℹ 194 more rows
h02 |> 
  mutate(log(Cost)) |> 
  gather() |> 
  ggplot(aes(x = Month, y = value)) +
  geom_line() +
  facet_grid(key ~ ., scales = "free_y") +
  xlab("Year") + ylab("") +
  ggtitle("Cortecosteroid drug scripts (H02)")

Training

h02_train <- h02 |> 
  filter_index(.~'2006 December')
h02_train %>% gg_tsdisplay(log(Cost) %>% difference(12) %>% difference, plot_type='partial', lag_max = 48)
Warning: Removed 13 rows containing missing values (`geom_line()`).
Warning: Removed 13 rows containing missing values (`geom_point()`).

h02_fit <- h02_train |> 
  model(
    arima_211_111 = ARIMA(log(Cost) ~ pdq(2,1,1) + PDQ(1,1,1)),
    arima_311_210 = ARIMA(log(Cost) ~ pdq(3,1,1) + PDQ(2,1,0)),
    arima_210_311 = ARIMA(log(Cost) ~ pdq(2,1,2) + PDQ(3,1,1)+1),
    arima_110_312 = ARIMA(log(Cost) ~ pdq(1,1,0) + PDQ(3,1,2)),
    arima_112_112 = ARIMA(log(Cost) ~ pdq(1,1,2) + PDQ(1,1,2)),
    arima_213_110 = ARIMA(log(Cost) ~ pdq(2,1,1) + PDQ(1,1,0)),
    arima_212_212 = ARIMA(log(Cost) ~ pdq(2,1,1) + PDQ(2,1,2)+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.
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_fit
# A mable: 1 x 7
              arima_211_111             arima_311_210
                    <model>                   <model>
1 <ARIMA(2,1,1)(1,1,1)[12]> <ARIMA(3,1,1)(2,1,0)[12]>
# ℹ 5 more variables: arima_210_311 <model>, arima_110_312 <model>,
#   arima_112_112 <model>, arima_213_110 <model>, arima_212_212 <model>
 h02_fit |> 
  glance() |> 
  arrange(AICc)
# A tibble: 7 × 8
  .model         sigma2 log_lik   AIC  AICc   BIC ar_roots   ma_roots  
  <chr>           <dbl>   <dbl> <dbl> <dbl> <dbl> <list>     <list>    
1 arima_212_212 0.00339    245. -473. -472. -444. <cpl [26]> <cpl [25]>
2 arima_211_111 0.00361    240. -469. -468. -450. <cpl [14]> <cpl [13]>
3 arima_210_311 0.00350    244. -468. -467. -437. <cpl [38]> <cpl [14]>
4 arima_112_112 0.00369    239. -464. -463. -442. <cpl [13]> <cpl [26]>
5 arima_311_210 0.00375    238. -463. -462. -441. <cpl [27]> <cpl [1]> 
6 arima_110_312 0.00357    237. -461. -460. -439. <cpl [37]> <cpl [24]>
7 arima_213_110 0.00418    229. -449. -449. -433. <cpl [14]> <cpl [1]> 
h02_accu_train <- accuracy(h02_fit) |> 
  arrange(MAPE)
h02_accu_train
# A tibble: 7 × 10
  .model        .type         ME   RMSE    MAE    MPE  MAPE  MASE RMSSE     ACF1
  <chr>         <chr>      <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>    <dbl>
1 arima_212_212 Traini… -5.14e-4 0.0447 0.0330 -0.137  4.20 0.551 0.620 -0.0388 
2 arima_210_311 Traini… -1.03e-3 0.0454 0.0335 -0.167  4.28 0.560 0.630 -0.0284 
3 arima_110_312 Traini… -2.77e-3 0.0473 0.0341 -0.395  4.32 0.570 0.656 -0.185  
4 arima_211_111 Traini… -4.02e-3 0.0465 0.0341 -0.528  4.36 0.569 0.645 -0.0448 
5 arima_311_210 Traini… -4.94e-3 0.0471 0.0340 -0.757  4.37 0.568 0.654 -0.00568
6 arima_112_112 Traini… -4.32e-3 0.0476 0.0348 -0.575  4.44 0.582 0.661 -0.0494 
7 arima_213_110 Traini… -2.28e-3 0.0499 0.0361 -0.413  4.63 0.602 0.693 -0.0758 
h02_fit |> 
  select( arima_212_212) |> 
  gg_tsresiduals()

h02_fit |> 
  select(arima_211_111) |> 
  gg_tsresiduals()

h02_fit|> 
  select(arima_210_311) |> 
  gg_tsresiduals()

h02_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 10) |> 
  mutate(residuos_autocorrelacionados = if_else(lb_pvalue >=0.05, "No autocorrelación", "Autocorrelacionados"))
# A tibble: 7 × 4
  .model        lb_stat lb_pvalue residuos_autocorrelacionados
  <chr>           <dbl>     <dbl> <chr>                       
1 arima_110_312   24.2    0.00716 Autocorrelacionados         
2 arima_112_112   20.4    0.0260  Autocorrelacionados         
3 arima_210_311    9.80   0.459   No autocorrelación          
4 arima_211_111   11.8    0.296   No autocorrelación          
5 arima_212_212    8.19   0.611   No autocorrelación          
6 arima_213_110   13.3    0.207   No autocorrelación          
7 arima_311_210   12.2    0.270   No autocorrelación          

Test

h02_fc <- h02_fit |> 
  forecast(h='24 months')

h02_fc
# A fable: 168 x 4 [1M]
# Key:     .model [7]
   .model            Month                Cost .mean
   <chr>             <mth>              <dist> <dbl>
 1 arima_211_111 2007 ene.   t(N(0.2, 0.0036)) 1.22 
 2 arima_211_111 2007 feb. t(N(-0.54, 0.0038)) 0.585
 3 arima_211_111 2007 mar. t(N(-0.37, 0.0048)) 0.696
 4 arima_211_111 2007 abr.  t(N(-0.4, 0.0062)) 0.669
 5 arima_211_111 2007 may. t(N(-0.27, 0.0067)) 0.768
 6 arima_211_111 2007 jun. t(N(-0.22, 0.0079)) 0.808
 7 arima_211_111 2007 jul. t(N(-0.11, 0.0088)) 0.901
 8 arima_211_111 2007 ago. t(N(0.001, 0.0096)) 1.01 
 9 arima_211_111 2007 sep.  t(N(0.021, 0.011)) 1.03 
10 arima_211_111 2007 oct.   t(N(0.11, 0.012)) 1.13 
# ℹ 158 more rows
h02_fc |> 
  autoplot(h02 |> filter_index('2000 January' ~.), level= NULL, size=1)

h02_fc |> 
  autoplot(h02 |> filter_index('2000 January' ~.), size=1)+
  facet_wrap(~ .model, ncol= 3)+
  theme(legend.position = 'none')

h02_fc |> 
  accuracy(h02) |> 
  arrange(MAPE)
Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing. 
6 observations are missing between 2008 jul. and 2008 dic.
# A tibble: 7 × 10
  .model        .type      ME   RMSE    MAE   MPE  MAPE  MASE RMSSE    ACF1
  <chr>         <chr>   <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>
1 arima_311_210 Test  0.00781 0.0678 0.0492 0.374  6.24 0.822 0.940 -0.111 
2 arima_211_111 Test  0.0241  0.0691 0.0529 2.10   6.51 0.883 0.959 -0.141 
3 arima_112_112 Test  0.0169  0.0675 0.0537 1.16   6.57 0.896 0.936 -0.0957
4 arima_212_212 Test  0.0145  0.0684 0.0537 1.04   6.57 0.896 0.949 -0.0582
5 arima_210_311 Test  0.0234  0.0709 0.0564 1.92   6.80 0.942 0.983 -0.0367
6 arima_110_312 Test  0.0224  0.0686 0.0572 1.87   6.83 0.955 0.951 -0.0588
7 arima_213_110 Test  0.0491  0.0857 0.0632 5.20   7.48 1.06  1.19  -0.196 

Pronóstico a futuro

 

h02_fut<- h02 |> 
  model(
    arima_311_210 = ARIMA(log(Cost) ~ pdq(2,0,1) + PDQ(1,1,0)) 
  ) |> 
  forecast(h= '24 months')

h02_fut
# A fable: 24 x 4 [1M]
# Key:     .model [1]
   .model            Month                Cost .mean
   <chr>             <mth>              <dist> <dbl>
 1 arima_311_210 2008 jul. t(N(0.026, 0.0051)) 1.03 
 2 arima_311_210 2008 ago.  t(N(0.11, 0.0051)) 1.12 
 3 arima_311_210 2008 sep.  t(N(0.13, 0.0057)) 1.14 
 4 arima_311_210 2008 oct.   t(N(0.18, 0.006)) 1.20 
 5 arima_311_210 2008 nov.  t(N(0.19, 0.0064)) 1.22 
 6 arima_311_210 2008 dic.  t(N(0.21, 0.0067)) 1.23 
 7 arima_311_210 2009 ene.  t(N(0.26, 0.0069)) 1.30 
 8 arima_311_210 2009 feb. t(N(-0.29, 0.0072)) 0.749
 9 arima_311_210 2009 mar. t(N(-0.34, 0.0074)) 0.712
10 arima_311_210 2009 abr. t(N(-0.26, 0.0076)) 0.776
# ℹ 14 more rows
h02_fut |> 
  autoplot(h02)