Created
April 7, 2022 20:21
-
-
Save mdancho84/bda92fb606ac169195290ffebc262122 to your computer and use it in GitHub Desktop.
Forecasting & Time Series Plotting at Scale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BUSINESS SCIENCE | |
# NEW TRELLISCOPE INTEGRATION ---- | |
# * AVAILABLE IN TIMETK 2.8.0 AND MODELTIME 1.2.0 | |
# LIBRARIES ---- | |
library(tidymodels) | |
library(modeltime) | |
library(tidyverse) | |
library(timetk) | |
# MULTI-TIME SERIES DATASET ---- | |
data_tbl <- walmart_sales_weekly %>% | |
select(id, Date, Weekly_Sales) %>% | |
set_names(c("id", "date", "value")) | |
# TRELLISCOPE ---- | |
data_tbl %>% | |
group_by(id) %>% | |
plot_time_series( | |
date, value, | |
.facet_ncol = 2, | |
.facet_nrow = 2, | |
.facet_strip_remove = TRUE, | |
.title = NULL, | |
.interactive = TRUE, | |
.trelliscope = TRUE | |
) | |
# TRAIN / TEST SPLIT ---- | |
splits <- data_tbl %>% | |
time_series_split( | |
assess = "3 months", | |
cumulative = TRUE | |
) | |
# PREPROCESS ---- | |
rec_obj <- recipe(value ~ ., training(splits)) %>% | |
step_mutate_at(id, fn = droplevels) %>% | |
step_timeseries_signature(date) %>% | |
step_rm(date) %>% | |
step_zv(all_predictors()) %>% | |
step_dummy(all_nominal_predictors(), one_hot = TRUE) | |
# MODEL ---- | |
wflw_xgb <- workflow() %>% | |
add_model( | |
boost_tree() %>% set_engine("xgboost") | |
) %>% | |
add_recipe(rec_obj) %>% | |
fit(training(splits)) | |
# ORGANIZE ---- | |
model_tbl <- modeltime_table( | |
wflw_xgb | |
) | |
# CALIBRATE ON TESTING SET ---- | |
calib_tbl <- model_tbl %>% | |
modeltime_calibrate( | |
new_data = testing(splits), | |
id = "id" | |
) | |
# VISUALIZE WITH NEW TRELLISCOPE ---- | |
calib_tbl %>% | |
modeltime_forecast( | |
new_data = testing(splits), | |
actual_data = data_tbl, | |
conf_by_id = TRUE | |
) %>% | |
group_by(id) %>% | |
plot_modeltime_forecast( | |
.facet_ncol = 2, | |
.facet_nrow = 2, | |
.facet_strip_remove = TRUE, | |
.legend_show = FALSE, | |
.title = NULL, | |
.interactive = TRUE, | |
.trelliscope = TRUE | |
) |
New Functionality.
I've just added a new parameter trelliscope_params
that exposes the facet_trelliscope()
parameters to all of the plotting functions.
Here's an example where I change the width of the trelliscopes.
library(timetk)
library(tidyverse)
walmart_sales_weekly %>%
select(id, Dept, Date, Weekly_Sales) %>%
set_names(c("id", "dept", "date", "value")) %>%
group_by(id, dept) %>%
plot_time_series(
date, value,
.trelliscope = TRUE,
.facet_ncol = 2,
.facet_nrow = 3,
.facet_strip_remove = TRUE,
.title = NULL,
# Access facet_trelliscope params
.trelliscope_params = list(
width = 1500
)
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should get 2 plots.
Plot 1: Time Series Trelliscope
Plot 2: Forecast Trelliscope