-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.R
266 lines (245 loc) · 10.2 KB
/
functions.R
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Translate product name to product id
translate_input <- function(input, data_to_arima) {
data_to_arima %>%
filter(product_name %in% input) %>%
pull(product) %>%
head(1)
}
# Make forecasts for different prices
get_forecasts <- function(chosen_product, data_to_arima, models){
# Translate product name to product id
chosen_product <- translate_input(chosen_product, data_to_arima)
# Make price data for forecasts
new_data <- data_to_arima %>%
filter(product == chosen_product) %>%
# Get the last observation
slice(n()) %>%
# One month ahead
mutate(yearmonth = yearmonth + 1,
original_price = price_mean,
# Price can vary +- 30 percent
price_mean = list(c(seq(price_mean,
price_mean * 1.3,
0.01),
seq(price_mean - 0.01,
price_mean * 0.7,
-0.01)))) %>%
unnest(price_mean) %>%
# Filter out negative prices just in case
filter(price_mean > 0) %>%
ungroup()
# Get the corresponding model
model_to_use <- models %>%
filter(product == new_data %>%
slice(1) %>%
pull(product))
# Make forecasts for different prices
forecasts <- future_map(1:nrow(new_data),
~model_to_use %>%
forecast(new_data %>%
slice(.x) %>%
as_tsibble(key = product,
index = yearmonth)) %>%
as_tibble()) %>%
reduce(bind_rows) %>%
group_by(product) %>%
select(product,
yearmonth,
quantity_sum,
new_price = price_mean,
pred_quantity = .mean,
original_price) %>%
# Join original sales quantities
inner_join(new_data %>%
group_by(product) %>%
slice(n()) %>%
select(product,
yearmonth,
original_quantity = quantity_sum)) %>%
# Calculate revenues
mutate(original_revenue = original_quantity * original_price,
pred_revenue = pred_quantity * new_price) %>%
# Filter out negative quantities just in case
filter(pred_quantity >= 0) %>%
# Select those with highest predicted quantities
arrange(-pred_revenue)
# Join normal forecast without optimized price
models %>%
filter(product == new_data %>%
slice(1) %>%
pull(product)) %>%
forecast(data_to_arima %>%
filter(product == chosen_product) %>%
# Get the last observation
slice(n()) %>%
# One month ahead
mutate(yearmonth = yearmonth + 1)) %>%
# Non-negative
mutate(pred_quantity_normal = ifelse(.mean < 0, 0, .mean)) %>%
as_tsibble() %>%
select(product, yearmonth, pred_quantity_normal) %>%
as_tibble() %>%
inner_join(forecasts) %>%
# Predicted normal revenue without price optimization
mutate(pred_revenue_normal = pred_quantity_normal * original_price)
}
# Obtain optimal prices to maximize sales
get_optimal_forecast <- function(forecasts){
# Get optimal prices by maximizing revenue
forecasts %>%
slice(1) %>%
as_tsibble(key = "product")
}
# Plots revenue vs time
plot_revenue_forecasts <- function(optimal_forecast,
chosen_product,
data_to_arima,
plot_font_size){
# Keep current product_name for later naming
product_name <- chosen_product
# Translate product_name to product_id
chosen_product <- translate_input(chosen_product, data_to_arima)
prod
optimal_forecast %>%
autoplot(pred_revenue) +
geom_point(color = "#DAD4D4") +
autolayer(optimal_forecast, pred_revenue_normal) +
geom_point(aes(y = pred_revenue_normal), color = "#369093") +
geom_segment(aes(xend = yearmonth,
y = pred_revenue_normal,
yend = pred_revenue),
color = "#DAD4D4") +
autolayer(data_to_arima %>%
filter(product == chosen_product),
revenue,
color = "#369093") +
ggtitle("Expected revenue",
subtitle = product_name) +
xlab(NULL) +
ylab("Revenue") +
theme_minimal() +
theme(text = element_text(colour = "#DAD4D4"),
panel.grid = element_line(colour = "#423C3C"),
panel.background = element_rect(fill = "#2D3741"),
axis.text = element_text(colour = "#BCB1B1",
size = plot_font_size,
angle = 45,
hjust = 1),
plot.background = element_rect(fill = "#2D3741",
color = "transparent"),
legend.position = "none",
legend.key.width = unit(2, "cm"),
legend.box.margin = margin(t = 13),
legend.background = element_rect(fill = "#2D3741"),
legend.text = element_text(size = plot_font_size),
legend.title = element_text(size = plot_font_size),
plot.title = element_text(size = plot_font_size),
axis.title = element_text(size = plot_font_size),
axis.ticks = element_line(colour = "#BCB1B1"),
panel.border = element_rect(fill = "transparent",
colour = "#BCB1B1"),
strip.text = element_text(colour = "#DAD4D4"),
plot.subtitle = element_text(size = plot_font_size - 5)
)
}
# Plots quantity vs time
plot_quantity_forecasts <- function(optimal_price_tibble,
chosen_product,
data_to_arima,
plot_font_size){
# Keep current product_name for later naming
product_name <- chosen_product
# Translate product_name to product_id
chosen_product <- translate_input(chosen_product, data_to_arima)
optimal_price_tibble %>%
autoplot(pred_quantity) +
geom_point(color = "#DAD4D4") +
autolayer(optimal_price_tibble, pred_quantity_normal) +
geom_point(aes(y = pred_quantity_normal), color = "#369093") +
geom_segment(aes(xend = yearmonth,
y = pred_quantity_normal,
yend = pred_quantity),
color = "#DAD4D4") +
autolayer(data_to_arima %>%
filter(product == chosen_product), color = "#369093") +
ggtitle("Expected sales quantity",
subtitle = product_name) +
xlab(NULL) +
ylab("Quantity") +
theme_minimal() +
theme(legend.position = "none",
text = element_text(colour = "#DAD4D4"),
axis.text = element_text(colour = "#BCB1B1",
size = plot_font_size,
angle = 45,
hjust = 1),
axis.ticks = element_line(colour = "#BCB1B1"),
axis.title = element_text(size = plot_font_size),
plot.title = element_text(size = plot_font_size),
plot.background = element_rect(fill = "#2D3741", color = "transparent"),
panel.grid = element_line(colour = "#423C3C"),
panel.border = element_rect(fill = "transparent", colour = "#BCB1B1"),
panel.background = element_rect(fill = "#2D3741"),
strip.text = element_text(colour = "#DAD4D4"),
plot.subtitle = element_text(size = plot_font_size - 5)
)
}
# Plots the revenue effect due to price optimization
plot_revenue_price <- function(all_prices,
chosen_product,
data_to_arima,
plot_font_size){
# Keep current product_name for later naming
product_name <- chosen_product
# Translate product_name to product_id
chosen_product <- translate_input(chosen_product, data_to_arima)
all_prices %>%
select(-yearmonth) %>%
as.data.frame() %>%
ggplot() +
geom_line(aes(y = pred_revenue, x = new_price), color = "#369093") +
geom_point(aes(y = pred_revenue_normal, x = original_price), color = "#369093") +
geom_point(data = . %>%
slice(1) %>%
as.data.frame(),
aes(y = pred_revenue, x = new_price), color = "#DAD4D4") +
ggtitle("Expected price vs revenue",
subtitle = product_name) +
xlab("Product price") +
ylab("Revenue") +
theme_minimal() +
theme(text = element_text(colour = "#DAD4D4"),
panel.grid = element_line(colour = "#423C3C"),
panel.background = element_rect(fill = "#2D3741"),
axis.text = element_text(colour = "#BCB1B1",
size = plot_font_size,
angle = 45),
plot.background = element_rect(fill = "#2D3741",
color = "transparent"),
legend.position = "none",
legend.background = element_rect(fill = "#2D3741"),
legend.text = element_text(size = plot_font_size),
legend.title = element_text(size = plot_font_size),
plot.title = element_text(size = plot_font_size),
axis.title = element_text(size = plot_font_size),
axis.ticks = element_line(colour = "#BCB1B1"),
panel.border = element_rect(fill = "transparent",
colour = "#BCB1B1"),
strip.text = element_text(colour = "#DAD4D4"),
plot.subtitle = element_text(size = plot_font_size - 5)
)
}
# Segment rules for RFM
create_segments <- function(rfm_result) {
# segment boundaries
recency_lower <- c(4, 2, 3, 4, 3, 2, 2, 1, 1, 1)
recency_upper <- c(5, 5, 5, 5, 4, 3, 3, 2, 1, 2)
frequency_lower <- c(4, 3, 1, 1, 1, 2, 1, 2, 4, 1)
frequency_upper <- c(5, 5, 3, 1, 1, 3, 2, 5, 5, 2)
monetary_lower <- c(4, 3, 1, 1, 1, 2, 1, 2, 4, 1)
monetary_upper <- c(5, 5, 3, 1, 1, 3, 2, 5, 5, 2)
rfm_segment(rfm_result, segment_names,
recency_lower, recency_upper,
frequency_lower, frequency_upper,
monetary_lower, monetary_upper)
}