-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market_Basket_Analysis.Rmd
278 lines (219 loc) · 9.21 KB
/
Market_Basket_Analysis.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
275
276
277
278
---
title: ""
output:
html_document:
df_print: paged
code_folding: hide
toc: TRUE
---
```{r, include = FALSE}
library(readxl)
df <- read_excel(here::here("online_retail_II.xlsx"))
library(dplyr)
library(stringr)
df <- df %>%
filter(str_length(StockCode) == 5 |
str_detect(StockCode, "^\\d{5}[a-zA-Z]{1,2}$") |
str_detect(StockCode, "PADS|DCGS|SP|gift")) %>%
filter(Price != 0) %>%
mutate(CustomerID = as.character(`Customer ID`),
Country = na_if(Country, "Unspecified"), .keep = "unused", .after = Price)
```
Market Basket Analysis allows us to find groups of items that are frequently bought together, an useful information for a variety of reasons, both commercially and operationally.
<br>
# - *data preparation and application of the algorithm*
Given that its algorithmic implementation is very resource-hungry, we will here concentrate on non duplicated purchases made in `Belgium`, considering both confirmed and cancelled invoices,
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct()
```
having access then to `56` of them for `479` distinct stock codes.
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct() %>%
summarise("Number of Invoices" = n_distinct(Invoice),
"Number of Distinct Stock Codes" = n_distinct(StockCode))
```
<br>
In order to feed it into the algorithm, we need to change the data frame into a transaction format, where we have a row for every invoice and as many additional columns as the number of distinct stock codes. The values in the cell are logical values that specify on whether the invoice contains (`TRUE`) or doesn't contain (`FALSE`) that particular stock code.
```{r}
(dftr <- df %>%
filter(Country == "Belgium") %>%
distinct() %>%
count(Invoice, StockCode) %>%
mutate(n = TRUE) %>%
tidyr::pivot_wider(id_cols = Invoice, names_from = StockCode, values_from = n, values_fill = FALSE))
```
```{r, message = FALSE, results = "hide"}
library(arules)
dftr <- dftr %>%
select(-Invoice)
tr <- as(dftr, "transactions")
#we transform the data frame into a "transactions" class
rules <- apriori(tr, conf = 0, minlen = 2)
#conf = 0 to not filter out any rules, minlen = 2 to not have rules with one side empty
```
<br>
# - *rules' filtering*
After its application, the algorithm returns the following `21` "rules", that must be read as
> "if `StockCode1` is being bought, then (`=>`) there is a chance that `StockCode2` will be bought as well (in the same invoice)"
```{r}
tibble(lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)),
rules@quality) %>%
mutate(" " = "=>", .after = "lhs") %>%
rename(StockCode1 = lhs, StockCode2 = rhs)
```
<br>
This "chance" is expressed by a number of metrics (`support`, `confidence`, `coverage` and `lift`), plus we also have the number of times that particular association of stock codes exists, `count`. Notice how we have many one-to-one associations (`22551 => 22554` and then `22554 => 22551`) but for some of them the values in the metrics are different (like for `22630 => 22629` / `22629 => 22630` in `confidence` and `coverage`).
The metrics are what we use to filter the rules, and we will here explain their meanings, using the first one (`22551 => 22554`)
```{r}
tibble(lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)),
rules@quality) %>%
mutate(" " = "=>", .after = "lhs") %>%
rename(StockCode1 = lhs, StockCode2 = rhs) %>%
slice(1)
```
<br>
as an example:
- `support` is the percentage of times all stock codes of the rule occur together in the same invoice over the totality of invoices, so we get `0.1785714` which is `10` / `56`
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct() %>%
count(Invoice, StockCode) %>%
mutate(n = TRUE) %>%
tidyr::pivot_wider(id_cols = Invoice, names_from = StockCode, values_from = n) %>%
select(Invoice, `22551`, `22554`) %>%
filter(if_all(everything(), ~ !is.na(.x)))
```
<br>
- `confidence` is the percentage of times the association happens, that is with `StockCode1` in an invoice there is also `StockCode2`, and that happens for `10` invoices out of `12`, the `0.8333333`
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct() %>%
count(Invoice, StockCode) %>%
mutate(n = TRUE) %>%
tidyr::pivot_wider(id_cols = Invoice, names_from = StockCode, values_from = n, values_fill = FALSE) %>%
select(Invoice, `22551`, `22554`) %>%
filter(`22551`)
```
<br>
- `coverage` is the percentage of times `StockCode1` is present in the data set, so here its value is `0.2142857` (`12` out of `56`)
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct() %>%
count(Invoice, StockCode) %>%
mutate(n = TRUE) %>%
tidyr::pivot_wider(id_cols = Invoice, names_from = StockCode, values_from = n) %>%
select(Invoice, `22551`) %>%
filter(`22551`)
```
<br>
- `lift` is calculated by dividing the `confidence` of the rule by the percentage of times `StockCode2` occurs in the data set, so `0.8333333` / (`12` / `56`), that equals `3.888889`
```{r}
df %>%
filter(Country == "Belgium") %>%
distinct() %>%
count(Invoice, StockCode) %>%
mutate(n = TRUE) %>%
tidyr::pivot_wider(id_cols = Invoice, names_from = StockCode, values_from = n) %>%
select(Invoice, `22554`) %>%
filter(`22554`)
```
It measures the "strength" of the rule, so, higher the `lift`, higher the number of times `StockCode1` and `StockCode2` occur together in a non fortuitous way, meaning that the purchase of `StockCode1` really improves the chances of `StockCode2` being bought.
For this reasons, we want it to be higher than `1.5`, given that having it under `1` means that the two stock codes are substitutes of one another, while it equal to `1` means that the two stock codes are independently bought.
Given that all of our rules have a strong `lift`, the factors by which we filter are `confidence` (to have trustworthy rules)
```{r}
tibble(lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)),
rules@quality) %>%
mutate(" " = "=>", .after = "lhs") %>%
rename(StockCode1 = lhs, StockCode2 = rhs) %>%
arrange(desc(confidence))
```
and `coverage` (to have rules that happen frequently).
```{r}
tibble(lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)),
rules@quality) %>%
mutate(" " = "=>", .after = "lhs") %>%
rename(StockCode1 = lhs, StockCode2 = rhs) %>%
arrange(desc(coverage))
```
<br>
Cutoff values could be `0.75` for `confidence`, meaning that the rule is true `3` times out of `4`, and `0.20` for `coverage` meaning that `StockCode1` is present every `5` stock codes.
With these values, we preserve `3` rules,
```{r}
tibble(lhs = labels(lhs(rules)),
rhs = labels(rhs(rules)),
rules@quality) %>%
mutate(" " = "=>", .after = "lhs") %>%
rename(StockCode1 = lhs, StockCode2 = rhs) %>%
filter(confidence >= 0.75 &
coverage >= 0.2)
```
for `4` distinct stock codes:
```{r}
df %>%
filter(StockCode %in% c("22551", "22554", "22629", "22630")) %>%
distinct(StockCode, Description)
```
<br>
# - *analysis of the results*
The `4` stock codes have, in the Belgian subset, these characteristics,
```{r}
df %>%
filter(Country == "Belgium" &
StockCode %in% c("22551", "22554", "22629", "22630")) %>%
group_by(StockCode) %>%
summarise("Number of Invoices" = n_distinct(Invoice),
"Median Quantity" = median(abs(Quantity)),
"Median Price" = median(Price))
```
that we can compare to the rest of the inventory, where we can see that they are generally cheaper
```{r}
library(ggplot2)
df %>%
filter(Country == "Belgium") %>%
mutate(Status = if_else(StockCode %in% c("22551", "22554", "22629", "22630"), "Selected Rules' Stock Codes", "Non Selected Rules' Stock Codes")) %>%
ggplot(aes(Status, Price)) +
geom_boxplot() +
labs(x = NULL,
y = NULL,
title ="Distribution of the Price column, differentiating by Status")
```
and that they are bought in roughly the same quantities.
```{r}
df %>%
filter(Country == "Belgium") %>%
mutate(Status = if_else(StockCode %in% c("22551", "22554", "22629", "22630"), "Selected Rules' Stock Codes", "Non Selected Rules' Stock Codes")) %>%
ggplot(aes(Status, Quantity)) +
geom_boxplot() +
labs(x = NULL,
y = NULL,
title ="Distribution of the Quantity column, differentiating by Status")
```
<br>
Finally, let's compare the revenues they generate with the overall country revenues.
```{r}
df %>%
filter(Country == "Belgium" &
!str_starts(Invoice, "C")) %>%
summarise("Belgian Revenues" = sum(Quantity * Price)) %>%
bind_cols(df %>%
filter(Country == "Belgium" &
StockCode %in% c("22551", "22554", "22629", "22630") &
!str_starts(Invoice, "C")) %>%
summarise("Selected Rules' Revenues" = sum(Quantity * Price))) %>%
mutate("In Percentage" = formattable::percent(`Selected Rules' Revenues` / sum(`Selected Rules' Revenues`, `Belgian Revenues`)))
```
<br>
# - *main takeaways and further developments*
We here applied an association rules algorithm to a subset of our data, finding, after some filtering, `4` stock codes that, in pairs of `2`, are frequently bought together.
Different subsets can be examined afterwards or, with more processing power, we can run the same analysis on the whole data frame.