rm(list=ls(all=TRUE))
knitr::opts_chunk$set(comment = NA)
knitr::opts_knit$set(global.par = TRUE) 
options(scipen=20, digits=5, width=100, tibble.print_min=4)
rmarkdown::find_pandoc(version = '2.7.3')
## $version
## [1] '0'
## 
## $dir
## NULL
if(!require(heatmaply)) install.packages("heatmaply")
if(!require(Matrix)) install.packages("Matrix")
if(!require(arules)) install.packages("arules")
pacman::p_load(ggplot2,dplyr,heatmaply)
pacman::p_load(Matrix, arules)


load("data/tf0.rdata")
sapply(list(cust=A0,tid=X0,items=Z0), nrow)
  cust    tid  items 
 32241 119328 817182 
Z0$tid %>% n_distinct
[1] 119422
Z0$prod %>% n_distinct
[1] 23789
p = count(Z0, prod, sort=T)
pk = p$prod[1:2000]
Z = filter(Z0, prod %in% pk)
L = split(Z$prod, Z$tid)
tr = as(L, "transactions"); tr
transactions in sparse format with
 107797 transactions (rows) and
 2000 items (columns)
rules <- apriori(tr, parameter=list(supp=0.0002, conf=0.5))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target  ext
        0.5    0.1    1 none FALSE            TRUE       5  0.0002      1     10  rules TRUE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 21 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[2000 item(s), 107797 transaction(s)] done [0.13s].
sorting and recoding items ... [2000 item(s)] done [0.00s].
creating transaction tree ... done [0.03s].
checking subsets of size 1 2 3 4 5 6 done [0.04s].
writing ... [1001 rule(s)] done [0.01s].
creating S4 object  ... done [0.01s].
summary(rules)
set of 1001 rules

rule length distribution (lhs + rhs):sizes
  2   3   4   5   6 
 85 558 289  64   5 

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   2.00    3.00    3.00    3.35    4.00    6.00 

summary of quality measures:
    support           confidence       coverage             lift             count    
 Min.   :0.000204   Min.   :0.500   Min.   :0.000232   Min.   :   6.64   Min.   : 22  
 1st Qu.:0.000232   1st Qu.:0.557   1st Qu.:0.000371   1st Qu.:  60.48   1st Qu.: 25  
 Median :0.000297   Median :0.636   Median :0.000482   Median :  91.25   Median : 32  
 Mean   :0.000519   Mean   :0.652   Mean   :0.000829   Mean   : 211.00   Mean   : 56  
 3rd Qu.:0.000501   3rd Qu.:0.736   3rd Qu.:0.000789   3rd Qu.: 281.70   3rd Qu.: 54  
 Max.   :0.006494   Max.   :0.966   Max.   :0.009648   Max.   :1131.30   Max.   :700  

mining info:
 data ntransactions support confidence
   tr        107797  0.0002        0.5
                                                            call
 apriori(data = tr, parameter = list(supp = 0.0002, conf = 0.5))
rx = subset(rules, subset = lift > 100 & count > 100) 
inspect(rx)
     lhs                               rhs             support    confidence coverage  lift   count
[1]  {4710030346110}                => {4710030346097} 0.00094622 0.55135    0.0017162 264.15 102  
[2]  {4710030346110}                => {4710030346103} 0.00111320 0.64865    0.0017162 272.07 120  
[3]  {4710030346110}                => {4710030346059} 0.00105754 0.61622    0.0017162 202.52 114  
[4]  {4716114000312}                => {4716114000329} 0.00141006 0.55273    0.0025511 209.06 152  
[5]  {4716114000329}                => {4716114000312} 0.00141006 0.53333    0.0026439 209.06 152  
[6]  {4710030346097}                => {4710030346103} 0.00109465 0.52444    0.0020873 219.97 118  
[7]  {4710030346097}                => {4710030346059} 0.00121525 0.58222    0.0020873 191.35 131  
[8]  {4719859015061}                => {4713080626225} 0.00105754 0.50667    0.0020873 241.67 114  
[9]  {4713080626225}                => {4719859015061} 0.00105754 0.50442    0.0020965 241.67 114  
[10] {4711524000617}                => {4711524000419} 0.00094622 0.54545    0.0017347 241.97 102  
[11] {4710030346103}                => {4710030346059} 0.00155848 0.65370    0.0023841 214.84 168  
[12] {4710030346059}                => {4710030346103} 0.00155848 0.51220    0.0030428 214.84 168  
[13] {4710008241119}                => {4710008241218} 0.00094622 0.52041    0.0018182 177.53 102  
[14] {4711524000457}                => {4711524000419} 0.00095550 0.50490    0.0018924 223.98 103  
[15] {4711524000457}                => {4711524000396} 0.00101116 0.53431    0.0018924 155.67 109  
[16] {0076150430530}                => {0076150215281} 0.00108537 0.51770    0.0020965 169.11 117  
[17] {4719090701051}                => {4719090790000} 0.00163270 0.57705    0.0028294 110.10 176  
[18] {4711524000471}                => {4711524000396} 0.00108537 0.51092    0.0021244 148.85 117  
[19] {4711524000495}                => {4711524000396} 0.00115031 0.50000    0.0023006 145.67 124  
[20] {4711524000419}                => {4711524000396} 0.00153065 0.67901    0.0022542 197.83 165  
[21] {4710321861209}                => {4710321861186} 0.00207798 0.60870    0.0034138 124.51 224  
[22] {4710321871260}                => {4710321861186} 0.00185534 0.54054    0.0034324 110.57 200  
[23] {4711524000907}                => {4711524000891} 0.00143789 0.56364    0.0025511 172.61 155  
[24] {4711524000907}                => {4711524001041} 0.00166053 0.65091    0.0025511 175.42 179  
[25] {0719859796124}                => {0719859796117} 0.00232845 0.69529    0.0033489 132.89 251  
[26] {4719090790017}                => {4719090790000} 0.00325612 0.80876    0.0040261 154.30 351  
[27] {4719090790000}                => {4719090790017} 0.00325612 0.62124    0.0052413 154.30 351  
[28] {4711524000891}                => {4711524001041} 0.00195738 0.59943    0.0032654 161.54 211  
[29] {4711524001041}                => {4711524000891} 0.00195738 0.52750    0.0037107 161.54 211  
[30] {4719090701051, 4719090790017} => {4719090790000} 0.00102971 0.84733    0.0012152 161.66 111  
[31] {4719090701051, 4719090790000} => {4719090790017} 0.00102971 0.63068    0.0016327 156.65 111  
[32] {4710321861209, 4710321871260} => {4710321861186} 0.00102044 0.63584    0.0016049 130.06 110  
[33] {4710321861186, 4710321871260} => {4710321861209} 0.00102044 0.55000    0.0018553 161.11 110  
[34] {4711524000891, 4711524000907} => {4711524001041} 0.00110393 0.76774    0.0014379 206.90 119  
[35] {4711524000907, 4711524001041} => {4711524000891} 0.00110393 0.66480    0.0016605 203.59 119  
[36] {4711524000891, 4711524001041} => {4711524000907} 0.00110393 0.56398    0.0019574 221.07 119  

☝️ The Association Rules …