knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(sknifedatar) library(recipes) library(modeltime) library(workflowsets) library(workflows) library(parsnip) library(timetk)
data <- sknifedatar::data_avellaneda %>% mutate(date=as.Date(date)) %>% filter(date<='2020-01-01' & date>='2017-01-01')
# 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 ------------------------------------------------------------------ mars <- mars(mode = 'regression') %>% set_engine('earth') prophet_boost <- prophet_boost(mode = 'regression') %>% set_engine("prophet_xgboost")
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
# Workflowsets fit -------------------------------------------------------- wffits <- modeltime_wfs_fit(.wfsets = wfsets, .split_prop = 0.8, .serie=data) wffits
modeltime_wfs_heatmap(wffits, 'rsq')
modeltime_wfs_forecast(.wfs_results=wffits, .series = data, .split_prop = 0.8) %>% plot_modeltime_forecast(.line_size=0.3, .interactive=FALSE)
ranking <- modeltime_wfs_rank(wffits, 'rsq') ranking
# Select best model ------------------------------------------------------- wfbests <- modeltime_wfs_bestmodel(.wfs_results = wffits, .metric='rsq', .model = "top 2", .minimize = FALSE) wfbests
# Refit ------------------------------------------------------------------- wfrefit <- modeltime_wfs_refit(.wfs_results = wfbests, data)
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.