Association Rules For groceries

GRData <- read.csv("D:\\DataScience\\Assignments\\AssociationRules\\groceries.csv")
#install.packages("mvinfluence")

library(car)
## Warning: package 'car' was built under R version 3.5.1
## Loading required package: carData
library(carData)
library(arules)
## Warning: package 'arules' was built under R version 3.5.1
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following object is masked from 'package:car':
## 
##     recode
## 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
library(mvinfluence)
## Warning: package 'mvinfluence' was built under R version 3.5.1
## Loading required package: heplots
## Warning: package 'heplots' was built under R version 3.5.1
GRData1 <- as(GRData,"transactions")
# Item Frequency plot
itemFrequencyPlot(GRData1,topN=25)

grocires_apriori <- apriori(GRData1, parameter = list(supp=0.005, conf=0.45, minlen=2, maxlen=4))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##        0.45    0.1    1 none FALSE            TRUE       5   0.005      2
##  maxlen target   ext
##       4  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 76 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[655 item(s), 15295 transaction(s)] done [0.03s].
## sorting and recoding items ... [168 item(s)] done [0.00s].
## creating transaction tree ... done [0.02s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [177 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
grocires_apriori
## set of 177 rules
#Ploting Grocires Rules

inspect(head(sort(grocires_apriori),n=20))
##      lhs                           rhs                       support confidence      lift count
## [1]  {margarine=}               => {ready.soups=}         0.39980386  1.0000000 1.8613849  6115
## [2]  {ready.soups=}             => {margarine=}           0.39980386  0.7441889 1.8613849  6115
## [3]  {semi.finished.bread=}     => {margarine=}           0.22785224  1.0000000 2.5012265  3485
## [4]  {margarine=}               => {semi.finished.bread=} 0.22785224  0.5699101 2.5012265  3485
## [5]  {semi.finished.bread=}     => {ready.soups=}         0.22785224  1.0000000 1.8613849  3485
## [6]  {semi.finished.bread=,                                                                    
##       margarine=}               => {ready.soups=}         0.22785224  1.0000000 1.8613849  3485
## [7]  {semi.finished.bread=,                                                                    
##       ready.soups=}             => {margarine=}           0.22785224  1.0000000 2.5012265  3485
## [8]  {margarine=,                                                                              
##       ready.soups=}             => {semi.finished.bread=} 0.22785224  0.5699101 2.5012265  3485
## [9]  {citrus.fruit=soda}        => {ready.soups=}         0.03059823  0.8509091 1.5838694   468
## [10] {citrus.fruit=whole milk}  => {ready.soups=}         0.02902909  0.5091743 0.9477694   444
## [11] {citrus.fruit=rolls/buns}  => {ready.soups=}         0.02576005  0.7150635 1.3310085   394
## [12] {citrus.fruit=canned beer} => {ready.soups=}         0.02451782  0.9664948 1.7990189   375
## [13] {citrus.fruit=soda}        => {margarine=}           0.02451782  0.6818182 1.7053817   375
## [14] {citrus.fruit=soda,                                                                       
##       margarine=}               => {ready.soups=}         0.02451782  1.0000000 1.8613849   375
## [15] {citrus.fruit=soda,                                                                       
##       ready.soups=}             => {margarine=}           0.02451782  0.8012821 2.0041879   375
## [16] {citrus.fruit=canned beer} => {margarine=}           0.02262177  0.8917526 2.2304752   346
## [17] {citrus.fruit=canned beer,                                                                
##       margarine=}               => {ready.soups=}         0.02262177  1.0000000 1.8613849   346
## [18] {citrus.fruit=canned beer,                                                                
##       ready.soups=}             => {margarine=}           0.02262177  0.9226667 2.3077983   346
## [19] {semi.finished.bread=soda} => {ready.soups=}         0.02000654  0.6270492 1.1671799   306
## [20] {citrus.fruit=rolls/buns}  => {margarine=}           0.01922197  0.5335753 1.3345927   294
inspect(tail(sort(grocires_apriori),n=20))
##      lhs                                          rhs                                      support confidence       lift count
## [1]  {citrus.fruit=domestic eggs}              => {ready.soups=}                       0.005295848  0.4525140  0.8423027    81
## [2]  {citrus.fruit=hygiene articles}           => {margarine=}                         0.005230467  0.8888889  2.2233124    80
## [3]  {semi.finished.bread=specialty chocolate} => {ready.soups=}                       0.005230467  0.8421053  1.5674820    80
## [4]  {citrus.fruit=napkins}                    => {semi.finished.bread=}               0.005230467  0.5925926  2.6007758    80
## [5]  {citrus.fruit=hygiene articles,                                                                                          
##       margarine=}                              => {ready.soups=}                       0.005230467  1.0000000  1.8613849    80
## [6]  {citrus.fruit=hygiene articles,                                                                                          
##       ready.soups=}                            => {margarine=}                         0.005230467  0.8888889  2.2233124    80
## [7]  {citrus.fruit=napkins,                                                                                                   
##       semi.finished.bread=}                    => {margarine=}                         0.005230467  1.0000000  2.5012265    80
## [8]  {citrus.fruit=napkins,                                                                                                   
##       margarine=}                              => {semi.finished.bread=}               0.005230467  0.6400000  2.8088379    80
## [9]  {citrus.fruit=napkins,                                                                                                   
##       semi.finished.bread=}                    => {ready.soups=}                       0.005230467  1.0000000  1.8613849    80
## [10] {citrus.fruit=napkins,                                                                                                   
##       ready.soups=}                            => {semi.finished.bread=}               0.005230467  0.6015038  2.6398852    80
## [11] {citrus.fruit=napkins,                                                                                                   
##       semi.finished.bread=,                                                                                                   
##       margarine=}                              => {ready.soups=}                       0.005230467  1.0000000  1.8613849    80
## [12] {citrus.fruit=napkins,                                                                                                   
##       semi.finished.bread=,                                                                                                   
##       ready.soups=}                            => {margarine=}                         0.005230467  1.0000000  2.5012265    80
## [13] {citrus.fruit=napkins,                                                                                                   
##       margarine=,                                                                                                             
##       ready.soups=}                            => {semi.finished.bread=}               0.005230467  0.6400000  2.8088379    80
## [14] {semi.finished.bread=coffee}              => {ready.soups=}                       0.005165087  0.5524476  1.0283176    79
## [15] {margarine=pip fruit}                     => {semi.finished.bread=tropical fruit} 0.005165087  0.5337838 22.7415682    79
## [16] {semi.finished.bread=specialty bar}       => {ready.soups=}                       0.005099706  0.8764045  1.6313261    78
## [17] {semi.finished.bread=napkins}             => {margarine=}                         0.005099706  0.6610169  1.6533531    78
## [18] {margarine=napkins}                       => {ready.soups=}                       0.005099706  0.6000000  1.1168310    78
## [19] {semi.finished.bread=napkins,                                                                                            
##       margarine=}                              => {ready.soups=}                       0.005099706  1.0000000  1.8613849    78
## [20] {semi.finished.bread=napkins,                                                                                            
##       ready.soups=}                            => {margarine=}                         0.005099706  0.6902655  1.7265103    78
plot(head(sort(grocires_apriori),n=20), method="graph", control=list(cex=0.70))

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

plot(head(sort(grocires_apriori),n=10), method="grouped", control=list(cex=0.2))
## Warning: Unknown control parameters: cex
## Available control parameters (with default values):
## main  =  Grouped Matrix for 10 Rules
## k     =  20
## rhs_max   =  10
## lhs_items     =  2
## aggr.fun  =  function (x, ...)  UseMethod("mean")
## col   =  c("#EE0000FF", "#EE0303FF", "#EE0606FF", "#EE0909FF", "#EE0C0CFF", "#EE0F0FFF", "#EE1212FF", "#EE1515FF", "#EE1818FF", "#EE1B1BFF", "#EE1E1EFF", "#EE2222FF", "#EE2525FF", "#EE2828FF", "#EE2B2BFF", "#EE2E2EFF", "#EE3131FF", "#EE3434FF", "#EE3737FF", "#EE3A3AFF", "#EE3D3DFF", "#EE4040FF", "#EE4444FF", "#EE4747FF", "#EE4A4AFF", "#EE4D4DFF", "#EE5050FF", "#EE5353FF", "#EE5656FF", "#EE5959FF", "#EE5C5CFF", "#EE5F5FFF", "#EE6262FF", "#EE6666FF", "#EE6969FF", "#EE6C6CFF", "#EE6F6FFF", "#EE7272FF", "#EE7575FF",  "#EE7878FF", "#EE7B7BFF", "#EE7E7EFF", "#EE8181FF", "#EE8484FF", "#EE8888FF", "#EE8B8BFF", "#EE8E8EFF", "#EE9191FF", "#EE9494FF", "#EE9797FF", "#EE9999FF", "#EE9B9BFF", "#EE9D9DFF", "#EE9F9FFF", "#EEA0A0FF", "#EEA2A2FF", "#EEA4A4FF", "#EEA5A5FF", "#EEA7A7FF", "#EEA9A9FF", "#EEABABFF", "#EEACACFF", "#EEAEAEFF", "#EEB0B0FF", "#EEB1B1FF", "#EEB3B3FF", "#EEB5B5FF", "#EEB7B7FF", "#EEB8B8FF", "#EEBABAFF", "#EEBCBCFF", "#EEBDBDFF", "#EEBFBFFF", "#EEC1C1FF", "#EEC3C3FF", "#EEC4C4FF", "#EEC6C6FF", "#EEC8C8FF",  "#EEC9C9FF", "#EECBCBFF", "#EECDCDFF", "#EECFCFFF", "#EED0D0FF", "#EED2D2FF", "#EED4D4FF", "#EED5D5FF", "#EED7D7FF", "#EED9D9FF", "#EEDBDBFF", "#EEDCDCFF", "#EEDEDEFF", "#EEE0E0FF", "#EEE1E1FF", "#EEE3E3FF", "#EEE5E5FF", "#EEE7E7FF", "#EEE8E8FF", "#EEEAEAFF", "#EEECECFF", "#EEEEEEFF")
## reverse   =  TRUE
## xlab  =  NULL
## ylab  =  NULL
## legend    =  Size: support  Color: lift
## spacing   =  -1
## panel.function    =  function (row, size, shading, spacing)  {     size[size == 0] <- NA     shading[is.na(shading)] <- 1     grid.circle(x = c(1:length(size)), y = row, r = size/2 * (1 - spacing), default.units = "native", gp = gpar(fill = shading, col = shading, alpha = 0.9)) }
## gp_main   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_labels     =  list(cex = 0.8)
## gp_labs   =  list(cex = 1.2, fontface = "bold", font = c(bold = 2))
## gp_lines  =  list(col = "gray", lty = 3)
## newpage   =  TRUE
## max.shading   =  NA
## engine    =  default
## verbose   =  FALSE