Install packages
# install.packages("arules") ## this enables the execution of association rules
# install.packages("arulesViz")  ## this enables visualization of rules 

library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)


##### for exercise 

# read data (note that the format is basket, i.e., one row contains all items in one transaction)
tr <- read.transactions("~/Desktop/R/Market_Basket_Optimisation.csv", format="basket", sep=",",rm.duplicates=TRUE)
## distribution of transactions with duplicates:
## 1 
## 5
# Answer the following questions (Let support=0.005, confidence=0.5)
# (1) How many rules do you discover? 
##20 rules
rules_sup05_conf50 <- apriori(tr, parameter=list(sup=0.005, conf=0.5))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.005      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: 37 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[119 item(s), 7501 transaction(s)] done [0.00s].
## sorting and recoding items ... [101 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [20 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(rules_sup05_conf50)
##      lhs                                rhs             support     confidence
## [1]  {salmon,spaghetti}              => {mineral water} 0.006799093 0.5049505 
## [2]  {olive oil,soup}                => {mineral water} 0.005199307 0.5820896 
## [3]  {frozen vegetables,soup}        => {mineral water} 0.005065991 0.6333333 
## [4]  {ground beef,soup}              => {mineral water} 0.005065991 0.5205479 
## [5]  {milk,soup}                     => {mineral water} 0.008532196 0.5614035 
## [6]  {chocolate,soup}                => {mineral water} 0.005599253 0.5526316 
## [7]  {soup,spaghetti}                => {mineral water} 0.007465671 0.5233645 
## [8]  {cooking oil,eggs}              => {mineral water} 0.006399147 0.5454545 
## [9]  {milk,turkey}                   => {mineral water} 0.006132516 0.5411765 
## [10] {chicken,chocolate}             => {mineral water} 0.007598987 0.5181818 
## [11] {frozen vegetables,olive oil}   => {spaghetti}     0.005732569 0.5058824 
## [12] {frozen vegetables,olive oil}   => {mineral water} 0.006532462 0.5764706 
## [13] {milk,olive oil}                => {mineral water} 0.008532196 0.5000000 
## [14] {chocolate,olive oil}           => {mineral water} 0.008265565 0.5040650 
## [15] {ground beef,shrimp}            => {spaghetti}     0.005999200 0.5232558 
## [16] {ground beef,pancakes}          => {mineral water} 0.007465671 0.5137615 
## [17] {frozen vegetables,ground beef} => {spaghetti}     0.008665511 0.5118110 
## [18] {frozen vegetables,ground beef} => {mineral water} 0.009198773 0.5433071 
## [19] {ground beef,milk}              => {mineral water} 0.011065191 0.5030303 
## [20] {eggs,ground beef}              => {mineral water} 0.010131982 0.5066667 
##      coverage    lift     count
## [1]  0.013464871 2.118363 51   
## [2]  0.008932142 2.441976 39   
## [3]  0.007998933 2.656954 38   
## [4]  0.009732036 2.183798 38   
## [5]  0.015197974 2.355194 64   
## [6]  0.010131982 2.318395 42   
## [7]  0.014264765 2.195614 56   
## [8]  0.011731769 2.288286 48   
## [9]  0.011331822 2.270338 46   
## [10] 0.014664711 2.173871 57   
## [11] 0.011331822 2.905531 43   
## [12] 0.011331822 2.418404 49   
## [13] 0.017064391 2.097595 64   
## [14] 0.016397814 2.114649 62   
## [15] 0.011465138 3.005315 45   
## [16] 0.014531396 2.155327 56   
## [17] 0.016931076 2.939582 65   
## [18] 0.016931076 2.279277 69   
## [19] 0.021997067 2.110308 83   
## [20] 0.019997334 2.125563 76
# (2) Are these rules all useful? 
##yes
# (3) If you want to recommend 'spaghetti' to some customers, how will you choose?
spaghetti <- apriori(tr, parameter = list(supp=0.005, conf=0.5),appearance = list(default="lhs",rhs="spaghetti"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.005      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: 37 
## 
## set item appearances ...[1 item(s)] done [0.00s].
## set transactions ...[119 item(s), 7501 transaction(s)] done [0.00s].
## sorting and recoding items ... [101 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [3 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(spaghetti)
##     lhs                                rhs         support     confidence
## [1] {frozen vegetables,olive oil}   => {spaghetti} 0.005732569 0.5058824 
## [2] {ground beef,shrimp}            => {spaghetti} 0.005999200 0.5232558 
## [3] {frozen vegetables,ground beef} => {spaghetti} 0.008665511 0.5118110 
##     coverage   lift     count
## [1] 0.01133182 2.905531 43   
## [2] 0.01146514 3.005315 45   
## [3] 0.01693108 2.939582 65
## I will put spaghetti om the shelf next to frozen vegetables, shrimp, ground beef and olive oil.