-
Notifications
You must be signed in to change notification settings - Fork 8
/
03_shiny_hierarchical_forecaster.Rmd
226 lines (171 loc) · 5 KB
/
03_shiny_hierarchical_forecaster.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: "Hierarchical Forecaster"
output:
flexdashboard::flex_dashboard:
orientation: rows
theme:
bg: "#FFFFFF"
fg: "#2c3e50"
primary: "#2c3e50"
base_font: !expr bslib::font_google("Oswald")
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
# IMPORTANT NOTE!!!
# INSTALL DEVELOPMENT VERSION OF RMARKDOWN AND CROSSTALK
# rmarkdown * 2.6.6 2021-02-02 [1] Github (rstudio/rmarkdown@d8e7a15)
# Run These:
# remotes::install_github("rstudio/rmarkdown")
# remotes::install_github("rstudio/crosstalk")
library(shiny)
library(collapsibleTree)
library(plotly)
library(DT)
library(modeltime)
library(modeltime.ensemble)
library(tidymodels)
library(tidyverse)
library(timetk)
```
```{r}
hierarchy_tbl <- read_rds("m5-forecasting-accuracy/sales_sample_tbl.rds") %>%
select(contains("id"))
full_data_tbl <- read_rds("m5-forecasting-accuracy/full_data_tbl.rds")
test_forecast_ensemble_tbl <- read_rds("m5-forecasting-accuracy/test_forecast_ensemble_tbl.rds")
future_forecast_ensemble_tbl <- read_rds("m5-forecasting-accuracy/future_forecast_ensemble_tbl.rds")
purchases_summarized_tbl <- full_data_tbl %>%
select(category, identifier, value) %>%
group_by(category, identifier) %>%
summarise(value = sum(value, na.rm = T)) %>%
ungroup() %>%
filter(category == 'item_id') %>%
left_join(hierarchy_tbl, by = c("identifier" = "item_id")) %>%
select(-id)
indicator_options <- full_data_tbl %>%
distinct(category, identifier) %>%
mutate(category = factor(
category,
levels = c("all_stores_id", "state_id", "store_id",
"cat_id", "dept_id", "item_id"))
) %>%
arrange(category, identifier) %>%
pull(identifier)
```
# Product Exploration
## Column {.sidebar}
#### What it does
Forecast any level within a __Product Hierarchy__.
<hr>
#### How it works
```{r}
shiny::selectInput(
"indicator",
label = "Select a Hierarchy to Forecast",
choices = indicator_options,
selectize = TRUE
)
shiny::radioButtons(
inputId = "lookback",
label = "Select a Look Back (Time Zoom)",
choices = c("3 months", "6 months", "12 months", "2 years"),
selected = "2 years",
inline = FALSE
)
```
<hr>
Created in __Learning Lab 50: Hierarchical Forecasting.__
Learn more at [Business Science](https://www.business-science.io/)
## Column 1
### Product Hierarchy
```{r}
renderCollapsibleTree({
purchases_summarized_tbl %>%
# filter(identifier %in% input$indicator) %>%
collapsibleTree(
hierarchy = c("state_id", "store_id", "cat_id", "dept_id", "identifier"),
attribute = "value",
root = "All Stores",
aggFun = sum,
nodeSize = "value",
tooltip = TRUE,
fontSize = 16
)
})
```
## Row {.tabset .tabset-fade}
### Predicted Forecast (Next 28-Days)
```{r}
renderPlotly({
future_forecast_ensemble_tbl %>%
# FILTERS
filter(identifier %in% input$indicator) %>%
group_by(identifier) %>%
# Focus on end of series
filter_by_time(
.start_date = last(date) %-time% input$lookback,
.end_date = "end"
) %>%
plot_modeltime_forecast(
.facet_ncol = 2,
.conf_interval_show = TRUE,
.interactive = TRUE
)
})
```
### Test Forecast
```{r}
renderPlotly({
test_forecast_ensemble_tbl %>%
# FILTERS
filter(identifier %in% input$indicator) %>%
group_by(identifier) %>%
# Focus on end of series
filter_by_time(
.start_date = last(date) %-time% input$lookback,
.end_date = "end"
) %>%
plot_modeltime_forecast(
.facet_ncol = 2,
.conf_interval_show = TRUE,
.interactive = TRUE
)
})
```
### Test Accuracy
```{r}
accuracy_by_identifier_tbl <- test_forecast_ensemble_tbl %>%
select(category, identifier, .model_desc, .index, .value) %>%
pivot_wider(
names_from = .model_desc,
values_from = .value
) %>%
drop_na() %>%
rename(PREDICTION = 5) %>%
group_by(category, identifier) %>%
summarize_accuracy_metrics(
truth = ACTUAL,
estimate = PREDICTION,
metric_set = default_forecast_accuracy_metric_set()
)
renderDataTable({
df <- accuracy_by_identifier_tbl %>%
# FILTERS
filter(identifier %in% input$indicator) %>%
mutate(across(c(mae, mape, rmse, smape), ~ round(., 1))) %>%
mutate(across(c(mase, rsq), ~ round(., 3)))
datatable(
data = df,
options = list(
scrollX = TRUE,
scrollY = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel')
)
)
})
```