Clase 5

Author

Ivanna Herrera

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

mex |> 
  features(Growth, features = guerrero)
# A tibble: 1 × 1
  lambda_guerrero
            <dbl>
1           0.725
mex |> 
  autoplot(Growth)

mex |> 
  autoplot(box_cox(Growth, lambda = 0.7249))

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
# A mable: 1 x 3
    media   naive         drift
  <model> <model>       <model>
1  <MEAN> <NAIVE> <RW w/ drift>

Diagnóstico de residuos

mex_fit |> 
  augment()
# A tsibble: 171 x 6 [1Y]
# Key:       .model [3]
   .model  Year Growth .fitted  .resid  .innov
   <chr>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
 1 media   1961   5.00    3.45  1.55    1.04  
 2 media   1962   4.66    3.45  1.21    0.826 
 3 media   1963   8.11    3.45  4.65    2.90  
 4 media   1964  11.9     3.45  8.45    4.92  
 5 media   1965   7.10    3.45  3.65    2.33  
 6 media   1966   6.10    3.45  2.64    1.73  
 7 media   1967   5.85    3.45  2.40    1.58  
 8 media   1968   9.42    3.45  5.97    3.63  
 9 media   1969   3.42    3.45 -0.0337 -0.0240
10 media   1970   6.50    3.45  3.05    1.97  
# ℹ 161 more rows
mex_fit |> 
  augment() |> 
  features(.innov, ljung_box, lag = 10, dof = 0)
# A tibble: 3 × 3
  .model lb_stat lb_pvalue
  <chr>    <dbl>     <dbl>
1 drift     16.5    0.0863
2 media     15.1    0.128 
3 naive     16.5    0.0863

Como el p-value de los tres 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
# A fable: 18 x 4 [1Y]
# Key:     .model [3]
   .model  Year         Growth .mean
   <chr>  <dbl>         <dist> <dbl>
 1 media   2018     t(N(2, 8))  4.08
 2 media   2019     t(N(2, 8))  4.08
 3 media   2020     t(N(2, 8))  4.08
 4 media   2021     t(N(2, 8))  4.08
 5 media   2022     t(N(2, 8))  4.08
 6 media   2023     t(N(2, 8))  4.08
 7 naive   2018 t(N(0.93, 11))  3.15
 8 naive   2019 t(N(0.93, 22))  4.26
 9 naive   2020 t(N(0.93, 33))  5.37
10 naive   2021 t(N(0.93, 44))  6.48
11 naive   2022 t(N(0.93, 56))  7.59
12 naive   2023 t(N(0.93, 67))  8.70
13 drift   2018  t(N(0.9, 12))  3.16
14 drift   2019 t(N(0.86, 23))  4.34
15 drift   2020 t(N(0.82, 36))  5.59
16 drift   2021 t(N(0.78, 48))  6.91
17 drift   2022 t(N(0.75, 62))  8.30
18 drift   2023 t(N(0.71, 75))  9.77
mex_fc |> 
  autoplot(mex) +
  facet_wrap(~ .model)

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).