-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinitial_arules_models.Rmd
796 lines (584 loc) · 19.5 KB
/
initial_arules_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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
---
title: "Using Association Rules 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(rlang)
library(stringr)
library(glue)
library(purrr)
library(furrr)
library(arules)
library(arulesViz)
library(DT)
library(tidygraph)
source("lib_utils.R")
conflict_lst <- resolve_conflicts(
c("magrittr", "rlang", "dplyr", "readr", "purrr", "ggplot2", "arules",
"Matrix", "DT")
)
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,
price > 0,
exclude == FALSE
) %>%
select(
invoice_id, stock_code, customer_id, quantity, price, stock_value,
description
)
tnx_purchase_tbl %>% glimpse()
```
We now write this data out as a CSV so `arules` can read it in and process it.
```{r read_transactions, echo=TRUE}
tnx_purchase_tbl %>% write_csv("data/tnx_purchase_tbl.csv")
```
We also want to load the free-text description of the various stock items as
this will help will interpretation of the various rules we have found.
```{r load_stock_code_descriptions, echo=TRUE}
stock_description_tbl <- read_rds("data/stock_description_tbl.rds")
stock_description_tbl %>% glimpse()
```
# Basket Analysis with Association Rules
We now want to do some basic basket analysis using association rules, which
tries to determine which items get bought together, similar to taking a graph
approachin many ways.
```{r setup_arules_structures, echo=TRUE}
basket_arules <- read.transactions(
file = "data/tnx_purchase_tbl.csv",
format = "single",
sep = ",",
header = TRUE,
cols = c("invoice_id", "stock_code")
)
basket_arules %>% glimpse()
```
Now that we have this data we can look at some basic plots much like we
produced before. For example, we can look at the relative frequency of the
different items.
```{r plot_arules_item_frequency, echo=TRUE}
itemFrequencyPlot(basket_arules, topN = 20)
itemFrequencyPlot(basket_arules, topN = 20, type = "absolute")
```
The stock codes do not mean a huge amount to us, so we also want to look at
the description field for these items.
```{r show_frequency_items, echo=TRUE}
freq_codes <- itemFrequency(basket_arules) %>%
sort(decreasing = TRUE) %>%
head(20) %>%
names()
stock_description_tbl %>%
filter(stock_code %in% freq_codes) %>%
arrange(stock_code) %>%
datatable()
```
## Basic Concepts
The basic ideas of association rule mining and basket analysis draws on basic
ideas from probability theory.
We speak in terms of the *itemset*: that is, a collection of one or more items
that co-occur in a transaction.
For example, suppose we have a list of transactions as follows:
| ID | Items |
|----|---------------------|
| 1 | milk, bread |
| 2 | bread, butter |
| 3 | beer |
| 4 | milk, bread, butter |
| 5 | bread, butter |
Using the above set of transactions, and itemset may be "milk" or
"bread, butter".
The support of an itemset $X$, $\text{Supp}(X)$, is defined as the proportion of
transactions in the dataset which contain the itemset.
In the above example:
$$
\text{Supp}(\text{\{milk, bread\}}) = \frac{2}{5} = 0.40.
$$
A *rule*, $X \Rightarrow Y$, between two itemsets $X$ and $Y$ is a directed
relationship of the itemset $X$ showing the presence of $Y$. The rule is not
symmetric: $X \Rightarrow Y$ and $Y \Rightarrow X$ are not the same.
The *confidence* for the rule $X \implies Y$, $\text{Conf}(X \Rightarrow Y)$ is
defined by
$$
\text{Conf}(X \Rightarrow Y) = \frac{\text{Supp}(X \cup Y)}{\text{Supp}(X)}.
$$
So, to calculate the confidence for a rule:
$$
\text{Conf}(\text{\{milk, bread\}} \Rightarrow \text{\{butter\}}) = \frac{0.2}{0.4} = 0.5.
$$
To illustrate how rules are not symmetric:
$$
\text{Conf}(\text{\{butter\}} \Rightarrow \text{\{milk, bread\}}) = \frac{0.2}{0.6} = 0.33.
$$
Finally, we want a measure of the strength of the relationship between the
itemsets $X$ and $Y$. That is, measuring the effect of the presence of $X$ on
the presence of $Y$. We measure this by defining the *lift* of a rule as
$$
\text{Lift}(X \Rightarrow Y) = \frac{\text{Supp}(X \cup Y)}{\text{Supp}(X) \text{Supp}(Y)}.
$$
Again, we repeat our calculations for our rule.
$$
\text{Lift}(\text{\{bread, milk\}} \Rightarrow \text{\{butter\}}) = \frac{0.2}{(0.4)(0.6)} = \frac{0.2}{0.24} = 0.8333
$$
Lift values greater than 1 implies the presence of $X$ increases the
probability of $Y$ being present when compared to the unconditional
probability.
Now that we have these metrics and concepts, we can turn our attention to
trying to find rules in a given dataset, using these metrics to rank them.
Rather than using brute-force approaches to discovering these rules, we use a
number of different algorithms to find associations within the dataset.
The two main algorithms for discovering some rules are the `apriori` and the
`eclat` algorithms.
## Construct apriori Rules
We now want to construct the association rules using the `apriori` algorithm.
To do this, we need to set parameters such as the minimum support and the
minimum confidence level.
This gives us a set of association rules, along with the support and lift.
```{r construct_apriori_rules, echo=TRUE}
basket_apriori <- apriori(
basket_arules,
parameter = list(supp = 0.005, conf = 0.8)
)
basket_apriori_tbl <- basket_apriori %>%
as("data.frame") %>%
as_tibble() %>%
arrange(desc(lift))
basket_apriori_tbl %>% glimpse()
```
We now want to inspect this table using the `ruleExplorer()`
```{r inspect_apriori_rules, echo=TRUE, eval=FALSE}
basket_apriori %>% ruleExplorer()
```
To help visualise these rules, we can produce a basic scatterplot of the
metrics.
```{r plot_arules_metrics, echo=TRUE}
ggplot(basket_apriori_tbl) +
geom_point(aes(x = confidence, y = lift), alpha = 0.2) +
xlab("Rule Confidence") +
ylab("Rule Lift") +
ggtitle("Scatterplot of Association Rule Metrics")
```
## Construct eclat Rules
An alternative method of constructing association rules is to use the `eclat`
algorithm. The code for doing this is slightly different, but gives us similar
outputs.
```{r construct_eclat_rules, echo=TRUE}
basket_eclat <- eclat(
basket_arules,
parameter = list(support = 0.005)
) %>%
ruleInduction(
basket_arules,
confidence = 0.8
)
basket_eclat_tbl <- basket_eclat %>%
as("data.frame") %>%
as_tibble() %>%
arrange(desc(lift))
basket_eclat_tbl %>% glimpse()
```
Once again, we inspect the data using `ruleExplorer()`
```{r inspect_eclat_rules, echo=TRUE, eval=FALSE}
basket_eclat %>% ruleExplorer()
```
## Compare Algorithms
We now want to compare the outputs of both algorithms in terms of association
rules and how they compare.
```{r compare_arules_algorithms, echo=TRUE}
basket_ap_tbl <- basket_apriori_tbl %>%
select(rules, support, confidence, lift)
basket_ec_tbl <- basket_eclat_tbl %>%
select(rules, support, confidence, lift)
rules_comparison_tbl <- basket_ap_tbl %>%
full_join(basket_ec_tbl, by = "rules", suffix = c("_a", "_e"))
rules_comparison_tbl %>% glimpse()
```
## Reducing Minimum Confidence
While high confidence rules are useful, they are more likely to find rules that
are "obvious" as the probabilities are such that co-occuring basket items will
be noticed as being together - or possibly be natural complements: butter, milk
and bread is a good example.
Instead, we are also interested in less obvious rules, and so we reduce our
confidence threshold and see how many additional rules are discovered.
```{r determine_lower_confidence_rules, echo=TRUE}
basket_lower_rules <- apriori(
basket_arules,
parameter = list(supp = 0.005, conf = 0.4)
)
basket_lower_rules_tbl <- basket_lower_rules %>%
as("data.frame") %>%
as_tibble() %>%
arrange(desc(lift))
```
```{r plot_lower_arules_metrics, echo=TRUE}
ggplot(basket_lower_rules_tbl) +
geom_point(aes(x = confidence, y = lift), alpha = 0.2) +
xlab("Rule Confidence") +
ylab("Rule Lift") +
ggtitle("Scatterplot of Association Rule Metrics")
```
# Converting Rules to Graphs
We also have the ability to convert these rules to a graph representation,
where each node is either a `stock_code` or a rule, with the edges of the
graph representing that item being contained in the rule.
```{r convert_rules_to_graph, echo=TRUE}
apriori_rules_igraph <- basket_apriori %>%
plot(
measure = "support",
method = "graph",
engine = "igraph",
control = list(max = 1000)
)
apriori_rules_igraph %>% print()
```
We should first visualise this graph, using the top 100 rules in the dataset,
as measured by the support of the rule.
```{r plot_interactive_rules_graph, echo=TRUE}
basket_apriori %>%
head(n = 100, by = "support") %>%
plot(
measure = "lift",
method = "graph",
engine = "htmlwidget"
)
```
## Extract Connected Product Labels
First we want to look at the different disjoint components of the graph, and
label them with an ID.
```{r create_component_labels, echo=TRUE}
apriori_rules_tblgraph <- apriori_rules_igraph %>%
igraph::as.undirected(mode = "collapse") %>%
as_tbl_graph() %>%
mutate(
component_id = group_components()
) %>%
group_by(component_id) %>%
mutate(
component_size = n()
) %>%
ungroup()
```
We then want to create groups of common products that form a disjoint cluster
within this graph.
```{r combine_connected_products, echo=TRUE}
product_groups_all_tbl <- apriori_rules_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(component_id) %>%
mutate(
product_count = n()
) %>%
ungroup() %>%
select(
product_group_id = component_id,
product_count,
stock_code = label
) %>%
arrange(product_group_id, stock_code)
product_groups_all_tbl %>% glimpse()
```
For display purposes, we can show all the `stock_id` values in a list.
```{r display_product_groups, echo=FALSE}
product_groups_all_tbl %>%
group_by(product_group_id) %>%
summarise(
.groups = "drop",
product_count = n(),
product_str = str_c(stock_code, collapse = ":")
) %>%
ungroup() %>%
select(-product_group_id) %>%
datatable()
```
### Cluster Larger Groups
Within the large disjoint cluster there are a large number of products so
rather than treating this as a single group we instead may investigate using
further graph clustering algorithms to create further groupings.
```{r create_large_component_clusters, echo=TRUE}
apriori_rules_large_tblgraph <- apriori_rules_tblgraph %>%
convert(to_subgraph, component_size == max(component_size)) %>%
morph(to_undirected) %>%
mutate(
sub_id = group_louvain()
) %>%
unmorph()
```
Now that we have sub-divided this large subgraph, we repeat the process.
```{r create_reduced_product_list, echo=TRUE}
product_groups_largest_tbl <- apriori_rules_large_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(sub_id) %>%
mutate(
product_count = n()
) %>%
ungroup() %>%
select(
product_group_id = sub_id, product_count, stock_code = label
) %>%
arrange(product_group_id, stock_code)
product_groups_largest_tbl %>% glimpse()
```
```{r display_largest_product_groups, echo=FALSE}
product_groups_largest_tbl %>%
group_by(product_group_id) %>%
summarise(
.groups = "drop",
product_count = n(),
product_str = str_c(stock_code, collapse = ":")
) %>%
ungroup() %>%
select(-product_group_id) %>%
datatable()
```
Finally, it is worth trying to use an interactive tool to investigate this
subgraph, we we can use `visNetwork()` to produce an interactive JS tool
```{r show_interactive_networks, echo=TRUE, eval=FALSE}
apriori_rules_large_tblgraph %>%
toVisNetworkData(idToLabel = FALSE) %>%
visNetwork(
nodes = .$nodes %>% transmute(id, label, group = sub_id),
edges = .$edges
)
```
## Evaluating Product Groups
How do we go about assessing the validity of these product groups?
Note that this work is exploratory - in effect this is more sophisticated
data exploration. Rather than use this model to make predictions - a job we
will need to do at some point, we instead just want to assess how novel these
grouping are.
To that end, it may be useful to check the co-occurrence of these products as a
group - for each purchase made by a customer, what proportion of the group was
featured in this data?
This question is worth exploring, so we should write some code to assess this.
Before we do this, we combine our two lists of product groups into a single
table.
```{r construct_combined_data_groups, echo=TRUE}
product_groups_tbl <- list(
ALL = product_groups_all_tbl,
LRG = product_groups_largest_tbl
) %>%
bind_rows(.id = "type") %>%
mutate(
group_label = sprintf("%s_%02d", type, product_group_id)
) %>%
group_by(group_label) %>%
mutate(
group_size = n()
) %>%
ungroup() %>%
select(group_label, group_size, stock_code)
product_groups_tbl %>% glimpse()
```
```{r construct_data_groups, echo=TRUE, cache=TRUE}
tnx_groups_tbl <- tnx_data_tbl %>%
select(invoice_id, invoice_date, stock_code) %>%
group_nest(invoice_id, .key = "invoice_data")
group_props_tbl <- product_groups_tbl %>%
group_nest(group_label, group_size, .key = "stock_data") %>%
filter(group_size > 1, group_size < 15) %>%
expand_grid(tnx_groups_tbl) %>%
mutate(
comb_data = future_map2(
invoice_data, stock_data,
inner_join,
by = "stock_code",
.options = furrr_options(globals = FALSE)
),
match_count = map_int(comb_data, nrow),
group_prop = match_count / group_size
) %>%
select(group_label, group_size, group_prop) %>%
filter(group_prop > 0)
group_props_tbl %>% glimpse()
```
We now create a histogram of the proportions for each group, and this gives us
a gauge of the 'novelty' of each of these groups.
```{r plot_product_group_proportions, echo=TRUE}
plot_tbl <- group_props_tbl %>%
mutate(label = glue("{group_label} ({group_size})"))
ggplot(plot_tbl) +
geom_histogram(aes(x = group_prop), binwidth = 0.1) +
facet_wrap(vars(label), scales = "free_y") +
scale_y_continuous(labels = label_comma()) +
xlab("Proportion") +
ylab("Purchase Count") +
ggtitle("Facetted Histograms of Group Coverages by Product Grouping") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
```
### Investigate Groups
Now that we have our groups we add on the description fields so that
interpretation of the different groupings is easier.
```{r display_stock_groups, echo=TRUE}
product_groups_tbl %>%
filter(group_size > 1, group_size < 15) %>%
mutate(stock_code = stock_code %>% str_trim() %>% str_to_upper()) %>%
left_join(stock_description_tbl, by = "stock_code") %>%
datatable()
```
# Investigate Lower Support Rules
Our previous analysis was all based on rules with a minimum confidence of 0.80
so we now want to repeat our analysis but on this new set of rules.
```{r construct_lower_rules_graph, echo=TRUE}
apriori_lower_rules_igraph <- basket_lower_rules %>%
plot(
measure = "support",
method = "graph",
engine = "igraph",
control = list(max = 5000)
)
apriori_lower_rules_igraph %>% print()
```
Once again we have a quick look at the top 50 rules.
```{r visual_inspection_rules_igraph, echo=TRUE}
basket_lower_rules %>%
head(n = 50, by = "support") %>%
plot(
measure = "lift",
method = "graph",
engine = "htmlwidget"
)
```
## Determine Distinct Rules Subgraphs
Having converted the association rules to the graph, we then look at the
distinct components of this graph and use these as our first pass at these
clusters.
```{r create_lower_component_labels, echo=TRUE}
apriori_lower_rules_tblgraph <- apriori_lower_rules_igraph %>%
igraph::as.undirected(mode = "collapse") %>%
as_tbl_graph() %>%
mutate(
component_id = group_components()
) %>%
group_by(component_id) %>%
mutate(
component_size = n()
) %>%
ungroup()
apriori_lower_rules_tblgraph %>% print()
```
```{r construct_product_groups_all, echo=TRUE}
product_groups_lower_all_tbl <- apriori_lower_rules_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(component_id) %>%
mutate(
product_count = n()
) %>%
ungroup() %>%
select(product_group_id = component_id, product_count, stock_code = label) %>%
arrange(product_group_id, stock_code)
product_groups_lower_all_tbl %>% glimpse()
```
```{r construct_largest_subgraph_groups, echo=TRUE}
apriori_lower_rules_bigcomp_tblgraph <- apriori_lower_rules_tblgraph %>%
convert(to_subgraph, component_size == max(component_size)) %>%
mutate(
sub_id = group_louvain()
)
apriori_lower_rules_bigcomp_tblgraph %>% print()
```
```{r convert_subgraph_groups_tbl}
product_groups_lower_bigcomp_tbl <- apriori_lower_rules_bigcomp_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(sub_id) %>%
mutate(
product_count = n()
) %>%
ungroup() %>%
select(product_group_id = sub_id, product_count, stock_code = label) %>%
arrange(product_group_id, stock_code)
product_groups_lower_bigcomp_tbl %>% glimpse()
```
```{r construct_combined_data_groups_lower, echo=TRUE}
product_groups_lower_tbl <- list(
ALL = product_groups_lower_all_tbl,
LRG = product_groups_lower_bigcomp_tbl
) %>%
bind_rows(.id = "type") %>%
mutate(
group_label = sprintf("%s_%02d", type, product_group_id)
) %>%
group_by(group_label) %>%
mutate(
group_size = n()
) %>%
ungroup() %>%
select(group_label, group_size, stock_code)
product_groups_lower_tbl %>% glimpse()
```
Now that we have our groups we add on the description fields so that
interpretation of the different groupings is easier.
```{r display_lower_stock_groups, echo=TRUE}
product_groups_lower_tbl %>%
filter(group_size > 1, group_size != max(group_size)) %>%
mutate(stock_code = stock_code %>% str_trim() %>% str_to_upper()) %>%
left_join(stock_description_tbl, by = "stock_code") %>%
datatable()
```
# Output Data to Disk
We now want to write the various data groups to disk.
As this may be useful for later analysis and for later modelling, we output
these groupings for later use.
```{r output_stock_groups, echo=TRUE}
product_groups_tbl %>% write_rds("data/product_groups_tbl.rds")
product_groups_lower_tbl %>% write_rds("data/product_groups_lower_tbl.rds")
```
# R Environment
```{r show_session_info, echo=TRUE, message=TRUE}
sessioninfo::session_info()
```