-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild_models.Rmd
1443 lines (1104 loc) · 37.9 KB
/
build_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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Building the Customer and Product Modelling"
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: 3
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(zoo)
library(arules)
library(arulesViz)
library(DT)
library(tidygraph)
library(rfm)
library(FactoMineR)
library(factoextra)
library(ggpubr)
library(tidytext)
library(ggwordcloud)
library(wordcloud2)
source("lib_utils.R")
conflict_lst <- resolve_conflicts(
c("magrittr", "rlang", "dplyr", "readr", "purrr", "ggplot2", "arules",
"Matrix", "DT", "zoo")
)
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.
## Load Transaction Data
```{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
) %>%
drop_na(customer_id) %>%
select(
invoice_id, invoice_date, stock_code, customer_id, quantity, price,
stock_value, description
)
tnx_purchase_tbl %>% glimpse()
```
To build the association rules, we need to load the transactions in the format
required for the `arules` package. We set a date for our dataset before which
we wish to train our data and use the remainder as our model validation.
```{r set_training_data_date, echo=TRUE}
training_data_date <- as.Date("2011-03-31")
```
We now combine all this data to construct our association rules.
```{r setup_arules_structure, echo=TRUE}
tnx_purchase_tbl %>%
filter(invoice_date <= training_data_date) %>%
select(invoice_id, stock_code) %>%
write_csv("data/tnx_arules_input.csv")
basket_tnxdata <- read.transactions(
file = "data/tnx_arules_input.csv",
format = "single",
sep = ",",
header = TRUE,
cols = c("invoice_id", "stock_code")
)
basket_tnxdata %>% glimpse()
```
## Load Customer Data
We also want to load the various data about the customers such as their cohort
and so on.
```{r load_customer_cohort_data, echo=TRUE}
customer_cohort_tbl <- read_rds("data/customer_cohort_tbl.rds")
customer_cohort_tbl %>% glimpse()
```
## Load Product Data
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_product_data, echo=TRUE}
product_data_tbl <- read_rds("data/stock_description_tbl.rds")
product_data_tbl %>% glimpse()
```
# Build Association Rules Model
We now build our association rules based on the lower support data.
The idea is to repeat some of the initial association rules analysis: we use
the APRIORI algorithm to mine the rules, and then convert the discovered rules
to produce a graph of the products and the rules.
With this graph, we then use the disjoint components of this graph to cluster
the products, and take the largest subgraph and cluster that one according
to some standard clustering.
## Construct Association Rules
Having loaded the individual transaction data we now construct our basket data
and use the APRIORI algorithm to discover our rules.
```{r construct_association_rules, echo=TRUE}
basket_arules <- apriori(
basket_tnxdata,
parameter = list(supp = 0.005, conf = 0.01)
)
basket_arules_tbl <- basket_arules %>%
as("data.frame") %>%
as_tibble() %>%
arrange(desc(lift))
basket_arules_tbl %>% glimpse()
```
Having constructed the main association rules, we then convert the discovered
rules into a graph.
```{r convert_rules_to_graph, echo=TRUE}
apriori_rules_igraph <- basket_arules %>%
plot(
measure = "support",
method = "graph",
engine = "igraph",
control = list(max = 20000)
)
apriori_rules_igraph %>% summary()
```
Having constructed the graph, we now want to visualise it.
```{r plot_interactive_rules_graph, echo=TRUE}
basket_arules %>%
head(n = 500, by = "support") %>%
plot(
measure = "lift",
method = "graph",
engine = "htmlwidget"
)
```
## Determine Graph Clusters
With the constructed graph we now want to label the elements that are part
of the disjoint components of the graph.
```{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()
apriori_rules_tblgraph %>% print()
```
From the graph, we extract the nodes that correspond to the products (as
opposed to the nodes corresponding to the mined association rules). These are
identified as the various numeric values attached to the rules are blank.
We also wish to add an additional column that is the size of the group, so
it is easier to identify outsized subgraphs suitable for further partitioning.
```{r combine_connected_products, echo=TRUE}
product_cluster_disjoint_tbl <- apriori_rules_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(component_id) %>%
mutate(
cluster_size = n()
) %>%
ungroup() %>%
arrange(desc(cluster_size), label) %>%
group_by(component_id) %>%
mutate(
product_group_id = sprintf("AR_DISJOINT_%03d", cur_group_id()),
cluster_size,
stock_code = label
) %>%
ungroup() %>%
select(product_group_id, cluster_size, stock_code) %>%
arrange(product_group_id, stock_code)
product_cluster_disjoint_tbl %>% glimpse()
```
We now segment up the largest disjoint subgraph using alternative clustering
techniques.
We try a few different types - inspecting the output of the various algorithms
to see which clustering may be the
```{r create_largest_subgraph_clusters, echo=TRUE, cache=TRUE}
run_subgraph_clusters <- function(graph_cluster_func, rules_tblgraph, ...) {
subgraph_clusters_tbl <- rules_tblgraph %>%
convert(to_subgraph, component_size == max(component_size)) %>%
morph(to_undirected) %>%
mutate(
sub_id = graph_cluster_func(...)
) %>%
unmorph() %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
count(sub_id, name = "cluster_size", sort = TRUE) %>%
mutate(
sub_id = factor(1:n(), levels = 1:n())
)
return(subgraph_clusters_tbl)
}
cluster_func <- c(
"group_fast_greedy",
"group_infomap",
"group_label_prop",
"group_louvain",
"group_spinglass"
)
cluster_data_tbl <- tibble(cluster_func_name = cluster_func) %>%
mutate(
cluster_func = map(cluster_func_name, get),
clustered = map(cluster_func, run_subgraph_clusters,
rules_tblgraph = apriori_rules_tblgraph)
) %>%
select(cluster_func_name, clustered) %>%
unnest(clustered)
cluster_data_tbl %>% glimpse()
```
Having split this largest component into various splits, we now visualise the
count and size of each cluster and use this to determine which clustering
splits the data into a smaller number of larger clusters.
```{r visualise_cluster_count, echo=TRUE}
ggplot(cluster_data_tbl) +
geom_col(aes(x = sub_id, y = cluster_size)) +
geom_hline(aes(yintercept = 5), colour = "red") +
facet_wrap(vars(cluster_func_name), scales = "free") +
labs(
x = "ID",
y = "Cluster Size"
) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, size = 8))
```
```{r plot_distribution_cluster_sizes, echo=TRUE}
plot_tbl <- cluster_data_tbl %>%
group_by(cluster_func_name) %>%
count(cluster_size, name = "cluster_count", sort = TRUE) %>%
ungroup() %>%
mutate(cluster_size = as.factor(cluster_size))
ggplot(plot_tbl) +
geom_col(aes(x = cluster_size, y = cluster_count, group = cluster_size)) +
facet_wrap(vars(cluster_func_name), scales = "free") +
labs(
x = "Cluster Size",
y = "Community Count",
title = "Visualisation of Spread of Cluster Sizes"
) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
```
From this, it appears that `louvain` is the method of choice.
Thus, we re-run the clustering for this larger component using the chosen
algorithm and use this to create our various product groups.
```{r construct_fast_greedy_clusters, echo=TRUE}
subgraph_groups_tbl <- apriori_rules_tblgraph %>%
convert(to_subgraph, component_size == max(component_size)) %>%
morph(to_undirected) %>%
mutate(
sub_id = group_louvain()
) %>%
unmorph() %>%
activate(nodes) %>%
as_tibble() %>%
filter(are_na(support)) %>%
group_by(sub_id) %>%
mutate(
cluster_size = n()
) %>%
ungroup() %>%
arrange(desc(cluster_size), label) %>%
group_by(sub_id) %>%
mutate(
product_group_id = sprintf("AR_LARGE_%03d", cur_group_id()),
cluster_size,
stock_code = label
) %>%
ungroup() %>%
select(product_group_id, cluster_size, stock_code) %>%
arrange(product_group_id, stock_code)
subgraph_groups_tbl %>% glimpse()
```
We now combine both these lists of groupings and combine them.
```{r combine_product_cluster, echo=TRUE}
product_cluster_tbl <- list(
product_cluster_disjoint_tbl,
subgraph_groups_tbl
) %>%
bind_rows() %>%
filter(product_group_id != "AR_DISJOINT_001")
product_cluster_tbl %>% glimpse()
```
## Assign Products to Groups
We now want to look at our complete list of products and then assign them to
each of our product groups. In terms of coverage, we need to check to see if
all the products appearing in the most invoices.
We also want to look at the most commonly purchased items (in terms of
appearance in baskets as opposed to quantity sold).
```{r construct_popular_product_data, echo=TRUE}
product_popular_tbl <- tnx_purchase_tbl %>%
mutate(
stock_code = str_to_upper(stock_code)
) %>%
count(stock_code, name = "invoice_count", sort = TRUE)
product_popular_tbl %>% glimpse()
```
We now combine this data to construct a product dataset containing the
relevant summary data about each product.
```{r construct_product_dataset, echo=TRUE}
product_data_full_tbl <- product_data_tbl %>%
left_join(product_cluster_tbl, by = "stock_code") %>%
left_join(product_popular_tbl, by = "stock_code") %>%
replace_na(
list(product_group_id = "none", cluster_size = "0")
) %>%
arrange(desc(invoice_count)) %>%
mutate(ranking = 1:n()) %>%
semi_join(tnx_purchase_tbl, by = "stock_code") %>%
arrange(stock_code)
product_data_full_tbl %>% glimpse()
```
First, let us export the table to help us inspect the data.
```{r show_product_data_dt, echo=TRUE}
product_data_full_tbl %>% datatable()
```
To make it more obvious, we look at the products unassigned to a group and
see how they rank in terms of invoice count.
```{r show_unassigned_products, echo=TRUE}
product_data_full_tbl %>% filter(product_group_id == "none") %>% datatable()
```
# Construct Transaction-Based Graph Clusters
We can treat the transaction data as a graph, turning both invoices and stock
items into nodes on the graph, and create an edge between stock and invoices
when the item occurs on the invoice.
We then cluster the graph to create groupings for the different stock codes.
```{r construct_basket_graph, echo=TRUE}
stock_nodes_tbl <- tnx_purchase_tbl %>%
select(stock_code) %>%
distinct() %>%
transmute(node_label = stock_code, node_type = "stock")
invoice_nodes_tbl <- tnx_purchase_tbl %>%
select(invoice_id) %>%
distinct() %>%
transmute(node_label = invoice_id, node_type = "invoice")
nodes_tbl <- list(stock_nodes_tbl, invoice_nodes_tbl) %>%
bind_rows()
edges_tbl <- tnx_purchase_tbl %>%
group_by(stock_code, invoice_id) %>%
summarise(
.groups = "drop",
total_quantity = sum(quantity),
total_cost = sum(quantity * price)
)
basket_tblgraph <- tbl_graph(
nodes = nodes_tbl,
edges = edges_tbl,
directed = FALSE,
node_key = "node_label"
)
```
## Check Graph Clustering Approaches
First we perform our basic clustering by splitting off the different disjoint
components of the graph.
```{r create_disjoint_component_labels, echo=TRUE}
basket_tblgraph <- basket_tblgraph %>%
mutate(
component_id = group_components()
) %>%
group_by(component_id) %>%
mutate(
component_size = n()
) %>%
ungroup()
basket_tblgraph %>% print()
```
We now want to check the sizes of the disjoint components of this graph.
```{r display_main_component_sizes, echo=TRUE}
basket_tblgraph %>%
as_tibble() %>%
filter(node_type == "stock") %>%
count(component_id, name = "stock_count", sort = TRUE)
```
We see that almost all the stock codes are contained in that one large
component and so confine the rest of this analysis to that one large component.
```{r run_subgraph_clusters, echo=TRUE}
run_subgraph_clusters <- function(graph_cluster_func, labelling, input_tblgraph, ...) {
message(glue("Clustering the graph using {labelling}..."))
subgraph_clusters_tbl <- input_tblgraph %>%
mutate(
cluster_id = graph_cluster_func(...)
) %>%
activate(nodes) %>%
as_tibble() %>%
filter(node_type == "stock") %>%
count(cluster_id, name = "cluster_size", sort = TRUE) %>%
mutate(
cluster_id = factor(1:n(), levels = 1:n())
)
return(subgraph_clusters_tbl)
}
```
```{r test_subgraph_cluster_sizes, echo=TRUE, cache=TRUE}
cluster_func <- c(
"group_fast_greedy",
"group_infomap",
"group_leading_eigen",
"group_louvain"
)
largecomp_tblgraph <- basket_tblgraph %>%
convert(to_subgraph, component_size == max(component_size))
cluster_data_tbl <- tibble(cluster_func_name = cluster_func) %>%
mutate(
cluster_func = map(cluster_func_name, get),
clustered = map2(
cluster_func, cluster_func_name,
run_subgraph_clusters,
input_tblgraph = largecomp_tblgraph
)
) %>%
select(cluster_func_name, clustered) %>%
unnest(clustered)
cluster_data_tbl %>% glimpse()
```
Having created a summary of the data splits, we now want to construct a
visualisation of how the various cluster routines split the data.
To do this, we turn the size of each cluster into a 'label' and then count how
many clusters of that size there are. We then use this summary data to
construct barplots of the size.
```{r visualise_community_splits, echo=TRUE}
plot_tbl <- cluster_data_tbl %>%
group_by(cluster_func_name) %>%
count(cluster_size, name = "cluster_count", sort = TRUE) %>%
ungroup() %>%
mutate(cluster_size = as.factor(cluster_size))
ggplot(plot_tbl) +
geom_col(aes(x = cluster_size, y = cluster_count, group = cluster_size)) +
facet_wrap(vars(cluster_func_name), scales = "free") +
labs(
x = "Cluster Size",
y = "Community Count",
title = "Visualisation of Spread of Cluster Sizes"
) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
```
From this graphic, we see that we want to use `group_louvain` gives us the
most even split across the data - though the sizes are still hugely unequal.
## Create Cluster-Based Allocation
We now use this algorithm to cluster this large component in the graph, and
this gives us an alternative allocation of the each `stock_code` to a product
group.
```{r cluster_largest_component_louvain, echo=TRUE}
largecomp_clustered_tbl <- largecomp_tblgraph %>%
mutate(
cluster_id = group_louvain()
) %>%
activate(nodes) %>%
as_tibble() %>%
filter(node_type == "stock") %>%
mutate(
cluster_group = sprintf("TNX_%03d", cluster_id)
) %>%
select(stock_code = node_label, cluster_group)
largecomp_clustered_tbl %>% glimpse()
```
## Combine Clustering Data
We now want to combine this data to construct our stock code allocations.
```{r combine_clustering_data, echo=TRUE}
other_tbl <- basket_tblgraph %>%
activate(nodes) %>%
as_tibble() %>%
filter(
node_type == "stock",
component_size != max(component_size)
) %>%
transmute(
stock_code = node_label, cluster_group = "TNX_010"
)
product_group_tnxgroups_tbl <- list(
largecomp_clustered_tbl,
other_tbl
) %>%
bind_rows() %>%
arrange(stock_code) %>%
inner_join(product_data_tbl, by = "stock_code") %>%
select(stock_code, product_group = cluster_group, desc)
product_group_tnxgroups_tbl %>% glimpse()
```
# Construct RFM Customer Segments
We now wish to repeat our RFM analysis, and then we reassign the customer base
to each of these groupings.
```{r construct_customer_segments, echo=TRUE}
segment_names <- c(
"Champions", "Loyal Customers", "Potential Loyalist", "New Customers",
"Promising", "Need Attention", "About To Sleep", "At Risk",
"Can't Lose Them", "Lost"
)
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)
segment_defs_tbl <- tibble(
segment_names,
recency_lower,
recency_upper,
frequency_lower,
frequency_upper,
monetary_lower,
monetary_upper
)
segment_defs_tbl %>% glimpse()
```
We first visually inspect these segment definitions and the bands.
```{r display_customer_segment_definitions, echo=TRUE}
segments_show_tbl <- segment_defs_tbl %>%
mutate(
recency = glue("{recency_lower}-{recency_upper}") %>% as.character(),
frequency = glue("{frequency_lower}-{frequency_upper}") %>% as.character(),
monetary = glue("{monetary_lower}-{monetary_upper}") %>% as.character()
) %>%
select(
segment_names, recency, frequency, monetary
)
segments_show_tbl %>%
datatable(
colnames = c("Segment", "R", "F", "M"),
options = list(
columnDefs = list(list(className = 'dt-left', targets = 0:4))
)
)
```
We now construct the RFM data from the purchase data and assign each of the
customers to a segment based on their RFM score.
There is a reasonable number of transactions with a missing `customer_id`, so
we exclude this from the analysis.
```{r construct_basic_rfm_structures, echo=TRUE}
customer_rfmdata <- tnx_purchase_tbl %>%
filter(
!are_na(customer_id),
invoice_date <= training_data_date
) %>%
group_by(invoice_date, customer_id) %>%
summarise(
.groups = "drop",
total_spend = sum(stock_value)
) %>%
rfm_table_order(
customer_id = customer_id,
order_date = invoice_date,
revenue = total_spend,
analysis_date = training_data_date
)
customer_rfmdata %>% print()
```
## Visualise RFM Data
As we explored earlier, the `rfm` package provides a number of inbuilt
descriptive visualisations.
First we look at the count of customers at each order count:
```{r rfm_order_count_barplot, echo=TRUE}
customer_rfmdata %>%
rfm_order_dist(print_plot = FALSE)
```
We also have a few summary plots - showing the histograms of the recency,
frequency and monetary measures.
```{r rfm_histograms, echo=TRUE}
customer_rfmdata %>%
rfm_histograms(print_plot = FALSE) +
scale_x_continuous(labels = label_comma()) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
```
Finally, we look at each of the three bivariate plots to explore the
relationship between the three quantities.
```{r plot_bivariate_visualisation, echo=TRUE}
customer_rfmdata %>%
rfm_rm_plot(print_plot = FALSE) +
scale_x_log10(labels = label_comma()) +
scale_y_log10(labels = label_comma())
customer_rfmdata %>%
rfm_rf_plot(print_plot = FALSE) +
scale_x_log10(labels = label_comma()) +
scale_y_log10(labels = label_comma())
customer_rfmdata %>%
rfm_fm_plot(print_plot = FALSE) +
scale_x_log10(labels = label_comma()) +
scale_y_log10(labels = label_comma())
```
## Assign Customer Segments
We now assign each customer to a segment and this allows us to analyse each
of the segments.
```{r segment_customer_base, echo=TRUE}
customer_segments_tbl <- customer_rfmdata %>%
rfm_segment(
segment_names = segment_names,
recency_lower = recency_lower,
recency_upper = recency_upper,
frequency_lower = frequency_lower,
frequency_upper = frequency_upper,
monetary_lower = monetary_lower,
monetary_upper = monetary_upper
)
customer_segments_tbl %>% glimpse()
```
We want to plot the count of each of the customer segments, before we calculate
the various summary statistics.
```{r plot_segment_count, echo=TRUE}
customer_segment_count_tbl <- customer_segments_tbl %>%
count(segment, name = "count", sort = TRUE)
ggplot(customer_segment_count_tbl) +
geom_col(aes(x = segment, y = count, fill = segment)) +
scale_fill_brewer(type = "qual", palette = "Set1") +
labs(
x = "Segment",
y = "Count"
) +
theme(
axis.text.x = element_text(angle = 20, vjust = 0.5),
legend.position = "none"
)
```
Again, `rfm` provides a number of inbuilt plots of the segments, but as they
create very simple summary plots we create these plots ourselves and this
allows us to summarise the segments however we wish.
```{r create_customer_segment_summary_plots, echo=TRUE}
plot_tbl <- customer_segments_tbl %>%
select(
customer_id, segment,
Transactions = transaction_count,
Recency = recency_days,
`Total Spend` = amount
) %>%
pivot_longer(
!c(customer_id, segment),
names_to = "quantity",
values_to = "value"
) %>%
mutate(
value = pmax(0.1, value)
)
ggplot(plot_tbl) +
geom_boxplot(aes(x = segment, y = value, fill = segment)) +
expand_limits(y = 0.1) +
facet_wrap(vars(quantity), ncol = 2, scales = "free_y") +
scale_y_log10(labels = label_comma()) +
scale_fill_brewer(type = "qual", palette = "Set1") +
labs(
x = "Customer Segment",
y = "Value"
) +
theme(
axis.text.x = element_text(angle = 20, vjust = 0.5, size = 8),
legend.position = "none"
)
```
## Inspect Segment Validation
Now that we have assigned customers to segments, we use these segments to
assess the transactions made after the cutoff date `training_data_date`.
```{r construct_post_training_data, echo=TRUE}
segments_alloc_tbl <- customer_segments_tbl %>%
select(customer_id, segment)
daily_spend_tbl <- tnx_purchase_tbl %>%
filter(invoice_date > training_data_date) %>%
group_by(invoice_date, customer_id) %>%
summarise(
.groups = "drop",
daily_spend = sum(stock_value)
) %>%
left_join(segments_alloc_tbl, by = "customer_id") %>%
replace_na(list(segment = "New Customer"))
daily_spend_tbl %>% glimpse()
```
Having constructed this data, we now calculate some per-customer summary
statistics.
```{r construct_post_date_stats, echo=TRUE}
postdate_customer_stats_tbl <- daily_spend_tbl %>%
group_by(customer_id, segment) %>%
summarise(
.groups = "drop",
total_transactions = n(),
total_spend = sum(daily_spend)
)
postdate_customer_stats_tbl %>% glimpse()
```
First we compare the segment counts from both pre- and post- dates. The first
metric to check is the proportion of the segment in the dataset, though we
exclude newly arrived customers in the post-date dataset to enable a direct
comparison.
```{r compare_pre_post_segment_data, echo=TRUE}
pre_data_tbl <- customer_segment_count_tbl %>%
mutate(prop = count / sum(count))
post_data_tbl <- postdate_customer_stats_tbl %>%
filter(segment != "New Customer") %>%
count(segment, name = "count", sort = TRUE) %>%
mutate(prop = count / sum(count))
comparison_tbl <- list(
Training = pre_data_tbl,
Validation = post_data_tbl
) %>%
bind_rows(.id = "data")
ggplot(comparison_tbl) +
geom_col(aes(x = segment, y = prop, fill = data), position = "dodge") +
labs(
x = "Segment",
y = "Segment Proportion",
fill = "Dataset"
) +
scale_fill_brewer(type = "qual", palette = "Dark2") +
theme(axis.text.x = element_text(angle = 20, vjust = 0.5))
```
Next, we check our RFM stats according the validation data as of the final
date in the dataset.
First we construct the data and then construct boxplots of each segment.
```{r calculate_validation_rfm_metrics, echo=TRUE}
validation_date <- daily_spend_tbl %>% pull(invoice_date) %>% max()
validation_rfm_data_tbl <- daily_spend_tbl %>%
group_by(segment, customer_id) %>%
summarise(
.groups = "drop",
transaction_count = n(),
recency_days = (validation_date - max(invoice_date)) %>% as.numeric(),
amount = sum(daily_spend)
)
validation_rfm_data_tbl %>% glimpse()
```
Having constructed the table - we view the data as a JS datatable.
```{r show_validation_rfm_data_dt, echo=TRUE}
validation_rfm_data_tbl %>% datatable()
```
We now produce boxplots of the three metrics using these segments, and also
look at the new customers as a separate category.