## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 7
)

## ----setup, message=FALSE-----------------------------------------------------
library(dplyr)
library(tsibble)
library(fable)
library(icomb)
library(feasts)
library(ggplot2)
library(plotly)

## -----------------------------------------------------------------------------
tourism_gts <- tourism |>
  aggregate_key(State * Purpose,
                Trips = sum(Trips))
tourism_gts

## -----------------------------------------------------------------------------
fit <- tourism_gts |>
  model(base = ETS(Trips))
fit

## ----eval=FALSE, echo=FALSE---------------------------------------------------
# # storing precomputed results to build vignette quickly
# fit_recon <- fit |>
#   reconcile(
#     ols = min_trace(base, method = "ols"),
#     icomb = icomb(base, train_size = 55)
#   )
# saveRDS(fit_recon, file = "../inst/extdata/fit_recon.rds")

## ----eval=FALSE---------------------------------------------------------------
# fit_recon <- fit |>
#   reconcile(
#     ols = min_trace(base, method = "ols"),
#     icomb = icomb(base, train_size = 55)
#   )

## ----echo=FALSE---------------------------------------------------------------
fit_recon <- readRDS(system.file("extdata", "fit_recon.rds", package = "icomb"))

## -----------------------------------------------------------------------------
fit_recon

## -----------------------------------------------------------------------------
glance_output <- fit_recon |>
  glance()
glance_output

## -----------------------------------------------------------------------------
tourism_features <- tourism_gts |>
  features(Trips, feature_set(pkgs = "feasts"))
tourism_features

## -----------------------------------------------------------------------------
pcs <- tourism_features |>
  select(-State, -Purpose, -zero_run_mean, -zero_start_prop, -zero_end_prop) |>
  prcomp(scale = TRUE) |>
  broom::augment(tourism_features)
pcs

## -----------------------------------------------------------------------------
all_info <- glance_output |>
  filter(.model == "icomb") |>
  select(State, Purpose, .included) |>
  full_join(pcs, by = c("State", "Purpose"))

all_info <- accuracy(fit) |>
  select(-.model, -.type) |>
  full_join(all_info, by = c("State", "Purpose"))
all_info

## -----------------------------------------------------------------------------
tourism_viz <- all_info |>
  ggplot(aes(
    x = .fittedPC1,
    y = .fittedPC2,
    colour = .included,
    text = paste0(
      "PC1: ", round(.fittedPC1, 2), "<br>",
      "PC2: ", round(.fittedPC2, 2), "<br>",
      "State: ", State, "<br>",
      "Purpose: ", Purpose, "<br>",
      "MAPE: ", round(MAPE, 2), "<br>",
      "RMSSE: ", round(RMSSE, 2), "<br>",
      "MASE: ", round(MASE, 2)
    )
  )) +
  geom_point()
tourism_viz

## -----------------------------------------------------------------------------
tourism_viz |>
  ggplotly(tooltip = "text")

## -----------------------------------------------------------------------------
tourism_feature_plot <- all_info |>
  ggplot(aes(
    x = trend_strength,
    y = seasonal_strength_year,
    colour = .included,
    text = paste0(
      "Trend_strength: ", round(trend_strength, 2), "<br>",
      "Seasonal_strength_year: ", round(seasonal_strength_year, 2), "<br>",
      "State: ", State, "<br>",
      "Purpose: ", Purpose, "<br>"
    )
  )) +
  geom_point()
tourism_feature_plot

## -----------------------------------------------------------------------------
tourism_feature_plot |>
  ggplotly(tooltip = "text")

## -----------------------------------------------------------------------------
tourism_purpose_plot <- all_info |>
  ggplot(aes(
    x = trend_strength,
    y = seasonal_strength_year,
    colour = .included,
    text = paste0(
      "State: ", State, "<br>",
      "Purpose: ", Purpose, "<br>")
  )) +
  geom_point() +
  facet_wrap(vars(Purpose))
tourism_purpose_plot

## -----------------------------------------------------------------------------
tourism_purpose_plot |>
  ggplotly(text = "text")

## -----------------------------------------------------------------------------
tourism_avg_gts <- tourism_gts |>
  filter(!is_aggregated(State), !is_aggregated(Purpose)) |>
  aggregate_key(State * Purpose,
                Trips = mean(Trips))

## ----echo=FALSE, eval=FALSE---------------------------------------------------
# fc <- tourism_avg_gts |>
#   model(base = ETS(Trips)) |>
#   reconcile(icomb = icomb(base, train_size = 55)) |>
#   forecast()
# saveRDS(fc, file = "../inst/extdata/fc.rds")

## ----eval=FALSE---------------------------------------------------------------
# fc <- tourism_avg_gts |>
#   model(base = ETS(Trips)) |>
#   reconcile(icomb = icomb(base, train_size = 55)) |>
#   forecast()

## ----echo=FALSE---------------------------------------------------------------
fc <- readRDS(system.file("extdata", "fc.rds", package = "icomb"))

## -----------------------------------------------------------------------------
fc |>
  filter(!is_aggregated(State), !is_aggregated(Purpose), .model == "icomb") |>
  aggregate_key(State * Purpose,
                mean_fc = mean(.mean)) |>
  full_join(fc |> filter(.model == "icomb")) |>
  mutate(diff = mean_fc - .mean) |>
  pull(diff) |>
  range()

