Pronóstico

Author

Luis Márquez

library(tidyverse)
library(fpp3)
library(plotly)
mex <- global_economy |> 
  filter(Country=='Mexico') |> 
  drop_na() |> 
  select(-c(Country, Code))

mex |> 
  features(Growth, features = guerrero)
m <- mex |> 
  autoplot(Growth)
ggplotly(m, dynamicTicks = TRUE)
mex |> 
  autoplot(box_cox(Growth, lambda = 0.7249))

Estimación de los modelos

mex_fit <- mex |> 
  model(
    media = MEAN(box_cox(Growth, lambda = 0.7249)),
    naive = NAIVE(box_cox(Growth, lambda = 0.7249)),
    drift = RW(box_cox(Growth, lambda = 0.7249) ~ drift())
  )
mex_fit

Diagnóstico de residuos

# Si hay transformaciones se usa innovaciones ya que es con residuos
# transformados
mex_fit |> 
  augment()
mex_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 10, dof = 0)

Como el p-value de los 3 modelos es mayor a nuestra \(\alpha\): \(p_{value} = 0.05\), NO RECHAZAMOS \(H_0 : \text{"Los residuos son ruido blanco"}\).

Media

mex_fit |> 
  select(media) |> 
  gg_tsresiduals()

Naive

mex_fit |> 
  select(naive) |> 
  gg_tsresiduals()

Drift

mex_fit |> 
  select(drift) |> 
  gg_tsresiduals()

Forecast

mex_fc <- mex_fit |> 
  forecast(h = "6 years")

mex_fc
mex_fc |> 
  autoplot(mex)

mex_fc |> 
  autoplot(mex) + 
  facet_wrap(~.model)

mex_fc |> 
  autoplot(mex) + 
  facet_wrap(~.model) + 
  theme(legend.position = "bottom")

Residuo es (original - modelo)

Ovejas

aus_livestock |> 
  distinct(Animal)

Datos

ovejas <- aus_livestock |> 
  filter(Animal == "Sheep",
         State == "New South Wales")
ovejas
ovejas |> 
  autoplot(Count)

Train/Test

ovejas_train <- ovejas |> 
  filter_index(.~"Dec. 2012")
ovejas_train

Visualización

ovejas_train |> 
  autoplot(Count)

Transformación

lambda <- ovejas_train |> 
  features(Count, features = guerrero) |> 
  pull(lambda_guerrero)
lambda
[1] 0.5806898
ovejas_train |> 
  autoplot(box_cox(Count, lambda))

Estacionalidad

ovejas_train |> 
  gg_season(Count)

ovejas_train |> 
  gg_season(box_cox(Count, lambda))

# De 0 a 1, marca la fuerza del componente en la serie
ovejas_train |> 
  features(Count, feat_stl)

STL Descomposición

ovejas_train |> 
  model(
    stl = STL(box_cox(Count, lambda), robust = TRUE)
  ) |> components() |> 
  autoplot()

Estimación del Modelo

ovejas_fit <- ovejas_train |> 
  model(
    snaive_bc = SNAIVE(box_cox(Count, lambda)),
    snaive = SNAIVE(Count),
    combi_bc = decomposition_model(
      STL(box_cox(Count, lambda), robust = TRUE),
      SNAIVE(season_year),
      RW(season_adjust ~ drift())
    ),
    combi = decomposition_model(
      STL(Count, robust = TRUE),
      SNAIVE(season_year),
      RW(season_adjust ~ drift())
    )
  )
ovejas_fit
ovejas_fit |> 
  select(snaive_bc) |> 
  gg_tsresiduals()

ovejas_fit |> 
  select(snaive) |> 
  gg_tsresiduals()

ovejas_fit |> 
  select(combi) |> 
  gg_tsresiduals()

ovejas_fit |> 
  select(combi_bc) |> 
  gg_tsresiduals()

ovejas_fit |> 
  augment() |> 
  features(.innov, ljung_box, dof = 0, lag = 24)
accuracy(ovejas_fit) |> 
  arrange(MAPE)

Forecast

ovejas_fc <- ovejas_fit |> 
  forecast(h = "6 years")

ovejas_fc
ovejas_fc |> 
  autoplot(ovejas |> filter_index("Jan. 2003" ~ .), level = NULL)

ovejas_fc |> 
  accuracy(ovejas) |> 
  arrange(MAPE)
ovejas |> 
  model(
    combi_bc = decomposition_model(
      STL(box_cox(Count, lambda), robust = TRUE),
      SNAIVE(season_year),
      RW(season_adjust ~ drift())
    )
  ) |> 
  
  forecast(h = "6 years") |> 
  autoplot(ovejas)