1 Introduction

Analyzing purchasing behavior is very important nowadays. Thanks to it, we can run a better recommendation system, understand the relationships between products, adapt to customer behavior, and achieve higher earnings.

2 Data description

The “online_retail_II.xlsx” is a real online retail transaction data set provided by UC Irvine Machine Learning Repository [1]. It contains all the transactions that occurred for a UK-based and registered, non-store online retail between 01/12/2009 and 09/12/2011. The company mainly sells unique all-occasion gift-ware. Many customers of the company are wholesalers. Each row in the data set represents one item on one invoice, and the available information includes the invoice number, stock code of the item, item description, quantity, invoice date, price, customer ID, and country. For the purpose of this study, we will focus on the period from 01/12/2009 to 09/12/2010.

head(x,10)
## # A tibble: 10 × 8
##    Invoice StockCode Description              Quantity InvoiceDate         Price
##    <chr>   <chr>     <chr>                       <dbl> <dttm>              <dbl>
##  1 489434  85048     "15CM CHRISTMAS GLASS B…       12 2009-12-01 07:45:00  6.95
##  2 489434  79323P    "PINK CHERRY LIGHTS"           12 2009-12-01 07:45:00  6.75
##  3 489434  79323W    "WHITE CHERRY LIGHTS"          12 2009-12-01 07:45:00  6.75
##  4 489434  22041     "RECORD FRAME 7\" SINGL…       48 2009-12-01 07:45:00  2.1 
##  5 489434  21232     "STRAWBERRY CERAMIC TRI…       24 2009-12-01 07:45:00  1.25
##  6 489434  22064     "PINK DOUGHNUT TRINKET …       24 2009-12-01 07:45:00  1.65
##  7 489434  21871     "SAVE THE PLANET MUG"          24 2009-12-01 07:45:00  1.25
##  8 489434  21523     "FANCY FONT HOME SWEET …       10 2009-12-01 07:45:00  5.95
##  9 489435  22350     "CAT BOWL"                     12 2009-12-01 07:46:00  2.55
## 10 489435  22349     "DOG BOWL , CHASING BAL…       12 2009-12-01 07:46:00  3.75
## # ℹ 2 more variables: `Customer ID` <dbl>, Country <chr>

3 Data preparation

3.1 Data cleaning

For the purposes of this work, we are most interested in the invoice number and product description, if any of these values are unknown, such a row will be deleted.

x[(is.na(x$Invoice)| is.na(x$Description)), ]
## # A tibble: 2,928 × 8
##    Invoice StockCode Description Quantity InvoiceDate         Price
##    <chr>   <chr>     <chr>          <dbl> <dttm>              <dbl>
##  1 489521  21646     <NA>             -50 2009-12-01 11:44:00     0
##  2 489655  20683     <NA>             -44 2009-12-01 17:26:00     0
##  3 489659  21350     <NA>             230 2009-12-01 17:39:00     0
##  4 489781  84292     <NA>              17 2009-12-02 11:45:00     0
##  5 489806  18010     <NA>            -770 2009-12-02 12:42:00     0
##  6 489821  85049G    <NA>            -240 2009-12-02 13:25:00     0
##  7 489882  35751C    <NA>              12 2009-12-02 16:22:00     0
##  8 489898  79323G    <NA>             954 2009-12-03 09:40:00     0
##  9 489901  21098     <NA>            -200 2009-12-03 09:47:00     0
## 10 489903  21166     <NA>              48 2009-12-03 09:57:00     0
## # ℹ 2,918 more rows
## # ℹ 2 more variables: `Customer ID` <dbl>, Country <chr>

There are some rows with missing values. All of them will be dropped.

x <- x[!(is.na(x$Invoice) | is.na(x$Description)), ]
x[(is.na(x$Invoice) | is.na(x$Description)), ]
## # A tibble: 0 × 8
## # ℹ 8 variables: Invoice <chr>, StockCode <chr>, Description <chr>,
## #   Quantity <dbl>, InvoiceDate <dttm>, Price <dbl>, Customer ID <dbl>,
## #   Country <chr>

3.2 Data Transformation

3.2.1 Extracting interesting data

The main objective of this work is to extract information about the relationships between products in the customer basket. For this purpose, we only require data related to the invoice number and the description of the product. Although the stock code could be used instead of the product description. For this project, it is more convenient to examine the product description rather than the raw stock code, as it provides more meaningful information.

xRed <- x[,c(1,3)]
head(xRed,5)
## # A tibble: 5 × 2
##   Invoice Description                          
##   <chr>   <chr>                                
## 1 489434  "15CM CHRISTMAS GLASS BALL 20 LIGHTS"
## 2 489434  "PINK CHERRY LIGHTS"                 
## 3 489434  "WHITE CHERRY LIGHTS"                
## 4 489434  "RECORD FRAME 7\" SINGLE SIZE"       
## 5 489434  "STRAWBERRY CERAMIC TRINKET BOX"

3.2.2 Data transformation to transaction format

In the code below, the data format has been converted to a transaction format. This format will facilitate data analysis, exploration and creation of rules between products.

write.table(xRed, file = "my_basket.csv", row.names=FALSE,sep = ", ",quote = FALSE ,col.names = FALSE)
trans = read.transactions("my_basket.csv", format = "single",cols=c(1,2), sep=",",quote = "")

3.2.2.1 First five transactions

inspect(trans[1:5])
##     items                                   transactionID
## [1] { 15CM CHRISTMAS GLASS BALL 20 LIGHTS,               
##       FANCY FONT HOME SWEET HOME DOORMAT,                
##       PINK CHERRY LIGHTS,                                
##       PINK DOUGHNUT TRINKET POT,                         
##       RECORD FRAME 7" SINGLE SIZE,                       
##       SAVE THE PLANET MUG,                               
##       STRAWBERRY CERAMIC TRINKET BOX,                    
##       WHITE CHERRY LIGHTS}                         489434
## [2] { CAT BOWL,                                          
##       DOG BOWL ,                                         
##       HEART MEASURING SPOONS LARGE,                      
##       LUNCHBOX WITH CUTLERY FAIRY CAKES}           489435
## [3] { AREA PATROLLED METAL SIGN,                         
##       ASSORTED COLOUR BIRD ORNAMENT,                     
##       BATH BUILDING BLOCK WORD,                          
##       BISCUITS SMALL BOWL LIGHT BLUE,                    
##       BLACK DINER WALL CLOCK,                            
##       CHRISTMAS CRAFT WHITE FAIRY,                       
##       CLASSIC WHITE FRAME,                               
##       DOOR MAT BLACK FLOCK,                              
##       FULL ENGLISH BREAKFAST PLATE,                      
##       HEART FILIGREE DOVE LARGE,                         
##       HEART IVORY TRELLIS LARGE,                         
##       HOME BUILDING BLOCK WORD,                          
##       LOVE BUILDING BLOCK WORD,                          
##       PEACE WOODEN BLOCK LETTERS,                        
##       PIZZA PLATE IN BOX,                                
##       PLEASE ONE PERSON  METAL SIGN,                     
##       SCOTTIE DOG HOT WATER BOTTLE,                      
##       SET OF 3 BLACK FLYING DUCKS,                       
##       SMALL MARSHMALLOWS PINK BOWL}                489436
## [4] { BLUE PADDED SOFT MOBILE,                           
##       CHOCOLATE HOT WATER BOTTLE,                        
##       CHRISTMAS CRAFT HEART DECORATIONS,                 
##       CHRISTMAS CRAFT HEART STOCKING,                    
##       CINAMMON & ORANGE WREATH,                          
##       EUCALYPTUS & PINECONE  WREATH,                     
##       FELTCRAFT DOLL EMILY,                              
##       FELTCRAFT DOLL MARIA,                              
##       FELTCRAFT DOLL ROSIE,                              
##       FLORAL BLUE MONSTER,                               
##       HANGING HEART ZINC T-LIGHT HOLDER,                 
##       INFLATABLE POLITICAL GLOBE,                        
##       JOY LARGE WOOD LETTERS,                            
##       PACK OF 20 SKULL PAPER NAPKINS,                    
##       PACK OF 6 SKULL PAPER CUPS,                        
##       PARTY CONE CHRISTMAS DECORATION,                   
##       PEACE SMALL WOOD LETTERS,                          
##       PINK BLUE FELT CRAFT TRINKET BOX,                  
##       RETRO COFFEE MUGS ASSORTED,                        
##       SCOTTIE DOG HOT WATER BOTTLE,                      
##       STRIPES DESIGN MONKEY DOLL,                        
##       VINTAGE SNAKES & LADDERS,                          
##       WOODEN BOX ADVENT CALENDAR}                  489437
## [5] { CARROT CHARLIE+LOLA COASTER SET,                   
##       CHARLIE & LOLA WASTEPAPER BIN BLUE,                
##       CHARLIE & LOLA WASTEPAPER BIN FLORA,               
##       CHARLIE + LOLA BISCUITS TINS,                      
##       CHARLIE + LOLA RED HOT WATER BOTTLE,               
##       CHARLIE AND LOLA CHARLOTTE BAG,                    
##       CHARLIE AND LOLA FIGURES TINS,                     
##       CHARLIE LOLA BLUE HOT WATER BOTTLE,                
##       CHARLIE+LOLA PINK HOT WATER BOTTLE,                
##       CHARLIE+LOLA RED HOT WATER BOTTLE,                 
##       COUNTRY COTTAGE  DOORSTOP GREEN,                   
##       DINOSAURS  WRITING SET,                            
##       GINGHAM HEART  DOORSTOP RED,                       
##       JUMBO BAG CHARLIE AND LOLA TOYS,                   
##       JUMBO BAG TOYS,                                    
##       SET OF MEADOW  FLOWER STICKERS,                    
##       TOMATO CHARLIE+LOLA COASTER SET}             489438

3.2.2.2 General statistics

summary(trans)
## transactions as itemMatrix in sparse format with
##  25888 rows (elements/itemsets/transactions) and
##  4550 columns (items) and a density of 0.004297642 
## 
## most frequent items:
##  WHITE HANGING HEART T-LIGHT HOLDER            REGENCY CAKESTAND 3 TIER 
##                                3409                                2186 
##      STRAWBERRY CERAMIC TRINKET BOX       ASSORTED COLOUR BIRD ORNAMENT 
##                                1767                                1420 
##    PACK OF 72 RETRO SPOT CAKE CASES                             (Other) 
##                                1419                              496020 
## 
## element (itemset/transaction) length distribution:
## sizes
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 
## 5126 1792 1160  896  775  677  635  584  636  562  588  567  566  541  556  523 
##   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32 
##  512  517  511  499  439  403  365  324  303  307  273  249  251  241  201  178 
##   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48 
##  168  165  160  149  143  135  142  159  133  104  103  109   89   92  112   79 
##   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64 
##   88   74   71   78   56   60   59   62   58   62   45   46   36   47   38   42 
##   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80 
##   33   29   23   22   28   32   35   20   27   29   17   15   15   16   20   19 
##   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96 
##   11   10   16   13   12    9   15   19   11   11    9    7    9    9   13    5 
##   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112 
##   12    8    6   12    7    3    9    9    8    6    9    7    8    6    6    8 
##  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128 
##    9    8   11    9    3    6    4    4   10    2    5    4    5    4    4    4 
##  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144 
##    3    6    5    8    9    6    7    4    3    4    3    2    2    6    3    3 
##  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160 
##    5    3    6    7    2    3    6    3    1    3    4    5    2    6    2    2 
##  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176 
##    5    4    3    2    6    6    4    2    2    2    3    2    1    2    1    5 
##  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192 
##    2    1    3    3    3    1    2    3    2    1    2    1    3    2    3    1 
##  193  194  195  196  197  198  199  200  201  202  204  206  208  211  212  216 
##    3    4    2    2    1    3    1    6    2    1    3    4    1    2    2    2 
##  217  218  219  220  221  222  223  224  226  228  229  233  235  238  239  240 
##    3    3    3    2    2    1    1    2    1    2    1    1    1    1    1    3 
##  243  244  246  247  252  254  259  262  263  264  265  267  270  271  273  275 
##    2    1    3    1    2    2    1    1    1    1    3    1    2    2    1    1 
##  277  281  284  295  296  306  312  313  316  321  322  327  335  337  338  341 
##    1    1    1    2    2    1    1    1    1    1    1    1    2    1    1    1 
##  342  352  355  359  364  368  369  370  376  378  380  398  399  406  407  410 
##    2    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
##  420  423  427  429  433  436  437  439  441  444  446  456  459  460  465  466 
##    1    1    2    1    1    1    1    1    1    1    1    1    1    1    2    1 
##  474  475  477  479  481  493  494  495  504  511  514  521  536  541  543  553 
##    1    1    1    2    1    1    2    1    1    1    1    1    1    1    1    1 
##  567  574  576  584  586  588  595  599  644  672 
##    1    2    1    1    1    1    1    1    1    1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    2.00   11.00   19.55   23.00  672.00 
## 
## includes extended item information - examples:
##                      labels
## 1     *Boombox Ipod Classic
## 2  *USB Office Glitter Lamp
## 3   *USB Office Mirror Ball
## 
## includes extended transaction information - examples:
##   transactionID
## 1        489434
## 2        489435
## 3        489436

Here, we can find the first five transactions and some general statistics for all transactions. We’ll dive into more details in the next chapters.

4 Data analysis

4.1 Number of transactions

length(trans)
## [1] 25888

There are 25,888 transactions recorded in the store for the year 2009-2010. This is almost 71 transactions per day.

4.2 Transaction size

4.2.1 All

transaction_sizes <- size(trans)
hist(transaction_sizes, main = "Histogram of all transaction sizes", xlab = "Transaction Size", ylab = "Frequency")

max(size(trans))
## [1] 672

We observe that the majority of transactions involve fewer than 100 different items. However, there are also a few significantly larger transactions, with the largest one involving 672 different items.

4.2.2 Fewer than 100 items

hist(transaction_sizes[transaction_sizes <= 100], main = "Histogram of transaction sizes smaller than 100 items", xlab = "Transaction Size", ylab = "Frequency")

We observe a chart similar to the one before. Now, we observe the most popular transactions involve less than 30 items.

4.2.3 Fewer than 30 items

hist(transaction_sizes[transaction_sizes <= 30], main = "Histogram of transaction sizes smaller than 30 items", xlab = "Transaction Size", ylab = "Frequency")

Upon closer examination of transactions with fewer than 30 items, it becomes evident that those involving less than 5 items are the most popular.

4.2.4 Fewer than 5 items

hist(transaction_sizes[transaction_sizes <= 5], main = "Histogram of transaction sizes smaller than 5 items", xlab = "Transaction Size", ylab = "Frequency")

It becomes apparent that the most popular transaction involves exactly one item.

5 Association rules

5.1 Apriori algorithm

Firstly, the Apriori algorithm will be used. Our goal is to identify rules that involve at least two products and meet the minimum thresholds for support (1%) and confidence level (25%). This implies that the rules should appear in at least 1% of all transactions and at least 25% of transactions where the antecedent is also the consequent.

transRules <- apriori(trans, parameter = list(support = 0.01, confidence = 0.25, minlen = 2))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.25    0.1    1 none FALSE            TRUE       5    0.01      2
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 258 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[4550 item(s), 25888 transaction(s)] done [0.32s].
## sorting and recoding items ... [562 item(s)] done [0.01s].
## creating transaction tree ... done [0.02s].
## checking subsets of size 1 2 3 done [0.02s].
## writing ... [317 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].

A total of 317 rules were found that meet our criteria.

summary(transRules)
## set of 317 rules
## 
## rule length distribution (lhs + rhs):sizes
##   2   3 
## 305  12 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   2.000   2.000   2.038   2.000   3.000 
## 
## summary of quality measures:
##     support          confidence        coverage            lift       
##  Min.   :0.01000   Min.   :0.2535   Min.   :0.01228   Min.   : 1.925  
##  1st Qu.:0.01116   1st Qu.:0.3307   1st Qu.:0.02565   1st Qu.: 7.702  
##  Median :0.01225   Median :0.3942   Median :0.03314   Median :10.005  
##  Mean   :0.01338   Mean   :0.4239   Mean   :0.03369   Mean   :12.113  
##  3rd Qu.:0.01468   3rd Qu.:0.4861   3rd Qu.:0.04125   3rd Qu.:13.500  
##  Max.   :0.03160   Max.   :0.8616   Max.   :0.06826   Max.   :65.032  
##      count      
##  Min.   :259.0  
##  1st Qu.:289.0  
##  Median :317.0  
##  Mean   :346.3  
##  3rd Qu.:380.0  
##  Max.   :818.0  
## 
## mining info:
##   data ntransactions support confidence
##  trans         25888    0.01       0.25
##                                                                                    call
##  apriori(data = trans, parameter = list(support = 0.01, confidence = 0.25, minlen = 2))
inspect(transRules[1:10])
##      lhs                                      rhs                                      support confidence   coverage     lift count
## [1]  { PAINTED METAL PEARS ASSORTED}       => { ASSORTED COLOUR BIRD ORNAMENT}      0.01089308  0.7324675 0.01487176 13.35361   282
## [2]  { BAKING SET SPACEBOY DESIGN}         => { BAKING SET 9 PIECE RETROSPOT}       0.01031366  0.4890110 0.02109085 10.87587   267
## [3]  { EDWARDIAN PARASOL RED}              => { EDWARDIAN PARASOL BLACK}            0.01039091  0.6481928 0.01603059 28.39326   269
## [4]  { EDWARDIAN PARASOL BLACK}            => { EDWARDIAN PARASOL RED}              0.01039091  0.4551607 0.02282911 28.39326   269
## [5]  { BLUE 3 PIECE MINI DOTS CUTLERY SET} => { PINK 3 PIECE MINI DOTS CUTLERY SET} 0.01158838  0.6185567 0.01873455 35.34922   300
## [6]  { PINK 3 PIECE MINI DOTS CUTLERY SET} => { BLUE 3 PIECE MINI DOTS CUTLERY SET} 0.01158838  0.6622517 0.01749845 35.34922   300
## [7]  { BLUE 3 PIECE MINI DOTS CUTLERY SET} => { RED 3 PIECE MINI DOTS CUTLERY SET}  0.01205192  0.6432990 0.01873455 33.10879   312
## [8]  { RED 3 PIECE MINI DOTS CUTLERY SET}  => { BLUE 3 PIECE MINI DOTS CUTLERY SET} 0.01205192  0.6202783 0.01942985 33.10879   312
## [9]  { BLUE  SPOTTY CUP}                   => { PINK  SPOTTY CUP}                   0.01004326  0.7008086 0.01433096 48.50945   260
## [10] { PINK  SPOTTY CUP}                   => { BLUE  SPOTTY CUP}                   0.01004326  0.6951872 0.01444685 48.50945   260

Here, we can find some overall statistics about the rules, along with the first 10 rules generated by the Apriori algorithm.

5.1.1 Rules with highest lift

inspect(sort(transRules, by = "lift")[1:10])
##      lhs                                   rhs                                   support confidence   coverage     lift count
## [1]  { POPPY'S PLAYHOUSE KITCHEN}       => { POPPY'S PLAYHOUSE BEDROOM}       0.01058405  0.7988338 0.01324938 65.03211   274
## [2]  { POPPY'S PLAYHOUSE BEDROOM}       => { POPPY'S PLAYHOUSE KITCHEN}       0.01058405  0.8616352 0.01228368 65.03211   274
## [3]  { GREEN REGENCY TEACUP AND SAUCER} => { ROSES REGENCY TEACUP AND SAUCER} 0.01116347  0.7506494 0.01487176 49.70028   289
## [4]  { ROSES REGENCY TEACUP AND SAUCER} => { GREEN REGENCY TEACUP AND SAUCER} 0.01116347  0.7391304 0.01510352 49.70028   289
## [5]  { BLUE  SPOTTY CUP}                => { PINK  SPOTTY CUP}                0.01004326  0.7008086 0.01433096 48.50945   260
## [6]  { PINK  SPOTTY CUP}                => { BLUE  SPOTTY CUP}                0.01004326  0.6951872 0.01444685 48.50945   260
## [7]  { COFFEE MUG DOG + BALL DESIGN}    => { COFFEE MUG CAT + BIRD DESIGN}    0.01004326  0.7222222 0.01390606 43.68432   260
## [8]  { COFFEE MUG CAT + BIRD DESIGN}    => { COFFEE MUG DOG + BALL DESIGN}    0.01004326  0.6074766 0.01653276 43.68432   260
## [9]  { SET/6 RED SPOTTY PAPER CUPS}     => { SET/6 RED SPOTTY PAPER PLATES}   0.01220643  0.7281106 0.01676452 39.43374   316
## [10] { SET/6 RED SPOTTY PAPER PLATES}   => { SET/6 RED SPOTTY PAPER CUPS}     0.01220643  0.6610879 0.01846415 39.43374   316

Here, we can observe rules with the highest lift. The lift of all rules in this list is much higher than 1, indicating that products on opposite sides of the rules are complementary. This implies that they tend to occur together and not independently. One of the most notable associations is between POPPY’S PLAYHOUSE KITCHEN and POPPY’S PLAYHOUSE BEDROOM. If a customer purchases POPPY’S PLAYHOUSE KITCHEN, there is a high likelihood that they also bought POPPY’S PLAYHOUSE BEDROOM. Due to the exceptionally high lift, we can assert that if someone has POPPY’S PLAYHOUSE KITCHEN, they are likely to also have POPPY’S PLAYHOUSE BEDROOM. When examining the confidence, we find that in 86% of transactions involving POPPY’S PLAYHOUSE BEDROOM, POPPY’S PLAYHOUSE KITCHEN was also present.

5.1.2 Rules with highest confidence

inspect(sort(transRules, by = "confidence")[1:10])
##      lhs                                      rhs                                   support confidence   coverage     lift count
## [1]  { POPPY'S PLAYHOUSE BEDROOM}          => { POPPY'S PLAYHOUSE KITCHEN}       0.01058405  0.8616352 0.01228368 65.03211   274
## [2]  { SWEETHEART CERAMIC TRINKET BOX,                                                                                          
##        WHITE HANGING HEART T-LIGHT HOLDER} => { STRAWBERRY CERAMIC TRINKET BOX}  0.01104759  0.8436578 0.01309487 12.36028   286
## [3]  { POPPY'S PLAYHOUSE KITCHEN}          => { POPPY'S PLAYHOUSE BEDROOM}       0.01058405  0.7988338 0.01324938 65.03211   274
## [4]  { SWEETHEART CERAMIC TRINKET BOX}     => { STRAWBERRY CERAMIC TRINKET BOX}  0.03159765  0.7827751 0.04036619 11.46830   818
## [5]  { GREEN REGENCY TEACUP AND SAUCER}    => { ROSES REGENCY TEACUP AND SAUCER} 0.01116347  0.7506494 0.01487176 49.70028   289
## [6]  { TOILET METAL SIGN}                  => { BATHROOM METAL SIGN}             0.01518078  0.7485714 0.02027967 22.53374   393
## [7]  { KITCHEN METAL SIGN}                 => { BATHROOM METAL SIGN}             0.01182015  0.7445255 0.01587608 22.41195   306
## [8]  { ROSES REGENCY TEACUP AND SAUCER}    => { GREEN REGENCY TEACUP AND SAUCER} 0.01116347  0.7391304 0.01510352 49.70028   289
## [9]  { PAINTED METAL PEARS ASSORTED}       => { ASSORTED COLOUR BIRD ORNAMENT}   0.01089308  0.7324675 0.01487176 13.35361   282
## [10] { SET/6 RED SPOTTY PAPER CUPS}        => { SET/6 RED SPOTTY PAPER PLATES}   0.01220643  0.7281106 0.01676452 39.43374   316

Here we can see rules with the highest confidence. The top rule is once again the association between POPPY’S PLAYHOUSE BEDROOM and KITCHEN. The second notable rule is {SWEETHEART CERAMIC TRINKET BOX, WHITE HANGING HEART T-LIGHT HOLDER} => {STRAWBERRY CERAMIC TRINKET BOX} with an 84% confidence level. This observation reinforces the pattern that customers, likely wholesalers, often purchase products within the same category.

5.1.3 Rules with highest support

inspect(sort(transRules, by = "support")[1:10])
##      lhs                                      rhs                                      support confidence   coverage      lift count
## [1]  { SWEETHEART CERAMIC TRINKET BOX}     => { STRAWBERRY CERAMIC TRINKET BOX}     0.03159765  0.7827751 0.04036619 11.468298   818
## [2]  { STRAWBERRY CERAMIC TRINKET BOX}     => { SWEETHEART CERAMIC TRINKET BOX}     0.03159765  0.4629315 0.06825556 11.468298   818
## [3]  { RED HANGING HEART T-LIGHT HOLDER}   => { WHITE HANGING HEART T-LIGHT HOLDER} 0.03012979  0.7169118 0.04202719  5.444239   780
## [4]  { WOODEN PICTURE FRAME WHITE FINISH}  => { WOODEN FRAME ANTIQUE WHITE}         0.02352441  0.6350365 0.03704419 14.160056   609
## [5]  { WOODEN FRAME ANTIQUE WHITE}         => { WOODEN PICTURE FRAME WHITE FINISH}  0.02352441  0.5245478 0.04484703 14.160056   609
## [6]  { PACK OF 72 RETRO SPOT CAKE CASES}   => { 60 TEATIME FAIRY CAKE CASES}        0.02333127  0.4256519 0.05481304  8.211085   604
## [7]  { 60 TEATIME FAIRY CAKE CASES}        => { PACK OF 72 RETRO SPOT CAKE CASES}   0.02333127  0.4500745 0.05183869  8.211085   604
## [8]  { PACK OF 60 PINK PAISLEY CAKE CASES} => { 60 TEATIME FAIRY CAKE CASES}        0.02298362  0.5129310 0.04480841  9.894753   595
## [9]  { 60 TEATIME FAIRY CAKE CASES}        => { PACK OF 60 PINK PAISLEY CAKE CASES} 0.02298362  0.4433681 0.05183869  9.894753   595
## [10] { LOVE BUILDING BLOCK WORD}           => { HOME BUILDING BLOCK WORD}           0.02267460  0.5321850 0.04260661 10.273828   587

Here, we can find rules with the highest support, indicating the most frequently occurring rules. Notably, the most popular combination is the association between STRAWBERRY CERAMIC TRINKET BOX and SWEETHEART CERAMIC TRINKET BOX.

5.1.4 Products leading to retro spot cake cases

rules.cakeCases<-apriori(data=trans, parameter=list(supp=0.01,conf = 0.01), 
                       appearance=list(default="lhs", rhs=" PACK OF 72 RETRO SPOT CAKE CASES"), control=list(verbose=F)) 
rules.cakeCases.byconf<-sort(rules.cakeCases, by="confidence", decreasing=TRUE)
inspect(head(rules.cakeCases.byconf))
##     lhs                                       rhs                                    support confidence   coverage      lift count
## [1] { SET OF 72 RETRO SPOT PAPER  DOILIES} => { PACK OF 72 RETRO SPOT CAKE CASES} 0.01266996  0.5878136 0.02155439 10.723974   328
## [2] { 60 TEATIME FAIRY CAKE CASES,                                                                                                
##       PACK OF 60 PINK PAISLEY CAKE CASES}  => { PACK OF 72 RETRO SPOT CAKE CASES} 0.01220643  0.5310924 0.02298362  9.689162   316
## [3] { PACK OF 60 DINOSAUR CAKE CASES}      => { PACK OF 72 RETRO SPOT CAKE CASES} 0.01340389  0.4846369 0.02765760  8.841634   347
## [4] { PACK OF 60 PINK PAISLEY CAKE CASES}  => { PACK OF 72 RETRO SPOT CAKE CASES} 0.02062732  0.4603448 0.04480841  8.398454   534
## [5] { 60 TEATIME FAIRY CAKE CASES}         => { PACK OF 72 RETRO SPOT CAKE CASES} 0.02333127  0.4500745 0.05183869  8.211085   604
## [6] { 72 SWEETHEART FAIRY CAKE CASES}      => { PACK OF 72 RETRO SPOT CAKE CASES} 0.01510352  0.4473684 0.03376082  8.161715   391

In our findings, it appears that customers who purchase any variant of CAKE CASES are also likely to buy the PACK OF 72 RETRO SPOT CAKE CASES. Interestingly, the most confident rule does not involve cake cases but paper doilies.

5.1.5 Products to which retro spot cake cases lead

rules.cakeCasesopp<-apriori(data=trans, parameter=list(supp=0.01,conf = 0.005), 
                          appearance=list(default="rhs", lhs=" PACK OF 72 RETRO SPOT CAKE CASES"), control=list(verbose=F)) 
rules.cakeCasesopp.byconf<-sort(rules.cakeCasesopp, by="confidence", decreasing=TRUE)
inspect(head(rules.cakeCasesopp.byconf))
##     lhs                                    rhs                                      support confidence   coverage     lift count
## [1] { PACK OF 72 RETRO SPOT CAKE CASES} => { 60 TEATIME FAIRY CAKE CASES}        0.02333127  0.4256519 0.05481304 8.211085   604
## [2] { PACK OF 72 RETRO SPOT CAKE CASES} => { PACK OF 60 PINK PAISLEY CAKE CASES} 0.02062732  0.3763214 0.05481304 8.398454   534
## [3] { PACK OF 72 RETRO SPOT CAKE CASES} => { 72 SWEETHEART FAIRY CAKE CASES}     0.01510352  0.2755462 0.05481304 8.161715   391
## [4] { PACK OF 72 RETRO SPOT CAKE CASES} => { WHITE HANGING HEART T-LIGHT HOLDER} 0.01436959  0.2621564 0.05481304 1.990820   372
## [5] { PACK OF 72 RETRO SPOT CAKE CASES} => { SET/20 RED SPOTTY PAPER NAPKINS}    0.01433096  0.2614517 0.05481304 7.124697   371
## [6] { PACK OF 72 RETRO SPOT CAKE CASES} => { PACK OF 72 SKULL CAKE CASES}        0.01425371  0.2600423 0.05481304 8.004726   369

Furthermore, customers who purchase PACK OF 72 RETRO SPOT CAKE CASES are frequently inclined to buy other types of cake cases as well. However, there is one exception in this pattern, involving the purchase of WHITE HANGING HEART T-LIGHT HOLDER. The confidence of this rule is 26%, that means that in 26% of instances where retro spot cake cases are bought, a simultaneous purchase of the heart t-light holder occurs.

5.1.6 Rules plots

plot(transRules)

In this chart, we have presented all rules, with the strongest ones located in the upper right corner. We can easy identify them.

inspect(apriori(trans, parameter = list(support = 0.03, confidence = 0.6, minlen = 2)))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.6    0.1    1 none FALSE            TRUE       5    0.03      2
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 776 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[4550 item(s), 25888 transaction(s)] done [0.43s].
## sorting and recoding items ... [63 item(s)] done [0.00s].
## creating transaction tree ... done [0.01s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [2 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
##     lhs                                    rhs                                      support confidence   coverage      lift count
## [1] { RED HANGING HEART T-LIGHT HOLDER} => { WHITE HANGING HEART T-LIGHT HOLDER} 0.03012979  0.7169118 0.04202719  5.444239   780
## [2] { SWEETHEART CERAMIC TRINKET BOX}   => { STRAWBERRY CERAMIC TRINKET BOX}     0.03159765  0.7827751 0.04036619 11.468298   818

There are { RED HANGING HEART T-LIGHT HOLDER} => { WHITE HANGING HEART T-LIGHT HOLDER} and { SWEETHEART CERAMIC TRINKET BOX} => { STRAWBERRY CERAMIC TRINKET BOX}. These rules appear over 750 times, each with a confidence level exceeding 70%, making them highly significant.

plot(transRules, method="graph")

We can see in this somewhat complicated chart that our rules do not form one coherent group when customers are retailers. Instead, we can distinguish small groups related to specific product categories. For example, in the upper left corner there are rules related to small home accessories, while in the middle right corner there are associations related to cake molds and kitchen accessories.

plot(rules.cakeCases, method="graph")

In this chart we can observe products that often lead to the purchase of PACK OF 72 RETRO SPOT CAKE CASES. While mostly wholesalers buy cake cases, we also notice the inclusion of some kitchen accessories such as glass bowls, paper napkins, or a lunch bag. Undoubtedly, all these items belong to the same category: kitchen.

5.2 Eclast algorithm

freq.items<-eclat(trans, parameter=list(supp=0.01, maxlen=15)) 
## Eclat
## 
## parameter specification:
##  tidLists support minlen maxlen            target  ext
##     FALSE    0.01      1     15 frequent itemsets TRUE
## 
## algorithmic control:
##  sparse sort verbose
##       7   -2    TRUE
## 
## Absolute minimum support count: 258 
## 
## create itemset ... 
## set transactions ...[4550 item(s), 25888 transaction(s)] done [0.24s].
## sorting and recoding items ... [562 item(s)] done [0.01s].
## creating sparse bit matrix ... [562 row(s), 25888 column(s)] done [0.00s].
## writing  ... [765 set(s)] done [0.55s].
## Creating S4 object  ... done [0.00s].
freq.rules<-ruleInduction(freq.items, trans, confidence=0.25)
freq.rules
## set of 317 rules
inspect(sort(freq.rules, by = "lift")[1:10])
##      lhs                                   rhs                                   support confidence     lift itemset
## [1]  { POPPY'S PLAYHOUSE BEDROOM}       => { POPPY'S PLAYHOUSE KITCHEN}       0.01058405  0.8616352 65.03211       9
## [2]  { POPPY'S PLAYHOUSE KITCHEN}       => { POPPY'S PLAYHOUSE BEDROOM}       0.01058405  0.7988338 65.03211       9
## [3]  { ROSES REGENCY TEACUP AND SAUCER} => { GREEN REGENCY TEACUP AND SAUCER} 0.01116347  0.7391304 49.70028      20
## [4]  { GREEN REGENCY TEACUP AND SAUCER} => { ROSES REGENCY TEACUP AND SAUCER} 0.01116347  0.7506494 49.70028      20
## [5]  { PINK  SPOTTY CUP}                => { BLUE  SPOTTY CUP}                0.01004326  0.6951872 48.50945       6
## [6]  { BLUE  SPOTTY CUP}                => { PINK  SPOTTY CUP}                0.01004326  0.7008086 48.50945       6
## [7]  { COFFEE MUG DOG + BALL DESIGN}    => { COFFEE MUG CAT + BIRD DESIGN}    0.01004326  0.7222222 43.68432       7
## [8]  { COFFEE MUG CAT + BIRD DESIGN}    => { COFFEE MUG DOG + BALL DESIGN}    0.01004326  0.6074766 43.68432       7
## [9]  { SET/6 RED SPOTTY PAPER PLATES}   => { SET/6 RED SPOTTY PAPER CUPS}     0.01220643  0.6610879 39.43374       8
## [10] { SET/6 RED SPOTTY PAPER CUPS}     => { SET/6 RED SPOTTY PAPER PLATES}   0.01220643  0.7281106 39.43374       8

The Eclat algorithm demonstrated faster computational time with the same parameters, providing the same result but in a significantly shorter duration.

6 Analyzing behaviour of exact client

In this section, we will attempt to determine whether clients are genuine wholesalers. We will verify if a randomly selected client indeed purchases products from a specific category.

x <- read_xlsx("online_retail_II.xlsx")
x<-x[x$`Customer ID` == 17850,]
x <- x[!(is.na(x$Invoice) | is.na(x$Description)), ]
write.table(x, file = "customer_basket.csv", row.names=FALSE,sep = ", ",quote = FALSE ,col.names = FALSE)
customerTrans = read.transactions("customer_basket.csv", format = "single",cols=c(1,3), sep=",",quote = "")
length(customerTrans)
## [1] 158

We’ll focus on the client with ID 17850 as they is the one of clients who have conducted the highest number of transactions, totaling 158 purchases.

itemFrequencyPlot(customerTrans, topN = 10, cex.names = 0.65)

We observe that this client frequently purchases office equipment, including photo frames, cabinets and mug. This confirms our thesis that customers mostly buy products from one category.

customerTransRules <- apriori(customerTrans, parameter = list(support = 0.5, confidence = 0.25, minlen = 2))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.25    0.1    1 none FALSE            TRUE       5     0.5      2
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 79 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[105 item(s), 158 transaction(s)] done [0.00s].
## sorting and recoding items ... [4 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [28 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].

The apriori algorithm was applied to analyze his transactions. Due to the frequent purchase of the same products, the support was increased level to 50%. Lower support levels resulted in too many rules. Only the highest confidence rules will be analyzed.

inspect(sort(customerTransRules, by = "confidence")[1:10])
##      lhs                                       rhs                                     support confidence  coverage     lift count
## [1]  { WOODEN PICTURE FRAME WHITE FINISH}   => { WOODEN FRAME ANTIQUE WHITE}         0.6772152  1.0000000 0.6772152 1.410714   107
## [2]  { WOOD 2 DRAWER CABINET WHITE FINISH,                                                                                        
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOOD S/3 CABINET ANT WHITE FINISH}  0.6582278  1.0000000 0.6582278 1.436364   104
## [3]  { WOOD S/3 CABINET ANT WHITE FINISH,                                                                                         
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOOD 2 DRAWER CABINET WHITE FINISH} 0.6582278  1.0000000 0.6582278 1.476636   104
## [4]  { WOOD 2 DRAWER CABINET WHITE FINISH,                                                                                        
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOODEN FRAME ANTIQUE WHITE}         0.6582278  1.0000000 0.6582278 1.410714   104
## [5]  { WOOD 2 DRAWER CABINET WHITE FINISH,                                                                                        
##        WOODEN FRAME ANTIQUE WHITE}          => { WOOD S/3 CABINET ANT WHITE FINISH}  0.6645570  1.0000000 0.6645570 1.436364   105
## [6]  { WOOD S/3 CABINET ANT WHITE FINISH,                                                                                         
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOODEN FRAME ANTIQUE WHITE}         0.6582278  1.0000000 0.6582278 1.410714   104
## [7]  { WOOD 2 DRAWER CABINET WHITE FINISH,                                                                                        
##        WOOD S/3 CABINET ANT WHITE FINISH,                                                                                         
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOODEN FRAME ANTIQUE WHITE}         0.6582278  1.0000000 0.6582278 1.410714   104
## [8]  { WOOD 2 DRAWER CABINET WHITE FINISH,                                                                                        
##        WOODEN FRAME ANTIQUE WHITE,                                                                                                
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOOD S/3 CABINET ANT WHITE FINISH}  0.6582278  1.0000000 0.6582278 1.436364   104
## [9]  { WOOD S/3 CABINET ANT WHITE FINISH,                                                                                         
##        WOODEN FRAME ANTIQUE WHITE,                                                                                                
##        WOODEN PICTURE FRAME WHITE FINISH}   => { WOOD 2 DRAWER CABINET WHITE FINISH} 0.6582278  1.0000000 0.6582278 1.476636   104
## [10] { WOOD S/3 CABINET ANT WHITE FINISH}   => { WOODEN FRAME ANTIQUE WHITE}         0.6898734  0.9909091 0.6962025 1.397890   109

We can predict purchases by these clients with almost 100% confidence. We see that if this client bought the WOODEN PICTURE FRAME WHITE FINISH, they always bought the WOODEN FRAME ANTIQUE WHITE. This set appeared in almost 68% of their purchases. We can also observe other rules related to photo frames and drawer cabinets with a confidence level of 1. This allows us run a recommendation system which is almost 100% accurate. If our buyer adds one product to the cart, we can immediately suggest another product that will perfectly match their needs.

7 Conclusions

  1. Association rules provide insights into the relationships between products.
  2. Thanks to association rules, we can observe that customers are wholesalers. When they buy one item, they frequently purchase the same item in a different colour or shape.
  3. The most popular item was the heart t-light holder.
  4. Both the Apriori algorithm and Eclat produced identical results with the same input values, but Eclat proved to be faster.
  5. Using association rules, we can predict the purchases of specific clients. With a sufficient purchase history for a client, we can employ association rules to anticipate their shopping cart and implement a recommendation system.