knitr::opts_chunk$set(warning = FALSE, 
                      message = FALSE)

Libraries

library(sknifedatar)
library(recipes)
library(modeltime)
library(workflowsets)
library(workflows)
library(parsnip)
library(timetk)

Data

data <- sknifedatar::data_avellaneda %>% mutate(date=as.Date(date)) %>% 
  filter(date<='2020-01-01' & date>='2017-01-01')

Recipes

# Preprocessors -----------------------------------------------------------
recipe_date <- recipe(value ~ ., data = data) %>% 
 step_date(date, features = c('dow','doy','week','month','year')) 

recipe_date_fourier <- recipe_date %>% step_fourier(date, period = 365, K=1)

Models

# Models ------------------------------------------------------------------
mars <- mars(mode = 'regression') %>% set_engine('earth')

prophet_boost <- prophet_boost(mode = 'regression') %>% set_engine("prophet_xgboost")

Workflowsets

wfsets <- workflow_set(
 preproc = list(base = recipe_date,
                fourier = recipe_date_fourier),
 models  = list(M_mars = mars, 
                M_prophet_boost = prophet_boost),
 cross   = TRUE)

wfsets 

Fitted a Workflowset into a time serie

# Workflowsets fit --------------------------------------------------------
wffits <- modeltime_wfs_fit(.wfsets = wfsets, 
                           .split_prop = 0.8, 
                           .serie=data)
wffits

Heatmap models/recipes

modeltime_wfs_heatmap(wffits, 'rsq')

Visual Forecast in test

modeltime_wfs_forecast(.wfs_results=wffits, 
                       .series = data,
                       .split_prop = 0.8) %>% 
  plot_modeltime_forecast(.line_size=0.3, .interactive=FALSE)

Ranking workflows

ranking <- modeltime_wfs_rank(wffits, 'rsq')
ranking

Select a top of workflows

# Select best model -------------------------------------------------------
wfbests <- modeltime_wfs_bestmodel(.wfs_results = wffits,
                                   .metric='rsq',
                                   .model = "top 2", 
                                   .minimize = FALSE)
wfbests

Refit in all data

# Refit -------------------------------------------------------------------
wfrefit <- modeltime_wfs_refit(.wfs_results = wfbests, data)

Forecast to the future

# Future forecast ---------------------------------------------------------
modeltime_wfs_forecast(.wfs_results=wfrefit, 
                                    .series = data,
                                    .h='4 months',
                                    .split_prop = 0.8)  %>% 
  plot_modeltime_forecast(.line_size=0.3, .interactive=FALSE)


rafzamb/sknifedatar documentation built on July 22, 2021, 4:10 p.m.