Tarea pronósticos

Author

Diego Lozoya y José Melchor

Published

March 6, 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() 

##Modelo

gdp_fit <- gdp_train |> 
  model(
    snaive = SNAIVE(log(price)),
    drift  = RW(log(price) ~ drift())
  )

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(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()`).

##P-value

gdp_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 24, dof = 0)
# A tibble: 2 × 4
  symbol           .model lb_stat lb_pvalue
  <chr>            <chr>    <dbl>     <dbl>
1 CLVMNACSCAB1GQDE drift     8.45     0.999
2 CLVMNACSCAB1GQDE snaive  137.       0    
gdp_train_accu <- accuracy(gdp_fit) |> 
  arrange(MAPE)
gdp_train_accu |> 
  select(symbol:.type, MAPE, RMSE, MAE, MASE)
# A tibble: 2 × 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 snaive Training 2.16  18331. 13986. 1    

##Modelación con descomposición

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

gdp_fit <- gdp_fit |> 
  left_join(gdp_fit_dcmp)
Joining with `by = join_by(symbol)`
gdp_fit |> 
  accuracy() |> 
  select(symbol:.type, MAPE, RMSE, MAE, MASE) |> 
  arrange(MAPE)
# A tibble: 3 × 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 snaive Training 2.16  18331. 13986. 1    
gdp_fit |> 
  select(stlf) |> 
  gg_tsresiduals()
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 |> 
  augment() |> 
  features(.innov, ljung_box)
# A tibble: 3 × 4
  symbol           .model lb_stat lb_pvalue
  <chr>            <chr>    <dbl>     <dbl>
1 CLVMNACSCAB1GQDE drift     2.93  8.70e- 2
2 CLVMNACSCAB1GQDE snaive   50.7   1.10e-12
3 CLVMNACSCAB1GQDE stlf      2.87  9.04e- 2

##Pronóstico

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

gdp_fc
# A fable: 18 x 5 [1Q]
# Key:     symbol, .model [3]
   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 drift  2021 Q1 t(N(14, 0.00026)) 736227.
 8 CLVMNACSCAB1GQDE drift  2021 Q2 t(N(14, 0.00052)) 738648.
 9 CLVMNACSCAB1GQDE drift  2021 Q3 t(N(14, 0.00079)) 741080.
10 CLVMNACSCAB1GQDE drift  2021 Q4  t(N(14, 0.0011)) 743521.
11 CLVMNACSCAB1GQDE drift  2022 Q1  t(N(14, 0.0013)) 745973.
12 CLVMNACSCAB1GQDE drift  2022 Q2  t(N(14, 0.0016)) 748435.
13 CLVMNACSCAB1GQDE stlf   2021 Q1 t(N(14, 0.00026)) 735965.
14 CLVMNACSCAB1GQDE stlf   2021 Q2 t(N(14, 0.00053)) 740547.
15 CLVMNACSCAB1GQDE stlf   2021 Q3 t(N(14, 0.00081)) 742864.
16 CLVMNACSCAB1GQDE stlf   2021 Q4  t(N(14, 0.0011)) 743521.
17 CLVMNACSCAB1GQDE stlf   2022 Q1  t(N(14, 0.0014)) 745707.
18 CLVMNACSCAB1GQDE stlf   2022 Q2  t(N(14, 0.0017)) 750358.
gdp_fc |> 
  filter(.model == "drift") |> 
  autoplot(gdp) 

gdp_fc |> 
  filter(.model == "snaive") |> 
  autoplot(gdp) 

gdp_fc |> 
  filter(.model == "stlf") |> 
  autoplot(gdp) 

###Error de pronóstico

gdp_fc |> 
  accuracy(gdp) |> 
  select(.model:.type, MAPE, RMSE, MAE, MASE) |> 
  arrange(MAPE)
# A tibble: 3 × 7
  .model symbol           .type  MAPE   RMSE    MAE  MASE
  <chr>  <chr>            <chr> <dbl>  <dbl>  <dbl> <dbl>
1 stlf   CLVMNACSCAB1GQDE Test  0.580  5811.  4284. 0.306
2 drift  CLVMNACSCAB1GQDE Test  0.705  6223.  5218. 0.373
3 snaive CLVMNACSCAB1GQDE Test  4.74  46399. 35359. 2.53 

##Pronóstico del futuro

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

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

gdp_fc_fut |> 
  autoplot(gdp)

#Produccion de ladrillos en Australia

##Importar datos

recent_production <- aus_production %>% filter(year(Quarter) >= 1982)
bricks_train <- recent_production %>% filter(year(Quarter) <= 2004)

p <- bricks_train |> 
  autoplot(Bricks)

ggplotly(p, dynamicTicks = TRUE) |> 
  rangeslider()
bricks_train |> 
  gg_season(Bricks) |> 
  ggplotly()
bricks_train |> 
  model(stl = STL(Bricks, robust = TRUE)) |> 
  components() |> 
  autoplot() |> 
  ggplotly()
bricks_fit <- bricks_train |> 
  model(
    Mean = MEAN(Bricks),
    `Naïve` = NAIVE(Bricks),
    `Seasonal naïve` = SNAIVE(Bricks),
    Drift = RW(Bricks ~ drift())
  )

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

bricks_fit |> 
  select(`Naïve`) |> 
  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()`).

bricks_fit |> 
  select(`Seasonal naïve`) |> 
  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 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()`).