# library arules  and arulesViz to be included
 txn = read.transactions("GroceryStore.csv",sep = ",") 
## Warning in asMethod(object): removing duplicated items in transactions
cat(sprintf("\n The details of the rules formed are as follows\n"))
## 
##  The details of the rules formed are as follows
rules = apriori(txn,parameter = list(support = 0.25, confidence= 0.5,minlen = 2,target = "rules"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.25      2
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 250 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[20 item(s), 1001 transaction(s)] done [0.00s].
## sorting and recoding items ... [18 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 done [0.00s].
## writing ... [10 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(rules,by = "lift"),n =15))
##      lhs           rhs        support   confidence lift     count
## [1]  {crackers} => {soda}     0.2507493 0.5143443  1.619052 251  
## [2]  {soda}     => {crackers} 0.2507493 0.7893082  1.619052 251  
## [3]  {hummus}   => {peroni}   0.2517483 0.8262295  1.378426 252  
## [4]  {soda}     => {peroni}   0.2567433 0.8081761  1.348307 257  
## [5]  {crackers} => {peroni}   0.3656344 0.7500000  1.251250 366  
## [6]  {peroni}   => {crackers} 0.3656344 0.6100000  1.251250 366  
## [7]  {olives}   => {tuna}     0.2557443 0.5412262  1.114748 256  
## [8]  {tuna}     => {olives}   0.2557443 0.5267490  1.114748 256  
## [9]  {risotto}  => {peroni}   0.2607393 0.6658163  1.110804 261  
## [10] {tuna}     => {peroni}   0.2877123 0.5925926  0.988642 288
plotly_arules(head(sort(rules,by = "lift"),n =15))
## Warning: 'plotly_arules' is deprecated.
## Use 'plot' instead.
## See help("Deprecated")
itemFrequencyPlot(txn, topN=15, type="absolute", main="Item Frequency")

distance = dissimilarity(txn,which = "items",method = "dice")
fit = hclust(distance,method = "ward.D")
plot(fit,main = "Dendogram of items")
rect.hclust(fit,k=3,border = "red")

plot(rules, method = "graph", engine = "htmlwidget")
cat(sprintf("\n \n"))

10 rules have a support count of at least 250 and confidence of 50%. Increasing the minimum support required removes spurious rules resulting from coincidence. The risk of raising the minimum support required is that we cull out meaningful rules that involve uncommon items.

Antecedent: hummus; Consequent: peroni. If a customer purchases hummus, then they also purchase peroni. The support of this rule is 252 meaning that hummus and peroni have been purchased 252 times together. The confidence of this rule is 82.62% which means that of the 305 times that hummus was purchased, peroni was also purchased 252 times. The lift ratio of this rule is 1.38 which means that customer purchasing hummus is 38% more likely than a randomly selected customer to purchase hummus. Customers who enjoy snacking on hummus apparently also drink more peroni than do other customers. Perhaps the two products could be displayed near each other or tied together in a promotion or couponing campaign.