Introduction

Association rule mining finds interesting associations and relationships among large sets of data items. This rule shows how frequently a item-set occurs in a transaction. A typical example is Market Based Analysis.

Market Based Analysis is one of the key techniques used by large relations to show associations between items.It allows retailers to identify relationships between the items that people buy together frequently. Given a set of transactions, we can find rules that will predict the occurrence of an item based on the occurrences of other items in the transaction.

The main purpose of the project is to find the relationship between the items of provided data-set “BreadBasket”.

Dataset Processing

“arules” package provides the infrastructure for representing, manipulating, and analyzing transaction data and patterns.

“arulesviz” package is used for visualizing Association Rules and Frequent Itemsets. It extends the package ‘arules’ with various visualization techniques for association rules and itemsets. The package also includes several interactive visualizations for rule exploration. “RColorBrewer” is a ColorBrewer Palette which provides color schemes for maps and other graphics.

library(arules)
## Warning: package 'arules' was built under R version 4.1.2
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Warning: package 'arulesViz' was built under R version 4.1.2
library(RColorBrewer)

Dataset Summary

association_data <- read.csv("C:\\Users\\HP\\Desktop\\Study\\Unsupervised\\BreadBasket.csv",header = F, colClasses = "factor")
head(association_data)
##            V1         V2            V3
## 1 Transaction       Date          Item
## 2           1 30/10/2016         Bread
## 3           2 30/10/2016  Scandinavian
## 4           2 30/10/2016  Scandinavian
## 5           3 30/10/2016 Hot chocolate
## 6           3 30/10/2016           Jam

Rules

To create rules we make use of the apriori algorithm.All association rules have an antecedent(if) and what is known as a consequent (then). An antecedent is the item that is found in the data and the consequence is the item that is found in combination with the antecedent.

To assess the quality of the rules we use 3 indicators namely Support, Confidence and Lift.

Support indicates the number of times an item appears in the data-set. Confidence indicates the validity of the rule. Lift shows the correlation of the items.

transactions_data<-read.transactions("C:\\Users\\HP\\Desktop\\Study\\Unsupervised\\BreadBasket.csv", format = "single", sep=",", cols = c(2,3))
summary(transactions_data)
## transactions as itemMatrix in sparse format with
##  47 rows (elements/itemsets/transactions) and
##  76 columns (items) and a density of 0.2816349 
## 
## most frequent items:
##   Bread     Tea  Coffee    NONE  Pastry (Other) 
##      46      46      45      45      43     781 
## 
## element (itemset/transaction) length distribution:
## sizes
##  1  7 17 18 19 20 21 22 23 24 25 26 28 29 30 
##  1  1  5  6  3  3  3  4  3  6  1  7  1  1  2 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0    18.0    22.0    21.4    24.5    30.0 
## 
## includes extended item information - examples:
##                     labels
## 1               Adjustment
## 2 Afternoon with the baker
## 3                Alfajores
## 
## includes extended transaction information - examples:
##   transactionID
## 1    01/11/2016
## 2    02/11/2016
## 3    03/11/2016

Applying apriori() function

apriori()‘ function is in-built in R to mine frequent item-sets and association rules using the Apriori algorithm. Here, ‘association_data’ is the transaction data. ‘parameter’ is a named list that specifies the minimum support and confidence for finding the association rules. inspect() function prints the internal representation of an R object or the result of an expression.

Support: Support is an indication of how frequently the item-set appears in the data-set. It is the count of records containing an item ‘x’ divided by the total number of records in the database.

Confidence: Confidence is a measure of times such that if an item ‘x’ is bought, then item ‘y’ is also bought together. It is the support count of (x U y) divided by the support count of ‘x’.

Lift: Lift is the ratio of the observed support to that which is expected if ‘x’ and ‘y’ were independent. It is the support count of (x U y) divided by the product of individual support counts of ‘x’ and ‘y’.

Applying itemFrequencyPlot() function

itemFrequencyPlot() creates a bar plot for item frequencies/ support. It creates an item frequency bar plot for inspecting the distribution of objects based on the transactions. The items are plotted ordered by descending support. Here, ‘topN=20’ means that 20 items with the highest item frequency/ lift will be plotted.

Now by using itemFrequencyPlot I can check that most frequent items in my data set are bread, tea,coffee, sandwiches and Pastry as shown in below graph.

itemFrequencyPlot(transactions_data, topN=30, type="relative",col = brewer.pal(10, 'Paired'),weighted = FALSE, main="Relative Item Frequency Plot") 

rules<-apriori(transactions_data, parameter=list(supp=0.9, conf=0.8))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5     0.9      1
##  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: 42 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[76 item(s), 47 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [72 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(rules)
##      lhs                          rhs        support   confidence coverage 
## [1]  {}                        => {Pastry}   0.9148936 0.9148936  1.0000000
## [2]  {}                        => {Sandwich} 0.9148936 0.9148936  1.0000000
## [3]  {}                        => {NONE}     0.9574468 0.9574468  1.0000000
## [4]  {}                        => {Coffee}   0.9574468 0.9574468  1.0000000
## [5]  {}                        => {Bread}    0.9787234 0.9787234  1.0000000
## [6]  {}                        => {Tea}      0.9787234 0.9787234  1.0000000
## [7]  {Pastry}                  => {Coffee}   0.9148936 1.0000000  0.9148936
## [8]  {Coffee}                  => {Pastry}   0.9148936 0.9555556  0.9574468
## [9]  {Pastry}                  => {Bread}    0.9148936 1.0000000  0.9148936
## [10] {Bread}                   => {Pastry}   0.9148936 0.9347826  0.9787234
## [11] {Pastry}                  => {Tea}      0.9148936 1.0000000  0.9148936
## [12] {Tea}                     => {Pastry}   0.9148936 0.9347826  0.9787234
## [13] {Sandwich}                => {Coffee}   0.9148936 1.0000000  0.9148936
## [14] {Coffee}                  => {Sandwich} 0.9148936 0.9555556  0.9574468
## [15] {Sandwich}                => {Bread}    0.9148936 1.0000000  0.9148936
## [16] {Bread}                   => {Sandwich} 0.9148936 0.9347826  0.9787234
## [17] {Sandwich}                => {Tea}      0.9148936 1.0000000  0.9148936
## [18] {Tea}                     => {Sandwich} 0.9148936 0.9347826  0.9787234
## [19] {NONE}                    => {Coffee}   0.9361702 0.9777778  0.9574468
## [20] {Coffee}                  => {NONE}     0.9361702 0.9777778  0.9574468
## [21] {NONE}                    => {Bread}    0.9574468 1.0000000  0.9574468
## [22] {Bread}                   => {NONE}     0.9574468 0.9782609  0.9787234
## [23] {NONE}                    => {Tea}      0.9574468 1.0000000  0.9574468
## [24] {Tea}                     => {NONE}     0.9574468 0.9782609  0.9787234
## [25] {Coffee}                  => {Bread}    0.9574468 1.0000000  0.9574468
## [26] {Bread}                   => {Coffee}   0.9574468 0.9782609  0.9787234
## [27] {Coffee}                  => {Tea}      0.9574468 1.0000000  0.9574468
## [28] {Tea}                     => {Coffee}   0.9574468 0.9782609  0.9787234
## [29] {Bread}                   => {Tea}      0.9787234 1.0000000  0.9787234
## [30] {Tea}                     => {Bread}    0.9787234 1.0000000  0.9787234
## [31] {Coffee, Pastry}          => {Bread}    0.9148936 1.0000000  0.9148936
## [32] {Bread, Pastry}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [33] {Bread, Coffee}           => {Pastry}   0.9148936 0.9555556  0.9574468
## [34] {Coffee, Pastry}          => {Tea}      0.9148936 1.0000000  0.9148936
## [35] {Pastry, Tea}             => {Coffee}   0.9148936 1.0000000  0.9148936
## [36] {Coffee, Tea}             => {Pastry}   0.9148936 0.9555556  0.9574468
## [37] {Bread, Pastry}           => {Tea}      0.9148936 1.0000000  0.9148936
## [38] {Pastry, Tea}             => {Bread}    0.9148936 1.0000000  0.9148936
## [39] {Bread, Tea}              => {Pastry}   0.9148936 0.9347826  0.9787234
## [40] {Coffee, Sandwich}        => {Bread}    0.9148936 1.0000000  0.9148936
## [41] {Bread, Sandwich}         => {Coffee}   0.9148936 1.0000000  0.9148936
## [42] {Bread, Coffee}           => {Sandwich} 0.9148936 0.9555556  0.9574468
## [43] {Coffee, Sandwich}        => {Tea}      0.9148936 1.0000000  0.9148936
## [44] {Sandwich, Tea}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [45] {Coffee, Tea}             => {Sandwich} 0.9148936 0.9555556  0.9574468
## [46] {Bread, Sandwich}         => {Tea}      0.9148936 1.0000000  0.9148936
## [47] {Sandwich, Tea}           => {Bread}    0.9148936 1.0000000  0.9148936
## [48] {Bread, Tea}              => {Sandwich} 0.9148936 0.9347826  0.9787234
## [49] {Coffee, NONE}            => {Bread}    0.9361702 1.0000000  0.9361702
## [50] {Bread, NONE}             => {Coffee}   0.9361702 0.9777778  0.9574468
## [51] {Bread, Coffee}           => {NONE}     0.9361702 0.9777778  0.9574468
## [52] {Coffee, NONE}            => {Tea}      0.9361702 1.0000000  0.9361702
## [53] {NONE, Tea}               => {Coffee}   0.9361702 0.9777778  0.9574468
## [54] {Coffee, Tea}             => {NONE}     0.9361702 0.9777778  0.9574468
## [55] {Bread, NONE}             => {Tea}      0.9574468 1.0000000  0.9574468
## [56] {NONE, Tea}               => {Bread}    0.9574468 1.0000000  0.9574468
## [57] {Bread, Tea}              => {NONE}     0.9574468 0.9782609  0.9787234
## [58] {Bread, Coffee}           => {Tea}      0.9574468 1.0000000  0.9574468
## [59] {Coffee, Tea}             => {Bread}    0.9574468 1.0000000  0.9574468
## [60] {Bread, Tea}              => {Coffee}   0.9574468 0.9782609  0.9787234
## [61] {Bread, Coffee, Pastry}   => {Tea}      0.9148936 1.0000000  0.9148936
## [62] {Coffee, Pastry, Tea}     => {Bread}    0.9148936 1.0000000  0.9148936
## [63] {Bread, Pastry, Tea}      => {Coffee}   0.9148936 1.0000000  0.9148936
## [64] {Bread, Coffee, Tea}      => {Pastry}   0.9148936 0.9555556  0.9574468
## [65] {Bread, Coffee, Sandwich} => {Tea}      0.9148936 1.0000000  0.9148936
## [66] {Coffee, Sandwich, Tea}   => {Bread}    0.9148936 1.0000000  0.9148936
## [67] {Bread, Sandwich, Tea}    => {Coffee}   0.9148936 1.0000000  0.9148936
## [68] {Bread, Coffee, Tea}      => {Sandwich} 0.9148936 0.9555556  0.9574468
## [69] {Bread, Coffee, NONE}     => {Tea}      0.9361702 1.0000000  0.9361702
## [70] {Coffee, NONE, Tea}       => {Bread}    0.9361702 1.0000000  0.9361702
## [71] {Bread, NONE, Tea}        => {Coffee}   0.9361702 0.9777778  0.9574468
## [72] {Bread, Coffee, Tea}      => {NONE}     0.9361702 0.9777778  0.9574468
##      lift     count
## [1]  1.000000 43   
## [2]  1.000000 43   
## [3]  1.000000 45   
## [4]  1.000000 45   
## [5]  1.000000 46   
## [6]  1.000000 46   
## [7]  1.044444 43   
## [8]  1.044444 43   
## [9]  1.021739 43   
## [10] 1.021739 43   
## [11] 1.021739 43   
## [12] 1.021739 43   
## [13] 1.044444 43   
## [14] 1.044444 43   
## [15] 1.021739 43   
## [16] 1.021739 43   
## [17] 1.021739 43   
## [18] 1.021739 43   
## [19] 1.021235 44   
## [20] 1.021235 44   
## [21] 1.021739 45   
## [22] 1.021739 45   
## [23] 1.021739 45   
## [24] 1.021739 45   
## [25] 1.021739 45   
## [26] 1.021739 45   
## [27] 1.021739 45   
## [28] 1.021739 45   
## [29] 1.021739 46   
## [30] 1.021739 46   
## [31] 1.021739 43   
## [32] 1.044444 43   
## [33] 1.044444 43   
## [34] 1.021739 43   
## [35] 1.044444 43   
## [36] 1.044444 43   
## [37] 1.021739 43   
## [38] 1.021739 43   
## [39] 1.021739 43   
## [40] 1.021739 43   
## [41] 1.044444 43   
## [42] 1.044444 43   
## [43] 1.021739 43   
## [44] 1.044444 43   
## [45] 1.044444 43   
## [46] 1.021739 43   
## [47] 1.021739 43   
## [48] 1.021739 43   
## [49] 1.021739 44   
## [50] 1.021235 44   
## [51] 1.021235 44   
## [52] 1.021739 44   
## [53] 1.021235 44   
## [54] 1.021235 44   
## [55] 1.021739 45   
## [56] 1.021739 45   
## [57] 1.021739 45   
## [58] 1.021739 45   
## [59] 1.021739 45   
## [60] 1.021739 45   
## [61] 1.021739 43   
## [62] 1.021739 43   
## [63] 1.044444 43   
## [64] 1.044444 43   
## [65] 1.021739 43   
## [66] 1.021739 43   
## [67] 1.044444 43   
## [68] 1.044444 43   
## [69] 1.021739 44   
## [70] 1.021739 44   
## [71] 1.021235 44   
## [72] 1.021235 44
rules_by_confidence<-sort(rules, by="confidence", decreasing=TRUE) 
inspect(head(rules_by_confidence))
##     lhs           rhs      support   confidence coverage  lift     count
## [1] {Pastry}   => {Coffee} 0.9148936 1          0.9148936 1.044444 43   
## [2] {Pastry}   => {Bread}  0.9148936 1          0.9148936 1.021739 43   
## [3] {Pastry}   => {Tea}    0.9148936 1          0.9148936 1.021739 43   
## [4] {Sandwich} => {Coffee} 0.9148936 1          0.9148936 1.044444 43   
## [5] {Sandwich} => {Bread}  0.9148936 1          0.9148936 1.021739 43   
## [6] {Sandwich} => {Tea}    0.9148936 1          0.9148936 1.021739 43
inspect(head(rules, n = 100, by = "confidence"))
##      lhs                          rhs        support   confidence coverage 
## [1]  {Pastry}                  => {Coffee}   0.9148936 1.0000000  0.9148936
## [2]  {Pastry}                  => {Bread}    0.9148936 1.0000000  0.9148936
## [3]  {Pastry}                  => {Tea}      0.9148936 1.0000000  0.9148936
## [4]  {Sandwich}                => {Coffee}   0.9148936 1.0000000  0.9148936
## [5]  {Sandwich}                => {Bread}    0.9148936 1.0000000  0.9148936
## [6]  {Sandwich}                => {Tea}      0.9148936 1.0000000  0.9148936
## [7]  {NONE}                    => {Bread}    0.9574468 1.0000000  0.9574468
## [8]  {NONE}                    => {Tea}      0.9574468 1.0000000  0.9574468
## [9]  {Coffee}                  => {Bread}    0.9574468 1.0000000  0.9574468
## [10] {Coffee}                  => {Tea}      0.9574468 1.0000000  0.9574468
## [11] {Bread}                   => {Tea}      0.9787234 1.0000000  0.9787234
## [12] {Tea}                     => {Bread}    0.9787234 1.0000000  0.9787234
## [13] {Coffee, Pastry}          => {Bread}    0.9148936 1.0000000  0.9148936
## [14] {Bread, Pastry}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [15] {Coffee, Pastry}          => {Tea}      0.9148936 1.0000000  0.9148936
## [16] {Pastry, Tea}             => {Coffee}   0.9148936 1.0000000  0.9148936
## [17] {Bread, Pastry}           => {Tea}      0.9148936 1.0000000  0.9148936
## [18] {Pastry, Tea}             => {Bread}    0.9148936 1.0000000  0.9148936
## [19] {Coffee, Sandwich}        => {Bread}    0.9148936 1.0000000  0.9148936
## [20] {Bread, Sandwich}         => {Coffee}   0.9148936 1.0000000  0.9148936
## [21] {Coffee, Sandwich}        => {Tea}      0.9148936 1.0000000  0.9148936
## [22] {Sandwich, Tea}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [23] {Bread, Sandwich}         => {Tea}      0.9148936 1.0000000  0.9148936
## [24] {Sandwich, Tea}           => {Bread}    0.9148936 1.0000000  0.9148936
## [25] {Coffee, NONE}            => {Bread}    0.9361702 1.0000000  0.9361702
## [26] {Coffee, NONE}            => {Tea}      0.9361702 1.0000000  0.9361702
## [27] {Bread, NONE}             => {Tea}      0.9574468 1.0000000  0.9574468
## [28] {NONE, Tea}               => {Bread}    0.9574468 1.0000000  0.9574468
## [29] {Bread, Coffee}           => {Tea}      0.9574468 1.0000000  0.9574468
## [30] {Coffee, Tea}             => {Bread}    0.9574468 1.0000000  0.9574468
## [31] {Bread, Coffee, Pastry}   => {Tea}      0.9148936 1.0000000  0.9148936
## [32] {Coffee, Pastry, Tea}     => {Bread}    0.9148936 1.0000000  0.9148936
## [33] {Bread, Pastry, Tea}      => {Coffee}   0.9148936 1.0000000  0.9148936
## [34] {Bread, Coffee, Sandwich} => {Tea}      0.9148936 1.0000000  0.9148936
## [35] {Coffee, Sandwich, Tea}   => {Bread}    0.9148936 1.0000000  0.9148936
## [36] {Bread, Sandwich, Tea}    => {Coffee}   0.9148936 1.0000000  0.9148936
## [37] {Bread, Coffee, NONE}     => {Tea}      0.9361702 1.0000000  0.9361702
## [38] {Coffee, NONE, Tea}       => {Bread}    0.9361702 1.0000000  0.9361702
## [39] {}                        => {Bread}    0.9787234 0.9787234  1.0000000
## [40] {}                        => {Tea}      0.9787234 0.9787234  1.0000000
## [41] {Bread}                   => {NONE}     0.9574468 0.9782609  0.9787234
## [42] {Tea}                     => {NONE}     0.9574468 0.9782609  0.9787234
## [43] {Bread}                   => {Coffee}   0.9574468 0.9782609  0.9787234
## [44] {Tea}                     => {Coffee}   0.9574468 0.9782609  0.9787234
## [45] {Bread, Tea}              => {NONE}     0.9574468 0.9782609  0.9787234
## [46] {Bread, Tea}              => {Coffee}   0.9574468 0.9782609  0.9787234
## [47] {NONE}                    => {Coffee}   0.9361702 0.9777778  0.9574468
## [48] {Coffee}                  => {NONE}     0.9361702 0.9777778  0.9574468
## [49] {Bread, NONE}             => {Coffee}   0.9361702 0.9777778  0.9574468
## [50] {Bread, Coffee}           => {NONE}     0.9361702 0.9777778  0.9574468
## [51] {NONE, Tea}               => {Coffee}   0.9361702 0.9777778  0.9574468
## [52] {Coffee, Tea}             => {NONE}     0.9361702 0.9777778  0.9574468
## [53] {Bread, NONE, Tea}        => {Coffee}   0.9361702 0.9777778  0.9574468
## [54] {Bread, Coffee, Tea}      => {NONE}     0.9361702 0.9777778  0.9574468
## [55] {}                        => {NONE}     0.9574468 0.9574468  1.0000000
## [56] {}                        => {Coffee}   0.9574468 0.9574468  1.0000000
## [57] {Coffee}                  => {Pastry}   0.9148936 0.9555556  0.9574468
## [58] {Coffee}                  => {Sandwich} 0.9148936 0.9555556  0.9574468
## [59] {Bread, Coffee}           => {Pastry}   0.9148936 0.9555556  0.9574468
## [60] {Coffee, Tea}             => {Pastry}   0.9148936 0.9555556  0.9574468
## [61] {Bread, Coffee}           => {Sandwich} 0.9148936 0.9555556  0.9574468
## [62] {Coffee, Tea}             => {Sandwich} 0.9148936 0.9555556  0.9574468
## [63] {Bread, Coffee, Tea}      => {Pastry}   0.9148936 0.9555556  0.9574468
## [64] {Bread, Coffee, Tea}      => {Sandwich} 0.9148936 0.9555556  0.9574468
## [65] {Bread}                   => {Pastry}   0.9148936 0.9347826  0.9787234
## [66] {Tea}                     => {Pastry}   0.9148936 0.9347826  0.9787234
## [67] {Bread}                   => {Sandwich} 0.9148936 0.9347826  0.9787234
## [68] {Tea}                     => {Sandwich} 0.9148936 0.9347826  0.9787234
## [69] {Bread, Tea}              => {Pastry}   0.9148936 0.9347826  0.9787234
## [70] {Bread, Tea}              => {Sandwich} 0.9148936 0.9347826  0.9787234
## [71] {}                        => {Pastry}   0.9148936 0.9148936  1.0000000
## [72] {}                        => {Sandwich} 0.9148936 0.9148936  1.0000000
##      lift     count
## [1]  1.044444 43   
## [2]  1.021739 43   
## [3]  1.021739 43   
## [4]  1.044444 43   
## [5]  1.021739 43   
## [6]  1.021739 43   
## [7]  1.021739 45   
## [8]  1.021739 45   
## [9]  1.021739 45   
## [10] 1.021739 45   
## [11] 1.021739 46   
## [12] 1.021739 46   
## [13] 1.021739 43   
## [14] 1.044444 43   
## [15] 1.021739 43   
## [16] 1.044444 43   
## [17] 1.021739 43   
## [18] 1.021739 43   
## [19] 1.021739 43   
## [20] 1.044444 43   
## [21] 1.021739 43   
## [22] 1.044444 43   
## [23] 1.021739 43   
## [24] 1.021739 43   
## [25] 1.021739 44   
## [26] 1.021739 44   
## [27] 1.021739 45   
## [28] 1.021739 45   
## [29] 1.021739 45   
## [30] 1.021739 45   
## [31] 1.021739 43   
## [32] 1.021739 43   
## [33] 1.044444 43   
## [34] 1.021739 43   
## [35] 1.021739 43   
## [36] 1.044444 43   
## [37] 1.021739 44   
## [38] 1.021739 44   
## [39] 1.000000 46   
## [40] 1.000000 46   
## [41] 1.021739 45   
## [42] 1.021739 45   
## [43] 1.021739 45   
## [44] 1.021739 45   
## [45] 1.021739 45   
## [46] 1.021739 45   
## [47] 1.021235 44   
## [48] 1.021235 44   
## [49] 1.021235 44   
## [50] 1.021235 44   
## [51] 1.021235 44   
## [52] 1.021235 44   
## [53] 1.021235 44   
## [54] 1.021235 44   
## [55] 1.000000 45   
## [56] 1.000000 45   
## [57] 1.044444 43   
## [58] 1.044444 43   
## [59] 1.044444 43   
## [60] 1.044444 43   
## [61] 1.044444 43   
## [62] 1.044444 43   
## [63] 1.044444 43   
## [64] 1.044444 43   
## [65] 1.021739 43   
## [66] 1.021739 43   
## [67] 1.021739 43   
## [68] 1.021739 43   
## [69] 1.021739 43   
## [70] 1.021739 43   
## [71] 1.000000 43   
## [72] 1.000000 43
rules_by_support<-sort(rules, by="support", decreasing=TRUE) 
inspect(head(rules_by_support))
##     lhs        rhs      support   confidence coverage  lift     count
## [1] {}      => {Bread}  0.9787234 0.9787234  1.0000000 1.000000 46   
## [2] {}      => {Tea}    0.9787234 0.9787234  1.0000000 1.000000 46   
## [3] {Bread} => {Tea}    0.9787234 1.0000000  0.9787234 1.021739 46   
## [4] {Tea}   => {Bread}  0.9787234 1.0000000  0.9787234 1.021739 46   
## [5] {}      => {NONE}   0.9574468 0.9574468  1.0000000 1.000000 45   
## [6] {}      => {Coffee} 0.9574468 0.9574468  1.0000000 1.000000 45
inspect(head(rules, n = 100, by = "support"))
##      lhs                          rhs        support   confidence coverage 
## [1]  {}                        => {Bread}    0.9787234 0.9787234  1.0000000
## [2]  {}                        => {Tea}      0.9787234 0.9787234  1.0000000
## [3]  {Bread}                   => {Tea}      0.9787234 1.0000000  0.9787234
## [4]  {Tea}                     => {Bread}    0.9787234 1.0000000  0.9787234
## [5]  {}                        => {NONE}     0.9574468 0.9574468  1.0000000
## [6]  {}                        => {Coffee}   0.9574468 0.9574468  1.0000000
## [7]  {NONE}                    => {Bread}    0.9574468 1.0000000  0.9574468
## [8]  {Bread}                   => {NONE}     0.9574468 0.9782609  0.9787234
## [9]  {NONE}                    => {Tea}      0.9574468 1.0000000  0.9574468
## [10] {Tea}                     => {NONE}     0.9574468 0.9782609  0.9787234
## [11] {Coffee}                  => {Bread}    0.9574468 1.0000000  0.9574468
## [12] {Bread}                   => {Coffee}   0.9574468 0.9782609  0.9787234
## [13] {Coffee}                  => {Tea}      0.9574468 1.0000000  0.9574468
## [14] {Tea}                     => {Coffee}   0.9574468 0.9782609  0.9787234
## [15] {Bread, NONE}             => {Tea}      0.9574468 1.0000000  0.9574468
## [16] {NONE, Tea}               => {Bread}    0.9574468 1.0000000  0.9574468
## [17] {Bread, Tea}              => {NONE}     0.9574468 0.9782609  0.9787234
## [18] {Bread, Coffee}           => {Tea}      0.9574468 1.0000000  0.9574468
## [19] {Coffee, Tea}             => {Bread}    0.9574468 1.0000000  0.9574468
## [20] {Bread, Tea}              => {Coffee}   0.9574468 0.9782609  0.9787234
## [21] {NONE}                    => {Coffee}   0.9361702 0.9777778  0.9574468
## [22] {Coffee}                  => {NONE}     0.9361702 0.9777778  0.9574468
## [23] {Coffee, NONE}            => {Bread}    0.9361702 1.0000000  0.9361702
## [24] {Bread, NONE}             => {Coffee}   0.9361702 0.9777778  0.9574468
## [25] {Bread, Coffee}           => {NONE}     0.9361702 0.9777778  0.9574468
## [26] {Coffee, NONE}            => {Tea}      0.9361702 1.0000000  0.9361702
## [27] {NONE, Tea}               => {Coffee}   0.9361702 0.9777778  0.9574468
## [28] {Coffee, Tea}             => {NONE}     0.9361702 0.9777778  0.9574468
## [29] {Bread, Coffee, NONE}     => {Tea}      0.9361702 1.0000000  0.9361702
## [30] {Coffee, NONE, Tea}       => {Bread}    0.9361702 1.0000000  0.9361702
## [31] {Bread, NONE, Tea}        => {Coffee}   0.9361702 0.9777778  0.9574468
## [32] {Bread, Coffee, Tea}      => {NONE}     0.9361702 0.9777778  0.9574468
## [33] {}                        => {Pastry}   0.9148936 0.9148936  1.0000000
## [34] {}                        => {Sandwich} 0.9148936 0.9148936  1.0000000
## [35] {Pastry}                  => {Coffee}   0.9148936 1.0000000  0.9148936
## [36] {Coffee}                  => {Pastry}   0.9148936 0.9555556  0.9574468
## [37] {Pastry}                  => {Bread}    0.9148936 1.0000000  0.9148936
## [38] {Bread}                   => {Pastry}   0.9148936 0.9347826  0.9787234
## [39] {Pastry}                  => {Tea}      0.9148936 1.0000000  0.9148936
## [40] {Tea}                     => {Pastry}   0.9148936 0.9347826  0.9787234
## [41] {Sandwich}                => {Coffee}   0.9148936 1.0000000  0.9148936
## [42] {Coffee}                  => {Sandwich} 0.9148936 0.9555556  0.9574468
## [43] {Sandwich}                => {Bread}    0.9148936 1.0000000  0.9148936
## [44] {Bread}                   => {Sandwich} 0.9148936 0.9347826  0.9787234
## [45] {Sandwich}                => {Tea}      0.9148936 1.0000000  0.9148936
## [46] {Tea}                     => {Sandwich} 0.9148936 0.9347826  0.9787234
## [47] {Coffee, Pastry}          => {Bread}    0.9148936 1.0000000  0.9148936
## [48] {Bread, Pastry}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [49] {Bread, Coffee}           => {Pastry}   0.9148936 0.9555556  0.9574468
## [50] {Coffee, Pastry}          => {Tea}      0.9148936 1.0000000  0.9148936
## [51] {Pastry, Tea}             => {Coffee}   0.9148936 1.0000000  0.9148936
## [52] {Coffee, Tea}             => {Pastry}   0.9148936 0.9555556  0.9574468
## [53] {Bread, Pastry}           => {Tea}      0.9148936 1.0000000  0.9148936
## [54] {Pastry, Tea}             => {Bread}    0.9148936 1.0000000  0.9148936
## [55] {Bread, Tea}              => {Pastry}   0.9148936 0.9347826  0.9787234
## [56] {Coffee, Sandwich}        => {Bread}    0.9148936 1.0000000  0.9148936
## [57] {Bread, Sandwich}         => {Coffee}   0.9148936 1.0000000  0.9148936
## [58] {Bread, Coffee}           => {Sandwich} 0.9148936 0.9555556  0.9574468
## [59] {Coffee, Sandwich}        => {Tea}      0.9148936 1.0000000  0.9148936
## [60] {Sandwich, Tea}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [61] {Coffee, Tea}             => {Sandwich} 0.9148936 0.9555556  0.9574468
## [62] {Bread, Sandwich}         => {Tea}      0.9148936 1.0000000  0.9148936
## [63] {Sandwich, Tea}           => {Bread}    0.9148936 1.0000000  0.9148936
## [64] {Bread, Tea}              => {Sandwich} 0.9148936 0.9347826  0.9787234
## [65] {Bread, Coffee, Pastry}   => {Tea}      0.9148936 1.0000000  0.9148936
## [66] {Coffee, Pastry, Tea}     => {Bread}    0.9148936 1.0000000  0.9148936
## [67] {Bread, Pastry, Tea}      => {Coffee}   0.9148936 1.0000000  0.9148936
## [68] {Bread, Coffee, Tea}      => {Pastry}   0.9148936 0.9555556  0.9574468
## [69] {Bread, Coffee, Sandwich} => {Tea}      0.9148936 1.0000000  0.9148936
## [70] {Coffee, Sandwich, Tea}   => {Bread}    0.9148936 1.0000000  0.9148936
## [71] {Bread, Sandwich, Tea}    => {Coffee}   0.9148936 1.0000000  0.9148936
## [72] {Bread, Coffee, Tea}      => {Sandwich} 0.9148936 0.9555556  0.9574468
##      lift     count
## [1]  1.000000 46   
## [2]  1.000000 46   
## [3]  1.021739 46   
## [4]  1.021739 46   
## [5]  1.000000 45   
## [6]  1.000000 45   
## [7]  1.021739 45   
## [8]  1.021739 45   
## [9]  1.021739 45   
## [10] 1.021739 45   
## [11] 1.021739 45   
## [12] 1.021739 45   
## [13] 1.021739 45   
## [14] 1.021739 45   
## [15] 1.021739 45   
## [16] 1.021739 45   
## [17] 1.021739 45   
## [18] 1.021739 45   
## [19] 1.021739 45   
## [20] 1.021739 45   
## [21] 1.021235 44   
## [22] 1.021235 44   
## [23] 1.021739 44   
## [24] 1.021235 44   
## [25] 1.021235 44   
## [26] 1.021739 44   
## [27] 1.021235 44   
## [28] 1.021235 44   
## [29] 1.021739 44   
## [30] 1.021739 44   
## [31] 1.021235 44   
## [32] 1.021235 44   
## [33] 1.000000 43   
## [34] 1.000000 43   
## [35] 1.044444 43   
## [36] 1.044444 43   
## [37] 1.021739 43   
## [38] 1.021739 43   
## [39] 1.021739 43   
## [40] 1.021739 43   
## [41] 1.044444 43   
## [42] 1.044444 43   
## [43] 1.021739 43   
## [44] 1.021739 43   
## [45] 1.021739 43   
## [46] 1.021739 43   
## [47] 1.021739 43   
## [48] 1.044444 43   
## [49] 1.044444 43   
## [50] 1.021739 43   
## [51] 1.044444 43   
## [52] 1.044444 43   
## [53] 1.021739 43   
## [54] 1.021739 43   
## [55] 1.021739 43   
## [56] 1.021739 43   
## [57] 1.044444 43   
## [58] 1.044444 43   
## [59] 1.021739 43   
## [60] 1.044444 43   
## [61] 1.044444 43   
## [62] 1.021739 43   
## [63] 1.021739 43   
## [64] 1.021739 43   
## [65] 1.021739 43   
## [66] 1.021739 43   
## [67] 1.044444 43   
## [68] 1.044444 43   
## [69] 1.021739 43   
## [70] 1.021739 43   
## [71] 1.044444 43   
## [72] 1.044444 43
rules_by_lift<-sort(rules, by="lift", decreasing=TRUE) 
inspect(head(rules_by_lift))
##     lhs                rhs        support   confidence coverage  lift     count
## [1] {Pastry}        => {Coffee}   0.9148936 1.0000000  0.9148936 1.044444 43   
## [2] {Coffee}        => {Pastry}   0.9148936 0.9555556  0.9574468 1.044444 43   
## [3] {Sandwich}      => {Coffee}   0.9148936 1.0000000  0.9148936 1.044444 43   
## [4] {Coffee}        => {Sandwich} 0.9148936 0.9555556  0.9574468 1.044444 43   
## [5] {Bread, Pastry} => {Coffee}   0.9148936 1.0000000  0.9148936 1.044444 43   
## [6] {Bread, Coffee} => {Pastry}   0.9148936 0.9555556  0.9574468 1.044444 43
inspect(head(rules, n = 100, by = "lift"))
##      lhs                          rhs        support   confidence coverage 
## [1]  {Pastry}                  => {Coffee}   0.9148936 1.0000000  0.9148936
## [2]  {Coffee}                  => {Pastry}   0.9148936 0.9555556  0.9574468
## [3]  {Sandwich}                => {Coffee}   0.9148936 1.0000000  0.9148936
## [4]  {Coffee}                  => {Sandwich} 0.9148936 0.9555556  0.9574468
## [5]  {Bread, Pastry}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [6]  {Bread, Coffee}           => {Pastry}   0.9148936 0.9555556  0.9574468
## [7]  {Pastry, Tea}             => {Coffee}   0.9148936 1.0000000  0.9148936
## [8]  {Coffee, Tea}             => {Pastry}   0.9148936 0.9555556  0.9574468
## [9]  {Bread, Sandwich}         => {Coffee}   0.9148936 1.0000000  0.9148936
## [10] {Bread, Coffee}           => {Sandwich} 0.9148936 0.9555556  0.9574468
## [11] {Sandwich, Tea}           => {Coffee}   0.9148936 1.0000000  0.9148936
## [12] {Coffee, Tea}             => {Sandwich} 0.9148936 0.9555556  0.9574468
## [13] {Bread, Pastry, Tea}      => {Coffee}   0.9148936 1.0000000  0.9148936
## [14] {Bread, Coffee, Tea}      => {Pastry}   0.9148936 0.9555556  0.9574468
## [15] {Bread, Sandwich, Tea}    => {Coffee}   0.9148936 1.0000000  0.9148936
## [16] {Bread, Coffee, Tea}      => {Sandwich} 0.9148936 0.9555556  0.9574468
## [17] {Pastry}                  => {Bread}    0.9148936 1.0000000  0.9148936
## [18] {Bread}                   => {Pastry}   0.9148936 0.9347826  0.9787234
## [19] {Pastry}                  => {Tea}      0.9148936 1.0000000  0.9148936
## [20] {Tea}                     => {Pastry}   0.9148936 0.9347826  0.9787234
## [21] {Sandwich}                => {Bread}    0.9148936 1.0000000  0.9148936
## [22] {Bread}                   => {Sandwich} 0.9148936 0.9347826  0.9787234
## [23] {Sandwich}                => {Tea}      0.9148936 1.0000000  0.9148936
## [24] {Tea}                     => {Sandwich} 0.9148936 0.9347826  0.9787234
## [25] {NONE}                    => {Bread}    0.9574468 1.0000000  0.9574468
## [26] {Bread}                   => {NONE}     0.9574468 0.9782609  0.9787234
## [27] {NONE}                    => {Tea}      0.9574468 1.0000000  0.9574468
## [28] {Tea}                     => {NONE}     0.9574468 0.9782609  0.9787234
## [29] {Coffee}                  => {Bread}    0.9574468 1.0000000  0.9574468
## [30] {Bread}                   => {Coffee}   0.9574468 0.9782609  0.9787234
## [31] {Coffee}                  => {Tea}      0.9574468 1.0000000  0.9574468
## [32] {Tea}                     => {Coffee}   0.9574468 0.9782609  0.9787234
## [33] {Bread}                   => {Tea}      0.9787234 1.0000000  0.9787234
## [34] {Tea}                     => {Bread}    0.9787234 1.0000000  0.9787234
## [35] {Coffee, Pastry}          => {Bread}    0.9148936 1.0000000  0.9148936
## [36] {Coffee, Pastry}          => {Tea}      0.9148936 1.0000000  0.9148936
## [37] {Bread, Pastry}           => {Tea}      0.9148936 1.0000000  0.9148936
## [38] {Pastry, Tea}             => {Bread}    0.9148936 1.0000000  0.9148936
## [39] {Bread, Tea}              => {Pastry}   0.9148936 0.9347826  0.9787234
## [40] {Coffee, Sandwich}        => {Bread}    0.9148936 1.0000000  0.9148936
## [41] {Coffee, Sandwich}        => {Tea}      0.9148936 1.0000000  0.9148936
## [42] {Bread, Sandwich}         => {Tea}      0.9148936 1.0000000  0.9148936
## [43] {Sandwich, Tea}           => {Bread}    0.9148936 1.0000000  0.9148936
## [44] {Bread, Tea}              => {Sandwich} 0.9148936 0.9347826  0.9787234
## [45] {Coffee, NONE}            => {Bread}    0.9361702 1.0000000  0.9361702
## [46] {Coffee, NONE}            => {Tea}      0.9361702 1.0000000  0.9361702
## [47] {Bread, NONE}             => {Tea}      0.9574468 1.0000000  0.9574468
## [48] {NONE, Tea}               => {Bread}    0.9574468 1.0000000  0.9574468
## [49] {Bread, Tea}              => {NONE}     0.9574468 0.9782609  0.9787234
## [50] {Bread, Coffee}           => {Tea}      0.9574468 1.0000000  0.9574468
## [51] {Coffee, Tea}             => {Bread}    0.9574468 1.0000000  0.9574468
## [52] {Bread, Tea}              => {Coffee}   0.9574468 0.9782609  0.9787234
## [53] {Bread, Coffee, Pastry}   => {Tea}      0.9148936 1.0000000  0.9148936
## [54] {Coffee, Pastry, Tea}     => {Bread}    0.9148936 1.0000000  0.9148936
## [55] {Bread, Coffee, Sandwich} => {Tea}      0.9148936 1.0000000  0.9148936
## [56] {Coffee, Sandwich, Tea}   => {Bread}    0.9148936 1.0000000  0.9148936
## [57] {Bread, Coffee, NONE}     => {Tea}      0.9361702 1.0000000  0.9361702
## [58] {Coffee, NONE, Tea}       => {Bread}    0.9361702 1.0000000  0.9361702
## [59] {NONE}                    => {Coffee}   0.9361702 0.9777778  0.9574468
## [60] {Coffee}                  => {NONE}     0.9361702 0.9777778  0.9574468
## [61] {Bread, NONE}             => {Coffee}   0.9361702 0.9777778  0.9574468
## [62] {Bread, Coffee}           => {NONE}     0.9361702 0.9777778  0.9574468
## [63] {NONE, Tea}               => {Coffee}   0.9361702 0.9777778  0.9574468
## [64] {Coffee, Tea}             => {NONE}     0.9361702 0.9777778  0.9574468
## [65] {Bread, NONE, Tea}        => {Coffee}   0.9361702 0.9777778  0.9574468
## [66] {Bread, Coffee, Tea}      => {NONE}     0.9361702 0.9777778  0.9574468
## [67] {}                        => {Pastry}   0.9148936 0.9148936  1.0000000
## [68] {}                        => {Sandwich} 0.9148936 0.9148936  1.0000000
## [69] {}                        => {NONE}     0.9574468 0.9574468  1.0000000
## [70] {}                        => {Coffee}   0.9574468 0.9574468  1.0000000
## [71] {}                        => {Bread}    0.9787234 0.9787234  1.0000000
## [72] {}                        => {Tea}      0.9787234 0.9787234  1.0000000
##      lift     count
## [1]  1.044444 43   
## [2]  1.044444 43   
## [3]  1.044444 43   
## [4]  1.044444 43   
## [5]  1.044444 43   
## [6]  1.044444 43   
## [7]  1.044444 43   
## [8]  1.044444 43   
## [9]  1.044444 43   
## [10] 1.044444 43   
## [11] 1.044444 43   
## [12] 1.044444 43   
## [13] 1.044444 43   
## [14] 1.044444 43   
## [15] 1.044444 43   
## [16] 1.044444 43   
## [17] 1.021739 43   
## [18] 1.021739 43   
## [19] 1.021739 43   
## [20] 1.021739 43   
## [21] 1.021739 43   
## [22] 1.021739 43   
## [23] 1.021739 43   
## [24] 1.021739 43   
## [25] 1.021739 45   
## [26] 1.021739 45   
## [27] 1.021739 45   
## [28] 1.021739 45   
## [29] 1.021739 45   
## [30] 1.021739 45   
## [31] 1.021739 45   
## [32] 1.021739 45   
## [33] 1.021739 46   
## [34] 1.021739 46   
## [35] 1.021739 43   
## [36] 1.021739 43   
## [37] 1.021739 43   
## [38] 1.021739 43   
## [39] 1.021739 43   
## [40] 1.021739 43   
## [41] 1.021739 43   
## [42] 1.021739 43   
## [43] 1.021739 43   
## [44] 1.021739 43   
## [45] 1.021739 44   
## [46] 1.021739 44   
## [47] 1.021739 45   
## [48] 1.021739 45   
## [49] 1.021739 45   
## [50] 1.021739 45   
## [51] 1.021739 45   
## [52] 1.021739 45   
## [53] 1.021739 43   
## [54] 1.021739 43   
## [55] 1.021739 43   
## [56] 1.021739 43   
## [57] 1.021739 44   
## [58] 1.021739 44   
## [59] 1.021235 44   
## [60] 1.021235 44   
## [61] 1.021235 44   
## [62] 1.021235 44   
## [63] 1.021235 44   
## [64] 1.021235 44   
## [65] 1.021235 44   
## [66] 1.021235 44   
## [67] 1.000000 43   
## [68] 1.000000 43   
## [69] 1.000000 45   
## [70] 1.000000 45   
## [71] 1.000000 46   
## [72] 1.000000 46
rules
## set of 72 rules

From the above rules indicator it can be analysed that the confidence level between Pastry and coffee is very high which indicates the strong association between two items.Similar the confidence between sandwich and coffee and coffee and bread,pastry combined is also very high thus indicating a strong association between them. The rule that has highest support values are mainly Pastry , coffee , sandwiches and Bread indicates that these items appear very often in transactions. The main concern here is to choose the items that are heavily correlated with each other. Some association rules that I can conclude from the above data are: If Coffee is bought, then Pastry is also bought. If Sandwich is bought, then Coffee is also bought. If Bread, Pastry is bought, Coffee is also bought.

Visualizations

The association between the items can also be visualized by applying multiple plots.

plot(rules, method="graph", measure = "support",shading = "lift",engine = "htmlwidget")
plot(rules, method="graph", measure = "support",shading = "lift",control=list(reorder=TRUE))
## Warning: Unknown control parameters: reorder
## Available control parameters (with default values):
## layout    =  stress
## circular  =  FALSE
## ggraphdots    =  NULL
## edges     =  <environment>
## nodes     =  <environment>
## nodetext  =  <environment>
## colors    =  c("#EE0000FF", "#EEEEEEFF")
## engine    =  ggplot2
## max   =  100
## verbose   =  FALSE

The relationship and dependencies between items can be visualized in a parallel coordinates plot as shown below.It can be observed that strong association is between Tea, Pastry, Coffee and Sandwich.

plot(rules, method="paracoord", control=list(reorder=TRUE))

Conclusion

Hence, it will be profitable to put ‘Coffee’ in a visible and reachable shelf as it is one of the most frequently bought items. Also, near the shelf where ‘Pastry’ is put, there should be shelves for ‘Coffee’ and ‘ Sandwich’ as their confidence value is quite high. So there is a higher probability of buying them along with ‘Tea’ Thus, with similar actions, it can be aimed at increasing the sales and profits of the Bakery store by analyzing users’ shopping patterns.