-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinitial_timeseries_models.Rmd
274 lines (205 loc) · 6.26 KB
/
initial_timeseries_models.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
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
267
268
269
270
271
272
273
274
---
title: "Time Series Analysis of the Online Retail Dataset"
author: "Mick Cooney <[email protected]>"
date: "Last updated: `r format(Sys.time(), '%B %d, %Y')`"
output:
rmdformats::readthedown:
toc_depth: 3
use_bookdown: TRUE
code_folding: hide
fig_caption: TRUE
html_document:
fig_caption: yes
theme: spacelab #sandstone #spacelab #flatly
highlight: pygments
number_sections: TRUE
toc: TRUE
toc_depth: 2
toc_float:
smooth_scroll: FALSE
pdf_document: default
---
```{r import_libraries, echo=FALSE, message=FALSE}
library(conflicted)
library(tidyverse)
library(scales)
library(cowplot)
library(magrittr)
library(glue)
library(stringr)
library(rlang)
library(purrr)
library(furrr)
library(DT)
library(tidygraph)
library(tidyquant)
source("lib_utils.R")
conflict_lst <- resolve_conflicts(
c("magrittr", "rlang", "dplyr", "readr", "purrr", "ggplot2", "DT",
"tidyquant")
)
knitr::opts_chunk$set(
tidy = FALSE,
cache = FALSE,
warning = FALSE,
message = FALSE,
fig.height = 8,
fig.width = 11
)
options(
width = 80L,
warn = 1,
mc.cores = parallel::detectCores()
)
theme_set(theme_cowplot())
set.seed(42)
plan(multisession)
```
# Load Data
We first want to load our datasets and prepare them for some simple association
rules mining.
```{r load_transaction_data, echo=TRUE}
tnx_data_tbl <- read_rds("data/retail_data_cleaned_tbl.rds")
tnx_data_tbl %>% glimpse()
```
To use our rules mining we just need the invoice data and the stock code, so
we can ignore the rest. Also, we ignore the issue of returns and just look at
purchases.
```{r prepare_data_arules, echo=TRUE}
tnx_purchase_tbl <- tnx_data_tbl %>%
filter(
quantity > 0,
exclude == FALSE
) %>%
select(
invoice_date, invoice_id, stock_code, description, customer_id,
quantity, price, stock_value
)
tnx_purchase_tbl %>% glimpse()
```
# Construct Basic Time-Series
There are a number of different perspective on this data - we can look at
customer-based time-series or individual items or just total revenue.
In all these cases we aggregate daily, weekly and monthly, so rather than
repeating our code over and over, we construct a function to do this.
```{r construct_aggregation_time_series, echo=TRUE}
construct_time_series_data <- function(data_tbl) {
daily_tbl <- data_tbl %>%
tq_transmute(
mutate_fun = apply.daily,
FUN = sum,
na.rm = TRUE,
col_rename = "amount"
)
weekly_tbl <- data_tbl %>%
tq_transmute(
mutate_fun = apply.weekly,
FUN = sum,
na.rm = TRUE,
col_rename = "amount"
)
monthly_tbl <- data_tbl %>%
tq_transmute(
mutate_fun = apply.monthly,
FUN = sum,
na.rm = TRUE,
col_rename = "amount"
)
agg_tbl <- list(
daily = daily_tbl,
weekly = weekly_tbl,
monthly = monthly_tbl
) %>%
bind_rows(.id = "period")
return(agg_tbl)
}
```
## Total Revenue
A revenue-based approach is the simplest, so we start there.
```{r construct_revenue_time_series, echo=TRUE}
use_data_tbl <- tnx_purchase_tbl %>%
select(date = invoice_date, stock_value)
ts_revenue_data_tbl <- use_data_tbl %>%
construct_time_series_data() %>%
mutate(
label = "total_revenue",
.after = "period"
)
ts_revenue_data_tbl %>% glimpse()
```
Now that we have constructed this simple time-series, we now want to plot them.
```{r plot_totalrevenue_time_series, echo=TRUE}
ggplot(ts_revenue_data_tbl) +
geom_line(aes(x = date, y = amount)) +
scale_y_continuous(labels = label_comma()) +
facet_wrap(vars(period), scales = "free_y", ncol = 2) +
xlab("Date") +
ylab("Amount") +
ggtitle("Time Series Plots of Total Revenue")
```
## Frequent Purchases
Time series plots of different item types may be useful, but only for items
that have regular transactions, as otherwise the data will be mostly zero
entries. Instead, we want to find items that are purchased on a regular basis
and inspect those.
```{r plot_histogram_day_count, echo=TRUE}
stock_daycount_tbl <- tnx_purchase_tbl %>%
select(invoice_date, stock_code) %>%
distinct() %>%
count(stock_code, name = "day_count")
ggplot(stock_daycount_tbl) +
geom_histogram(aes(x = day_count), bins = 50) +
xlab("Days in Transaction Log") +
ylab("Count of Items") +
ggtitle("Histogram of Date Appearances for Stock Items")
```
Rather than focus on all these items, we choose the arbitrary cut-off of the
top 12 items, and construct time series of the daily sales of each of these
items.
```{r construct_item_sales_time_series, echo=TRUE}
use_data_tbl <- tnx_purchase_tbl %>%
select(date = invoice_date, stock_code, stock_value)
use_stock_tbl <- stock_daycount_tbl %>%
arrange(desc(day_count)) %>%
slice_head(n = 12)
ts_itemsales_data_tbl <- use_data_tbl %>%
semi_join(use_stock_tbl, by = "stock_code") %>%
group_by(stock_code) %>%
construct_time_series_data() %>%
mutate(
label = "item_sales",
.after = "period"
) %>%
ungroup()
ts_itemsales_data_tbl %>% glimpse()
```
```{r plot_itemsales_time_series, echo=TRUE}
ggplot(ts_itemsales_data_tbl %>% filter(period == "daily")) +
geom_line(aes(x = date, y = amount)) +
scale_y_continuous(labels = label_comma()) +
facet_wrap(vars(stock_code), scales = "free_y") +
xlab("Date") +
ylab("Sales") +
ggtitle("Time Series Plots of Item Sales (daily)") +
theme(axis.text.x = element_text(angle = 30, vjust = 0.5))
ggplot(ts_itemsales_data_tbl %>% filter(period == "weekly")) +
geom_line(aes(x = date, y = amount)) +
scale_y_continuous(labels = label_comma()) +
facet_wrap(vars(stock_code), scales = "free_y") +
xlab("Date") +
ylab("Sales") +
ggtitle("Time Series Plots of Item Sales (weekly)") +
theme(axis.text.x = element_text(angle = 30, vjust = 0.5))
ggplot(ts_itemsales_data_tbl %>% filter(period == "monthly")) +
geom_line(aes(x = date, y = amount)) +
scale_y_continuous(labels = label_comma()) +
facet_wrap(vars(stock_code), scales = "free_y") +
xlab("Date") +
ylab("Sales") +
ggtitle("Time Series Plots of Item Sales (monthly)") +
theme(axis.text.x = element_text(angle = 30, vjust = 0.5))
```
# R Environment
```{r show_session_info, echo=TRUE, message=TRUE}
sessioninfo::session_info()
```