dds |> gg_season(ibcr_ce) |> plotly_build()
dds |> gg_subseries(ibcr_ce) |> plotly_build()
dds |> 
  model(classical_decomposition(ibcr_ce,
                              type = "additive")) |>
  components() |>
  autoplot() |>
  labs(title = "Decomposição Adivita por Média Móvel do IBCR Cearense")
## [[1]]

## 
## $title
## [1] "Decomposição Adivita por Média Móvel do IBCR Cearense"
## 
## attr(,"class")
## [1] "labels"
dds |> 
  features(ibcr_ce,c(unitroot_kpss, unitroot_pp))
## # A tibble: 1 × 4
##   kpss_stat kpss_pvalue pp_stat pp_pvalue
##       <dbl>       <dbl>   <dbl>     <dbl>
## 1      3.60        0.01   -2.81    0.0600
ce_models <-dds |>
  model(ce_auto = ARIMA(ibcr_ce ~ 0 + pdq(2,1,1) + PDQ(0,1,2, period = "1 year")))

glance(ce_models) |>
  arrange(AICc) |>
  select(.model:BIC)
## # A tibble: 1 × 6
##   .model  sigma2 log_lik   AIC  AICc   BIC
##   <chr>    <dbl>   <dbl> <dbl> <dbl> <dbl>
## 1 ce_auto   8.22   -539. 1091. 1091. 1111.
ggplotly(ce_models |>
           dplyr::select(ce_auto) |>
           fitted() |>
           ggplot() +
           geom_line(aes(x=dt, y=.fitted),
                     alpha = .66, size= .8,
                     linetype="twodash") +
           geom_line(aes(x=dt, y=dds$ibcr_ce),
                     color = 'seagreen4',
                     alpha = .75, size= .8))
ce_models |>
  dplyr::select(ce_auto) |>
  fitted() |>
  ggplot() +
  geom_col(aes(x=dt, y=(.fitted - dds$ibcr_ce)), color='seagreen4') +
  labs(title = 'Erros entre Estimado e Valor Real da Série Cearense')

ce_models |>
  dplyr::select(ce_auto) |>
  gg_tsresiduals()

prev_ce <- ce_models |> dplyr::select(ce_auto) |> fabletools::forecast(h=12)

prev_ce |> autoplot(dds)

time_prev <- seq(as.Date("2018-01-01"),
                 as.Date("2023-01-01"),
                 by = "1 month") |> yearmonth()

ggplot() +
  geom_line(aes(
    x=time_prev,
    y= c(dds$ibcr_ce[dds$dt >= yearmonth('2018 Jan')],
         prev_ce$.mean)),
    color='tomato3',
    size=.8) +
  geom_line(aes(x=prev_ce$dt,
                y=prev_ce$.mean),
            size=.8) +
  labs(title ='Previsão do IBCR Cearense',
       subtitle = "ARIMA(2,1,1)(0,1,2)(12)",
       x= 'Tempo',
       y='Índice')