Association Rules

Apriori Algorithm

Grocery Dataset

library(arules)
## 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 3.5.1
## Loading required package: grid
data("Groceries")
class(Groceries)
## [1] "transactions"
## attr(,"package")
## [1] "arules"
itemFrequencyPlot(Groceries,topN=25)

rules <- apriori(Groceries, parameter = list(supp = 0.002, confidence = 0.50, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.002      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 19 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
## sorting and recoding items ... [147 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(Groceries, parameter = list(supp = 0.002, confidence =
## 0.5, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [582 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(rules), n = 10))
##      lhs                     rhs                   support confidence     lift count
## [1]  {other vegetables,                                                             
##       yogurt}             => {whole milk}       0.02226741  0.5128806 2.007235   219
## [2]  {tropical fruit,                                                               
##       yogurt}             => {whole milk}       0.01514997  0.5173611 2.024770   149
## [3]  {other vegetables,                                                             
##       whipped/sour cream} => {whole milk}       0.01464159  0.5070423 1.984385   144
## [4]  {root vegetables,                                                              
##       yogurt}             => {whole milk}       0.01453991  0.5629921 2.203354   143
## [5]  {pip fruit,                                                                    
##       other vegetables}   => {whole milk}       0.01352313  0.5175097 2.025351   133
## [6]  {root vegetables,                                                              
##       yogurt}             => {other vegetables} 0.01291307  0.5000000 2.584078   127
## [7]  {root vegetables,                                                              
##       rolls/buns}         => {whole milk}       0.01270971  0.5230126 2.046888   125
## [8]  {other vegetables,                                                             
##       domestic eggs}      => {whole milk}       0.01230300  0.5525114 2.162336   121
## [9]  {tropical fruit,                                                               
##       root vegetables}    => {other vegetables} 0.01230300  0.5845411 3.020999   121
## [10] {root vegetables,                                                              
##       rolls/buns}         => {other vegetables} 0.01220132  0.5020921 2.594890   120
plot(head(sort(rules, by = "lift"), n = 5), method = "graph", control = list(cex = 1.3)) #cex = font size

plot(rules)
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

plot(head(sort(rules, by = "lift"), n = 10), method = "grouped")