Tarea ARIMA

Author

Diego Lozoya y José Melchor

Published

March 18, 2024

pkgs

PIB Alemania

Importar datos

gdp <- tidyquant::tq_get(
  x    = "CLVMNACSCAB1GQDE",
  get  = "economic.data",
  from = "1997-01-01"
)
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
gdp <- gdp |> 
  mutate(date = yearquarter(date)) |> 
  as_tsibble(
    index = date,
    key   = symbol
  )

gdp
# A tsibble: 108 x 3 [1Q]
# Key:       symbol [1]
   symbol              date   price
   <chr>              <qtr>   <dbl>
 1 CLVMNACSCAB1GQDE 1997 Q1 543909.
 2 CLVMNACSCAB1GQDE 1997 Q2 550950.
 3 CLVMNACSCAB1GQDE 1997 Q3 552622.
 4 CLVMNACSCAB1GQDE 1997 Q4 556805.
 5 CLVMNACSCAB1GQDE 1998 Q1 561998.
 6 CLVMNACSCAB1GQDE 1998 Q2 559628.
 7 CLVMNACSCAB1GQDE 1998 Q3 562276.
 8 CLVMNACSCAB1GQDE 1998 Q4 559976.
 9 CLVMNACSCAB1GQDE 1999 Q1 566459.
10 CLVMNACSCAB1GQDE 1999 Q2 566459.
# ℹ 98 more rows
gdp_train <- gdp |> 
  filter_index(. ~ "2020 Q4")

gdp_train
# A tsibble: 96 x 3 [1Q]
# Key:       symbol [1]
   symbol              date   price
   <chr>              <qtr>   <dbl>
 1 CLVMNACSCAB1GQDE 1997 Q1 543909.
 2 CLVMNACSCAB1GQDE 1997 Q2 550950.
 3 CLVMNACSCAB1GQDE 1997 Q3 552622.
 4 CLVMNACSCAB1GQDE 1997 Q4 556805.
 5 CLVMNACSCAB1GQDE 1998 Q1 561998.
 6 CLVMNACSCAB1GQDE 1998 Q2 559628.
 7 CLVMNACSCAB1GQDE 1998 Q3 562276.
 8 CLVMNACSCAB1GQDE 1998 Q4 559976.
 9 CLVMNACSCAB1GQDE 1999 Q1 566459.
10 CLVMNACSCAB1GQDE 1999 Q2 566459.
# ℹ 86 more rows

Gráfica

p <- gdp_train |> 
  autoplot(price) +
  labs(
    title = "Time series plot of the Real GDP for Germany",
    y = "GDP"
  )
 
ggplotly(p, dynamicTicks = TRUE) |> 
  rangeslider()

Estacionalidad

gdp_train |> 
  gg_season(price) |> 
  ggplotly()

Descomposición

gdp_train |> 
  model(stl = STL(price, robust = TRUE)) |> 
  components() |> 
  autoplot() |> 
  ggplotly()

Transformación

gdp_train |> 
  autoplot(log(price)) +
  ggtitle("Log of the Real GDP of Germany")

gdp_train |> 
  model(stl = STL(log(price) ~ season(window = "periodic"), robust = TRUE)) |> 
  components() |> 
  autoplot() 

Suavización exponencial

Modelo

gdp_fit <- gdp_train |> 
  model(
    snaive = SNAIVE(log(price)),
    naive = NAIVE(log(price)),
    drift = RW(log(price) ~ drift()),
    stlf = decomposition_model(
          STL(log(price) ~ season(window = "periodic"), robust = TRUE),
          RW(season_adjust ~ drift())
        ),
    ets_ANA = ETS(log(price) ~ error("A") + trend("N") + season("A")),
    ets_AAdA = ETS(log(price) ~ error("A") + trend("Ad") + season("A")),
    ets_MAdM = ETS(log(price) ~ error("M") + trend("Ad") + season("M")),
  )

gdp_fit |> 
  select(snaive) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the Seasonal Naïve Model")
Warning: Removed 4 rows containing missing values (`geom_line()`).
Warning: Removed 4 rows containing missing values (`geom_point()`).
Warning: Removed 4 rows containing non-finite values (`stat_bin()`).

gdp_fit |> 
  select(naive) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the Naïve Model")
Warning: Removed 1 row containing missing values (`geom_line()`).
Warning: Removed 1 rows containing missing values (`geom_point()`).
Warning: Removed 1 rows containing non-finite values (`stat_bin()`).

gdp_fit |> 
  select(drift) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the Drift Model")
Warning: Removed 1 row containing missing values (`geom_line()`).
Warning: Removed 1 rows containing missing values (`geom_point()`).
Warning: Removed 1 rows containing non-finite values (`stat_bin()`).

gdp_fit |> 
  select(stlf) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the stlf Model")
Warning: Removed 4 rows containing missing values (`geom_line()`).
Warning: Removed 4 rows containing missing values (`geom_point()`).
Warning: Removed 4 rows containing non-finite values (`stat_bin()`).

gdp_fit |> 
  select(ets_ANA) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_ANA Model")

gdp_fit |> 
  select(ets_AAdA) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_AAdA Model")

gdp_fit |> 
  select(ets_MAdM) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_MAdM Model")

P-value

gdp_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 24, dof = 0)
# A tibble: 7 × 4
  symbol           .model   lb_stat lb_pvalue
  <chr>            <chr>      <dbl>     <dbl>
1 CLVMNACSCAB1GQDE drift       8.45     0.999
2 CLVMNACSCAB1GQDE ets_AAdA    7.93     0.999
3 CLVMNACSCAB1GQDE ets_ANA     8.05     0.999
4 CLVMNACSCAB1GQDE ets_MAdM    8.24     0.999
5 CLVMNACSCAB1GQDE naive       8.45     0.999
6 CLVMNACSCAB1GQDE snaive    137.       0    
7 CLVMNACSCAB1GQDE stlf        9.00     0.998
gdp_train_accu <- accuracy(gdp_fit) |> 
  arrange(MAPE)
gdp_train_accu |> 
  select(symbol:.type, MAPE, RMSE, MAE, MASE)
# A tibble: 7 × 7
  symbol           .model   .type     MAPE   RMSE    MAE  MASE
  <chr>            <chr>    <chr>    <dbl>  <dbl>  <dbl> <dbl>
1 CLVMNACSCAB1GQDE drift    Training 0.781 10862.  5113. 0.366
2 CLVMNACSCAB1GQDE stlf     Training 0.816 11188.  5366. 0.384
3 CLVMNACSCAB1GQDE ets_MAdM Training 0.861 10487.  5635. 0.403
4 CLVMNACSCAB1GQDE ets_AAdA Training 0.864 10504.  5653. 0.404
5 CLVMNACSCAB1GQDE naive    Training 0.868 11023.  5688. 0.407
6 CLVMNACSCAB1GQDE ets_ANA  Training 0.946 10936.  6156. 0.440
7 CLVMNACSCAB1GQDE snaive   Training 2.16  18331. 13986. 1    

Entrenamiento de pronóstico

gdp_fc <- gdp_fit |> 
  forecast(h = 12) 

gdp_fc
# A fable: 84 x 5 [1Q]
# Key:     symbol, .model [7]
   symbol           .model    date             price   .mean
   <chr>            <chr>    <qtr>            <dist>   <dbl>
 1 CLVMNACSCAB1GQDE snaive 2021 Q1 t(N(14, 0.00079)) 736685.
 2 CLVMNACSCAB1GQDE snaive 2021 Q2 t(N(13, 0.00079)) 668769.
 3 CLVMNACSCAB1GQDE snaive 2021 Q3 t(N(13, 0.00079)) 728387.
 4 CLVMNACSCAB1GQDE snaive 2021 Q4 t(N(14, 0.00079)) 734105.
 5 CLVMNACSCAB1GQDE snaive 2022 Q1  t(N(14, 0.0016)) 736975.
 6 CLVMNACSCAB1GQDE snaive 2022 Q2  t(N(13, 0.0016)) 669033.
 7 CLVMNACSCAB1GQDE snaive 2022 Q3  t(N(13, 0.0016)) 728674.
 8 CLVMNACSCAB1GQDE snaive 2022 Q4  t(N(14, 0.0016)) 734394.
 9 CLVMNACSCAB1GQDE snaive 2023 Q1  t(N(14, 0.0024)) 737266.
10 CLVMNACSCAB1GQDE snaive 2023 Q2  t(N(13, 0.0024)) 669297.
# ℹ 74 more rows
gdp_fc |> 
  autoplot(gdp |> filter_index("2005 Q1" ~ .), size = 1) +
  facet_wrap(~ .model, ncol = 3) +
  theme(legend.position = "none")

Error del pronóstico

gdp_fc |> 
  accuracy(gdp) |> 
  select(.model, .type, MAPE, RMSE, MAE, MASE) |> 
  arrange(MAPE)
# A tibble: 7 × 6
  .model   .type  MAPE   RMSE    MAE  MASE
  <chr>    <chr> <dbl>  <dbl>  <dbl> <dbl>
1 stlf     Test  0.701  6709.  5234. 0.374
2 drift    Test  0.745  6607.  5559. 0.397
3 ets_AAdA Test  2.00  15563. 14984. 1.07 
4 naive    Test  2.03  15784. 15225. 1.09 
5 ets_MAdM Test  2.04  15869. 15299. 1.09 
6 ets_ANA  Test  2.56  21360. 19184. 1.37 
7 snaive   Test  4.39  42755. 32859. 2.35 

Pronóstico a futuro

gdp_fit_fut <- gdp_train |> 
  model(
    stlf = decomposition_model(
          STL(log(price) ~ season(window = "periodic"), robust = TRUE),
          RW(season_adjust ~ drift())
        )
  )

gdp_fc_fut <- gdp_fit_fut |> 
  forecast(h = "3 years")

gdp_fc_fut |> 
  autoplot(gdp_train)

El modelo que más de ajusta a los datos, y por ende se toma como pronóstico es el modelo stlf, el cuál combina una descomposición de tipo STL, con un modelo drift estacional. Aunque la serie de tiempo no muestra estacionalidad, el hecho de que modelo la incluya, ayuda a su ajuste, ya que la ondulación del pronóstico se asemeja más a el crecimeinto del PIB de cualquier país. Por otro lado el segundo modelo que mejor se ajusta, sorprendentemente, es el modelo drift. No fuimos capaces de encontrar un modelo de suavización exponencial que se ajustara mejor a los datos.

ARIMA

gdp |> 
  features(log(price), unitroot_ndiffs) 
# A tibble: 1 × 2
  symbol           ndiffs
  <chr>             <int>
1 CLVMNACSCAB1GQDE      1
gdp_diff <- diff(gdp$price)

gdp_diff
  [1]   7040.2   1672.9   4182.3   5192.7  -2369.8   2648.6  -2300.1   6482.4
  [9]      0.0   6273.1   4739.7   8242.6   4739.7   -278.8  -1742.6  11832.1
 [17]   -209.1  -1254.6   -557.6  -3066.9   2578.9   2927.5   -836.4  -8904.5
 [25]    627.3   4530.7   1672.9  -1045.6   3066.9  -1254.6   -487.9    -52.5
 [33]   3206.4   5088.5   2927.6   5907.2  10525.2   4809.5   9549.3    418.1
 [41]   5436.9   3276.0   4182.2   4182.2  -1533.4  -4251.9 -10385.8 -30041.9
 [49]    975.8   3415.4   4530.7   5001.6  14079.6   5297.3   5227.6  12564.2
 [57]    627.3   5855.1  -2091.1   1463.7   1254.7   1951.7  -3136.7  -3014.4
 [65]   7248.9   3694.2   1881.9   6639.4    139.4   3276.0   5576.2  -3485.1
 [73]   5367.2   2718.3   3136.6   5924.8   3415.5   1394.0   2718.4   8573.5
 [81]   6691.5   4600.4   6970.3  -4478.4   5994.3  -6900.4   6342.8   5088.4
 [89]   -278.8   -139.4   2230.5 -13244.0 -67889.1  59594.6   5715.5  -9339.5
 [97]  16170.8   4948.8     69.7   7475.8   -975.9   2718.4  -3066.8    784.1
[105]    209.1    -69.7  -2160.8
gdp |> 
  gg_tsdisplay(log(price), plot_type = "partial")

Modelo

gdp_fit <- gdp_train |> 
  model(
    arima_010 = ARIMA(log(price) ~ pdq(1, 1, 0) + PDQ(0, 0, 0)),
    arima_011 = ARIMA(log(price) ~ pdq(1, 1, 0) + PDQ(0, 0, 0)),
    arima_012 = ARIMA(log(price) ~ pdq(1, 1, 0) + PDQ(0, 0, 0)),
    arima_110 = ARIMA(log(price) ~ pdq(1, 1, 0) + PDQ(0, 0, 0)),
    arima_111 = ARIMA(log(price) ~ pdq(1, 1, 1) + PDQ(0, 0, 0)),
    arima_210 = ARIMA(log(price) ~ pdq(2, 1, 0) + PDQ(0, 0, 0)),
    arima_212 = ARIMA(log(price) ~ pdq(2, 1, 2) + PDQ(0, 0, 0)),
    arima_auto = ARIMA(log(price) ~ PDQ(0, 0, 0),
                       stepwise = FALSE, approximation = FALSE),
  )

gdp_fit
# A mable: 1 x 9
# Key:     symbol [1]
  symbol               arima_010               arima_011               arima_012
  <chr>                  <model>                 <model>                 <model>
1 CLVMN… <ARIMA(1,1,0) w/ drift> <ARIMA(1,1,0) w/ drift> <ARIMA(1,1,0) w/ drift>
# ℹ 5 more variables: arima_110 <model>, arima_111 <model>, arima_210 <model>,
#   arima_212 <model>, arima_auto <model>
gdp_fit |> 
  select(arima_010) |> 
  report()
Series: price 
Model: ARIMA(1,1,0) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1  constant
      -0.1719    0.0037
s.e.   0.1008    0.0016

sigma^2 estimated as 0.0002518:  log likelihood=260.17
AIC=-514.34   AICc=-514.08   BIC=-506.68
gdp_fit |> 
  select(arima_011) |> 
  report()
Series: price 
Model: ARIMA(1,1,0) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1  constant
      -0.1719    0.0037
s.e.   0.1008    0.0016

sigma^2 estimated as 0.0002518:  log likelihood=260.17
AIC=-514.34   AICc=-514.08   BIC=-506.68
gdp_fit |> 
  select(arima_012) |> 
  report()
Series: price 
Model: ARIMA(1,1,0) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1  constant
      -0.1719    0.0037
s.e.   0.1008    0.0016

sigma^2 estimated as 0.0002518:  log likelihood=260.17
AIC=-514.34   AICc=-514.08   BIC=-506.68
gdp_fit |> 
  select(arima_110) |> 
  report()
Series: price 
Model: ARIMA(1,1,0) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1  constant
      -0.1719    0.0037
s.e.   0.1008    0.0016

sigma^2 estimated as 0.0002518:  log likelihood=260.17
AIC=-514.34   AICc=-514.08   BIC=-506.68
gdp_fit |> 
  select(arima_111) |> 
  report()
Series: price 
Model: ARIMA(1,1,1) 
Transformation: log(price) 

Coefficients:
          ar1      ma1
      -0.0064  -0.1268
s.e.   0.5372   0.5307

sigma^2 estimated as 0.000265:  log likelihood=257.74
AIC=-509.49   AICc=-509.23   BIC=-501.83
gdp_fit |> 
  select(arima_210) |> 
  report()
Series: price 
Model: ARIMA(2,1,0) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1      ar2  constant
      -0.1911  -0.0998    0.0039
s.e.   0.1031   0.1209    0.0016

sigma^2 estimated as 0.0002527:  log likelihood=260.51
AIC=-513.02   AICc=-512.58   BIC=-502.81
gdp_fit |> 
  select(arima_212) |> 
  report()
Series: price 
Model: ARIMA(2,1,2) w/ drift 
Transformation: log(price) 

Coefficients:
          ar1      ar2     ma1     ma2  constant
      -0.4824  -0.8125  0.2825  0.6340    0.0069
s.e.   0.4003   0.2205  0.4185  0.2744    0.0031

sigma^2 estimated as 0.0002526:  log likelihood=261.39
AIC=-510.79   AICc=-509.83   BIC=-495.46
gdp_fit |> 
  select(arima_auto) |> 
  report()
Series: price 
Model: ARIMA(0,1,1) w/ drift 
Transformation: log(price) 

Coefficients:
          ma1  constant
      -0.1933    0.0031
s.e.   0.1053    0.0013

sigma^2 estimated as 0.0002509:  log likelihood=260.35
AIC=-514.69   AICc=-514.43   BIC=-507.03

El modelo determinado automáticamente es un modelo ARIMA(0,1,1)

gdp_fit |> 
  glance() |> 
  arrange(AICc)
# A tibble: 8 × 9
  symbol           .model     sigma2 log_lik   AIC  AICc   BIC ar_roots ma_roots
  <chr>            <chr>       <dbl>   <dbl> <dbl> <dbl> <dbl> <list>   <list>  
1 CLVMNACSCAB1GQDE arima_au… 2.51e-4    260. -515. -514. -507. <cpl>    <cpl>   
2 CLVMNACSCAB1GQDE arima_010 2.52e-4    260. -514. -514. -507. <cpl>    <cpl>   
3 CLVMNACSCAB1GQDE arima_011 2.52e-4    260. -514. -514. -507. <cpl>    <cpl>   
4 CLVMNACSCAB1GQDE arima_012 2.52e-4    260. -514. -514. -507. <cpl>    <cpl>   
5 CLVMNACSCAB1GQDE arima_110 2.52e-4    260. -514. -514. -507. <cpl>    <cpl>   
6 CLVMNACSCAB1GQDE arima_210 2.53e-4    261. -513. -513. -503. <cpl>    <cpl>   
7 CLVMNACSCAB1GQDE arima_212 2.53e-4    261. -511. -510. -495. <cpl>    <cpl>   
8 CLVMNACSCAB1GQDE arima_111 2.65e-4    258. -509. -509. -502. <cpl>    <cpl>   
accuracy(gdp_fit) |> 
  arrange(MAE)
# A tibble: 8 × 11
  symbol      .model .type     ME   RMSE   MAE     MPE  MAPE  MASE RMSSE    ACF1
  <chr>       <chr>  <chr>  <dbl>  <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>   <dbl>
1 CLVMNACSCA… arima… Trai…   55.0 10660. 5300. 0.00297 0.814 0.379 0.582 -0.0309
2 CLVMNACSCA… arima… Trai…   55.0 10660. 5300. 0.00297 0.814 0.379 0.582 -0.0309
3 CLVMNACSCA… arima… Trai…   55.0 10660. 5300. 0.00297 0.814 0.379 0.582 -0.0309
4 CLVMNACSCA… arima… Trai…   55.0 10660. 5300. 0.00297 0.814 0.379 0.582 -0.0309
5 CLVMNACSCA… arima… Trai…   56.3 10630. 5320. 0.00345 0.817 0.380 0.580 -0.0143
6 CLVMNACSCA… arima… Trai…   55.8 10597. 5348. 0.00404 0.823 0.382 0.578 -0.0198
7 CLVMNACSCA… arima… Trai…   43.8 10467. 5475. 0.00303 0.843 0.391 0.571 -0.0162
8 CLVMNACSCA… arima… Trai… 2345.  10876. 6006. 0.357   0.920 0.429 0.593 -0.0710
gdp_fit |> 
  select(arima_010) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_011) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_012) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_110) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_111) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_210) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_212) |> 
  gg_tsresiduals()

gdp_fit |> 
  select(arima_auto) |> 
  gg_tsresiduals()

P-value

gdp_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 10) |> 
  mutate(residuos_autocorrelacionados = if_else(lb_pvalue >=0.05, "No autocorrelación", "Autocorrelacionados"))
# A tibble: 8 × 5
  symbol           .model     lb_stat lb_pvalue residuos_autocorrelacionados
  <chr>            <chr>        <dbl>     <dbl> <chr>                       
1 CLVMNACSCAB1GQDE arima_010     4.52     0.921 No autocorrelación          
2 CLVMNACSCAB1GQDE arima_011     4.52     0.921 No autocorrelación          
3 CLVMNACSCAB1GQDE arima_012     4.52     0.921 No autocorrelación          
4 CLVMNACSCAB1GQDE arima_110     4.52     0.921 No autocorrelación          
5 CLVMNACSCAB1GQDE arima_111     4.49     0.922 No autocorrelación          
6 CLVMNACSCAB1GQDE arima_210     5.32     0.869 No autocorrelación          
7 CLVMNACSCAB1GQDE arima_212     4.21     0.938 No autocorrelación          
8 CLVMNACSCAB1GQDE arima_auto    4.87     0.900 No autocorrelación          

Pronóstico a futuro

gdp_fit_fut <- gdp_train |> 
  model(
    arima_auto = ARIMA(log(price) ~ PDQ(0, 0, 0),
                       stepwise = FALSE, approximation = FALSE),
  )

gdp_fc_fut <- gdp_fit_fut |> 
  forecast(h = "3 years")

gdp_fc_fut |> 
  autoplot(gdp_train)

Produccion de ladrillos en Australia

Importar datos

bricks_production <- aus_production |> 
  select(Bricks)

bricks_production
# A tsibble: 218 x 2 [1Q]
   Bricks Quarter
    <dbl>   <qtr>
 1    189 1956 Q1
 2    204 1956 Q2
 3    208 1956 Q3
 4    197 1956 Q4
 5    187 1957 Q1
 6    214 1957 Q2
 7    227 1957 Q3
 8    222 1957 Q4
 9    199 1958 Q1
10    229 1958 Q2
# ℹ 208 more rows
recent_production <- bricks_production %>% filter(year(Quarter) >= 1981)
bricks_train <- recent_production %>% filter(year(Quarter) <= 2003)

bricks_train
# A tsibble: 92 x 2 [1Q]
   Bricks Quarter
    <dbl>   <qtr>
 1    519 1981 Q1
 2    581 1981 Q2
 3    581 1981 Q3
 4    578 1981 Q4
 5    501 1982 Q1
 6    560 1982 Q2
 7    512 1982 Q3
 8    412 1982 Q4
 9    303 1983 Q1
10    409 1983 Q2
# ℹ 82 more rows

Gráfica

p <- bricks_train |> 
  autoplot(Bricks)

ggplotly(p, dynamicTicks = TRUE) |> 
  rangeslider()

Estacionalidad

bricks_train |> 
  gg_season(Bricks) |> 
  ggplotly()

##Descomposición

bricks_train |> 
  model(stl = STL(Bricks, robust = TRUE)) |> 
  components() |> 
  autoplot() |> 
  ggplotly()

Modelo

bricks_fit <- bricks_train |> 
  model(
    snaive = SNAIVE(Bricks),
    naive = NAIVE(Bricks),
    drift  = RW(Bricks ~ drift()),
    stlf = decomposition_model(
          STL(Bricks ~ season(window = "periodic"), robust = TRUE),
          RW(season_adjust ~ drift())
        ),
    ets_ANA = ETS(Bricks ~ error("A") + trend("N") + season("A")),
    ets_AAdA = ETS(Bricks ~ error("A") + trend("Ad") + season("A")),
    ets_MAdM = ETS(Bricks ~ error("M") + trend("Ad") + season("M")),
    stl_ets_A = decomposition_model(
      STL(Bricks, robust=TRUE),
      ETS(season_year ~ error("A") + trend("N") + season("A")),
      ETS(season_adjust ~ error("A") + trend("Ad") + season("N"))
    ),
    stl_ets_M = decomposition_model(
      STL(Bricks, robust=TRUE),
      ETS(season_year ~ error("M") + trend("N") + season("M")),
      ETS(season_adjust ~ error("A") + trend("Ad") + season("N"))
    )
  )

bricks_fit |> 
  select(snaive) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the Seasonal Naïve Model")
Warning: Removed 4 rows containing missing values (`geom_line()`).
Warning: Removed 4 rows containing missing values (`geom_point()`).
Warning: Removed 4 rows containing non-finite values (`stat_bin()`).

bricks_fit |> 
  select(drift) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the Drift Model")
Warning: Removed 1 row containing missing values (`geom_line()`).
Warning: Removed 1 rows containing missing values (`geom_point()`).
Warning: Removed 1 rows containing non-finite values (`stat_bin()`).

bricks_fit |> 
  select(stlf) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the stlf Model")
Warning: Removed 4 rows containing missing values (`geom_line()`).
Warning: Removed 4 rows containing missing values (`geom_point()`).
Warning: Removed 4 rows containing non-finite values (`stat_bin()`).

bricks_fit |> 
  select(ets_ANA) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_ANA Model")

bricks_fit |> 
  select(ets_AAdA) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_AAdA Model")

bricks_fit |> 
  select(ets_MAdM) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the ets_MAdM Model")

bricks_fit |> 
  select(stl_ets_A) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the stl_ets_A Model")

bricks_fit |> 
  select(stl_ets_M) |> 
  gg_tsresiduals() +
  ggtitle("Residuals Diagnostics for the stl_ets_M Model")

P-value

bricks_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 24, dof = 0)
# A tibble: 9 × 3
  .model    lb_stat lb_pvalue
  <chr>       <dbl>     <dbl>
1 drift       366.     0     
2 ets_AAdA     25.5    0.381 
3 ets_ANA      33.7    0.0903
4 ets_MAdM     22.5    0.550 
5 naive       366.     0     
6 snaive      205.     0     
7 stl_ets_A    21.0    0.637 
8 stl_ets_M    29.7    0.194 
9 stlf         31.1    0.151 
bricks_train_accu <- accuracy(bricks_fit) |> 
  arrange(MAPE)
bricks_train_accu |> 
  select(.model, .type, MAPE, RMSE, MAE, MASE)
# A tibble: 9 × 6
  .model    .type     MAPE  RMSE   MAE  MASE
  <chr>     <chr>    <dbl> <dbl> <dbl> <dbl>
1 stl_ets_A Training  3.98  22.8  17.1 0.395
2 ets_ANA   Training  4.35  24.4  18.4 0.426
3 ets_AAdA  Training  4.36  24.3  18.5 0.428
4 stlf      Training  4.48  24.0  18.8 0.433
5 ets_MAdM  Training  4.50  25.0  19.3 0.445
6 drift     Training  8.74  44.6  37.1 0.856
7 naive     Training  8.76  44.6  37.1 0.856
8 stl_ets_M Training  8.92 176.   44.2 1.02 
9 snaive    Training 10.5   57.7  43.3 1    

Entrenamiento de pronóstico

bricks_fc <- bricks_fit |> 
  forecast(h = 6) 

bricks_fc
# A fable: 54 x 4 [1Q]
# Key:     .model [9]
   .model Quarter       Bricks .mean
   <chr>    <qtr>       <dist> <dbl>
 1 snaive 2004 Q1 N(387, 3334)   387
 2 snaive 2004 Q2 N(415, 3334)   415
 3 snaive 2004 Q3 N(440, 3334)   440
 4 snaive 2004 Q4 N(413, 3334)   413
 5 snaive 2005 Q1 N(387, 6667)   387
 6 snaive 2005 Q2 N(415, 6667)   415
 7 naive  2004 Q1 N(413, 1987)   413
 8 naive  2004 Q2 N(413, 3973)   413
 9 naive  2004 Q3 N(413, 5960)   413
10 naive  2004 Q4 N(413, 7946)   413
# ℹ 44 more rows
bricks_fc |> 
  autoplot(bricks_production |> filter_index("1990 Q1" ~ .), size = 1) +
  facet_wrap(~ .model, ncol = 3) +
  theme(legend.position = "none")
Warning: Removed 20 rows containing missing values (`geom_line()`).

Error del pronóstico

bricks_fc |> 
  accuracy(bricks_production) |> 
  select(.model:.type, MAPE, RMSE, MAE, MASE) |> 
  arrange(MAPE)
# A tibble: 9 × 6
  .model    .type  MAPE  RMSE   MAE  MASE
  <chr>     <chr> <dbl> <dbl> <dbl> <dbl>
1 stlf      Test   3.10  19.0  12.7 0.352
2 ets_ANA   Test   3.71  19.4  14.9 0.415
3 ets_AAdA  Test   3.77  19.0  15.1 0.420
4 ets_MAdM  Test   3.93  19.3  15.7 0.437
5 stl_ets_M Test   4.30  21.3  17.5 0.488
6 stl_ets_A Test   4.30  21.3  17.6 0.488
7 snaive    Test   4.62  19.9  18.3 0.510
8 drift     Test   5.36  26.4  21.0 0.585
9 naive     Test   5.38  27.2  20.8 0.579

Pronóstico a futuro

bricks_fit_fut <- bricks_train |> 
  model(
    stlf = decomposition_model(
          STL(Bricks ~ season(window = "periodic"), robust = TRUE),
          RW(season_adjust ~ drift())
    )
  )

bricks_fc_fut <- bricks_fit_fut |> 
  forecast(h = "5 years")

bricks_fc_fut |> 
  autoplot(bricks_train)

El modelo que presentó un menor error en el ajuste del pronóstico es el stlf, el cual mezcal una descomposición tipo STL con un modelo drift estacional. Caso contrario con la serie de tiempo pasada, esta si muestra estacionalidad de manera anual, por lo que hace sentido que el modelo se ajuste de mejor manera, ya que respeta la estacionalidad que se presenta en los datos de entramiento, y muestra la tendencia bajista, presentada en los últimos 20 años. El segundo modelo que mejor se ajusta es uno de suavización exponencial con error aditivo, sin tendencia y con una estacionalidad aditiva.