---
title: "Using Individual finnts Forecast Components"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{individual-forecast-components}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE
)
library(finnts)
```

The easiest way to use the finnts package is through the function `forecast_time_series()`, but instead of calling that function you can also call the sub components of the Finn forecast process. This could enable you to break out your time series forecast process into separate steps in a production pipeline, or even give you more control over how you use Finn.

Below is an example workflow of using the sub components of Finn.

## Get Data and Set Run Info

Let's get some example data and then set our Finn run info.

```{r, message = FALSE}
library(finnts)

hist_data <- timetk::m4_monthly %>%
  dplyr::filter(
    date >= "2013-01-01",
    id == "M2"
  ) %>%
  dplyr::rename(Date = date) %>%
  dplyr::mutate(id = as.character(id))

run_info <- set_run_info(
  experiment_name = "finnts_fcst",
  run_name = "finn_sub_component_run"
)
```

## Prep the Data

Clean and prepare our data before training models. We can even pull out our prepped data to see the features and transformations applied before models are trained.

```{r message=FALSE}
prep_data(
  run_info = run_info,
  input_data = hist_data,
  combo_variables = c("id"),
  target_variable = "value",
  date_type = "month",
  forecast_horizon = 6
)

R1_prepped_data_tbl <- get_prepped_data(
  run_info = run_info,
  recipe = "R1"
)

print(R1_prepped_data_tbl)

R2_prepped_data_tbl <- get_prepped_data(
  run_info = run_info,
  recipe = "R2"
)

print(R2_prepped_data_tbl)
```

## Train Individual Models

Now that our data is prepared for modeling, let's now train some models. First we need to create the model workflows, determine our back testing process, and how many hyperparameter combinations to try during the validation process.

Then we can kick off training each model on our data.

```{r, message = FALSE}
prep_models(
  run_info = run_info,
  models_to_run = c("arima", "ets", "glmnet"),
  num_hyperparameters = 2
)

train_models(
  run_info = run_info,
  run_global_models = FALSE
)
```

## Train Ensemble Models

After each individual model is trained, we can feed those predictions into ensemble models.

```{r, message = FALSE}
ensemble_models(run_info = run_info)
```

## Final Models

The last step is to create the final simple model averages and select the best models.

```{r, message = FALSE}
final_models(run_info = run_info)
```

## Get Forecast Results

Finally we can now retrieve the forecast results from this Finn run.

```{r, message = FALSE}
finn_output_tbl <- get_forecast_data(run_info = run_info)

print(finn_output_tbl)
```
