1 Packages

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

2 Loading and Inspecting Data

# Load the dataset
df <- read.csv("Q05_1.csv")
# Display the first few rows of the dataset
head(df)

3 Data Transformation

# Transform the dataframe to a transaction format
df <- apply(df, 2, as.matrix)
m <- as(df, "transactions")
# Inspect the transaction data
summary(m)
## transactions as itemMatrix in sparse format with
##  1361 rows (elements/itemsets/transactions) and
##  40 columns (items) and a density of 0.03897869 
## 
## most frequent items:
##        Eggs White.Bread X2pct..Milk    Hot.Dogs    Tomatoes     (Other) 
##         167         162         149         126          90        1428 
## 
## element (itemset/transaction) length distribution:
## sizes
##   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19 
## 785 297  63  38  25  22  23  18  14   8  15   6   7  12  10   4   2   5   2   2 
##  20  40 
##   2   1 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   1.559   1.000  40.000 
## 
## includes extended item information - examples:
##         labels
## 1     Hot.Dogs
## 2   Can.Opener
## 3 Dried.Apples

4 Create Frequency Plot

# Plot item frequency
itemFrequencyPlot(m, topN = 10, type = "relative", main = "Top 10 Items by Relative Frequency")

5 Generate Rules with Specific Thresholds

# Generating rules with support threshold 0.01 and confidence threshold 0.5
rules <- apriori(m, parameter = list(supp = 0.01, conf = 0.5, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.01      1
##  maxlen target  ext
##       3  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 13 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[40 item(s), 1361 transaction(s)] done [0.00s].
## sorting and recoding items ... [33 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(m, parameter = list(supp = 0.01, conf = 0.5, maxlen = 3)):
## Mining stopped (maxlen reached). Only patterns up to a length of 3 returned!
##  done [0.00s].
## writing ... [529 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
# Summary of the rules
summary(rules)
## set of 529 rules
## 
## rule length distribution (lhs + rhs):sizes
##   2   3 
##  68 461 
## 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.000   3.000   3.000   2.871   3.000   3.000 
## 
## summary of quality measures:
##     support          confidence        coverage            lift       
##  Min.   :0.01029   Min.   :0.5000   Min.   :0.01029   Min.   : 4.075  
##  1st Qu.:0.01102   1st Qu.:0.5769   1st Qu.:0.01543   1st Qu.: 5.727  
##  Median :0.01249   Median :0.6818   Median :0.02057   Median : 6.434  
##  Mean   :0.01490   Mean   :0.6816   Mean   :0.02278   Mean   : 6.685  
##  3rd Qu.:0.01690   3rd Qu.:0.7692   3rd Qu.:0.02719   3rd Qu.: 7.351  
##  Max.   :0.04041   Max.   :1.0000   Max.   :0.06613   Max.   :15.752  
##      count      
##  Min.   :14.00  
##  1st Qu.:15.00  
##  Median :17.00  
##  Mean   :20.28  
##  3rd Qu.:23.00  
##  Max.   :55.00  
## 
## mining info:
##  data ntransactions support confidence
##     m          1361    0.01        0.5
##                                                                      call
##  apriori(data = m, parameter = list(supp = 0.01, conf = 0.5, maxlen = 3))

6 Rules with ‘Eggs’ as Consequent

# Generate rules with 'Eggs' as the consequent, support threshold 0.01, confidence threshold 0.5
eggs_rules <- apriori(m, parameter = list(supp = 0.01, conf = 0.5), 
                      appearance = list(default = "lhs", rhs = "Eggs"))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5    0.01      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: 13 
## 
## set item appearances ...[1 item(s)] done [0.00s].
## set transactions ...[40 item(s), 1361 transaction(s)] done [0.00s].
## sorting and recoding items ... [33 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 done [0.00s].
## writing ... [205 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
# Display the rules with 'Eggs' as consequent
inspect(eggs_rules)
##       lhs                          rhs       support confidence   coverage     lift count
## [1]   {Frozen.Cauliflower}      => {Eggs} 0.01249082  0.7391304 0.01689934 6.023692    17
## [2]   {Dried.Apples}            => {Eggs} 0.01396032  0.5937500 0.02351212 4.838885    19
## [3]   {Merlot.Wine}             => {Eggs} 0.01396032  0.5588235 0.02498163 4.554244    19
## [4]   {Limes}                   => {Eggs} 0.01542983  0.5384615 0.02865540 4.388300    21
## [5]   {Napkins}                 => {Eggs} 0.01616458  0.5238095 0.03085966 4.268891    22
## [6]   {Chicken.Breast...Frozen} => {Eggs} 0.01689934  0.5227273 0.03232917 4.260071    23
## [7]   {Cranberry.Juice}         => {Eggs} 0.01542983  0.5000000 0.03085966 4.074850    21
## [8]   {Vanilla.Wafers}          => {Eggs} 0.01616458  0.5365854 0.03012491 4.373010    22
## [9]   {Grape.Fruit.Roll}        => {Eggs} 0.01616458  0.5500000 0.02939015 4.482335    22
## [10]  {Canned.Mixed.Fruit}      => {Eggs} 0.01763409  0.5581395 0.03159442 4.548670    24
## [11]  {Blueberry.Waffles}       => {Eggs} 0.01983835  0.5744681 0.03453343 4.681743    27
## [12]  {Paper.Plates}            => {Eggs} 0.01910360  0.6046512 0.03159442 4.927726    26
## [13]  {Diet.Cola}               => {Eggs} 0.01910360  0.6190476 0.03085966 5.045053    26
## [14]  {Sharp.Cheddar.Cheese}    => {Eggs} 0.02057311  0.5957447 0.03453343 4.855141    28
## [15]  {Vegetable.Oil}           => {Eggs} 0.02130786  0.5272727 0.04041146 4.297115    29
## [16]  {Chicken.Soup}            => {Eggs} 0.02057311  0.5714286 0.03600294 4.656972    28
## [17]  {Shampoo}                 => {Eggs} 0.02277737  0.5740741 0.03967671 4.678532    31
## [18]  {Spaghetti}               => {Eggs} 0.02130786  0.5918367 0.03600294 4.823292    29
## [19]  {Frozen.Shrimp}           => {Eggs} 0.02792065  0.5000000 0.05584129 4.074850    38
## [20]  {Pancake.Mix}             => {Eggs} 0.03232917  0.5641026 0.05731080 4.597267    44
## [21]  {Popcorn.Salt}            => {Eggs} 0.03673769  0.5747126 0.06392359 4.683736    50
## [22]  {Tomatoes}                => {Eggs} 0.03673769  0.5555556 0.06612785 4.527611    50
## [23]  {Raisins}                 => {Eggs} 0.03673769  0.6024096 0.06098457 4.909458    50
## [24]  {X2pct..Milk,                                                                      
##        Frozen.Cauliflower}      => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [25]  {Frozen.Cauliflower,                                                               
##        White.Bread}             => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [26]  {Dried.Apples,                                                                     
##        X2pct..Milk}             => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [27]  {Merlot.Wine,                                                                      
##        X2pct..Milk}             => {Eggs} 0.01249082  0.8095238 0.01542983 6.597377    17
## [28]  {Merlot.Wine,                                                                      
##        White.Bread}             => {Eggs} 0.01102131  0.6818182 0.01616458 5.556614    15
## [29]  {White.Bread,                                                                      
##        Limes}                   => {Eggs} 0.01028655  0.7000000 0.01469508 5.704790    14
## [30]  {Bologna,                                                                          
##        Napkins}                 => {Eggs} 0.01028655  0.9333333 0.01102131 7.606387    14
## [31]  {Raisins,                                                                          
##        Napkins}                 => {Eggs} 0.01028655  0.7777778 0.01322557 6.338656    14
## [32]  {X2pct..Milk,                                                                      
##        Napkins}                 => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [33]  {Napkins,                                                                          
##        White.Bread}             => {Eggs} 0.01249082  0.7727273 0.01616458 6.297496    17
## [34]  {Popcorn.Salt,                                                                     
##        Chicken.Breast...Frozen} => {Eggs} 0.01249082  0.8500000 0.01469508 6.927246    17
## [35]  {X2pct..Milk,                                                                      
##        Chicken.Breast...Frozen} => {Eggs} 0.01028655  0.7000000 0.01469508 5.704790    14
## [36]  {White.Bread,                                                                      
##        Chicken.Breast...Frozen} => {Eggs} 0.01102131  0.7500000 0.01469508 6.112275    15
## [37]  {Cranberry.Juice,                                                                  
##        X2pct..Milk}             => {Eggs} 0.01028655  0.6666667 0.01542983 5.433134    14
## [38]  {Cranberry.Juice,                                                                  
##        White.Bread}             => {Eggs} 0.01028655  0.6086957 0.01689934 4.960687    14
## [39]  {Hot.Dogs,                                                                         
##        Vanilla.Wafers}          => {Eggs} 0.01175606  0.8000000 0.01469508 6.519760    16
## [40]  {X2pct..Milk,                                                                      
##        Vanilla.Wafers}          => {Eggs} 0.01028655  0.7000000 0.01469508 5.704790    14
## [41]  {Vanilla.Wafers,                                                                   
##        White.Bread}             => {Eggs} 0.01322557  0.7500000 0.01763409 6.112275    18
## [42]  {Hot.Dogs,                                                                         
##        Grape.Fruit.Roll}        => {Eggs} 0.01102131  0.7894737 0.01396032 6.433974    15
## [43]  {Grape.Fruit.Roll,                                                                 
##        X2pct..Milk}             => {Eggs} 0.01175606  0.6666667 0.01763409 5.433134    16
## [44]  {Grape.Fruit.Roll,                                                                 
##        White.Bread}             => {Eggs} 0.01175606  0.7619048 0.01542983 6.209296    16
## [45]  {Canned.Mixed.Fruit,                                                               
##        White.Bread}             => {Eggs} 0.01396032  0.7916667 0.01763409 6.451846    19
## [46]  {Tomatoes,                                                                         
##        Blueberry.Waffles}       => {Eggs} 0.01028655  0.9333333 0.01102131 7.606387    14
## [47]  {X2pct..Milk,                                                                      
##        Blueberry.Waffles}       => {Eggs} 0.01028655  0.6666667 0.01542983 5.433134    14
## [48]  {Blueberry.Waffles,                                                                
##        White.Bread}             => {Eggs} 0.01396032  0.7307692 0.01910360 5.955550    19
## [49]  {Tomatoes,                                                                         
##        Hot.Chocolate}           => {Eggs} 0.01102131  0.7500000 0.01469508 6.112275    15
## [50]  {X2pct..Milk,                                                                      
##        Hot.Chocolate}           => {Eggs} 0.01249082  0.6538462 0.01910360 5.328650    17
## [51]  {Hot.Chocolate,                                                                    
##        White.Bread}             => {Eggs} 0.01102131  0.5769231 0.01910360 4.701750    15
## [52]  {Paper.Plates,                                                                     
##        Bologna}                 => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [53]  {Paper.Plates,                                                                     
##        Tomatoes}                => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [54]  {Raisins,                                                                          
##        Paper.Plates}            => {Eggs} 0.01102131  0.7894737 0.01396032 6.433974    15
## [55]  {Hot.Dogs,                                                                         
##        Paper.Plates}            => {Eggs} 0.01175606  0.8421053 0.01396032 6.862906    16
## [56]  {Paper.Plates,                                                                     
##        X2pct..Milk}             => {Eggs} 0.01396032  0.7307692 0.01910360 5.955550    19
## [57]  {Paper.Plates,                                                                     
##        White.Bread}             => {Eggs} 0.01249082  0.7391304 0.01689934 6.023692    17
## [58]  {Bologna,                                                                          
##        Diet.Cola}               => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [59]  {Popcorn.Salt,                                                                     
##        Diet.Cola}               => {Eggs} 0.01102131  0.7894737 0.01396032 6.433974    15
## [60]  {Tomatoes,                                                                         
##        Diet.Cola}               => {Eggs} 0.01028655  0.8750000 0.01175606 7.130988    14
## [61]  {Hot.Dogs,                                                                         
##        Diet.Cola}               => {Eggs} 0.01102131  0.7500000 0.01469508 6.112275    15
## [62]  {X2pct..Milk,                                                                      
##        Diet.Cola}               => {Eggs} 0.01469508  0.7407407 0.01983835 6.036815    20
## [63]  {White.Bread,                                                                      
##        Diet.Cola}               => {Eggs} 0.01469508  0.8000000 0.01836885 6.519760    20
## [64]  {Frozen.Shrimp,                                                                    
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01102131  0.7894737 0.01396032 6.433974    15
## [65]  {Pancake.Mix,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [66]  {Popcorn.Salt,                                                                     
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01175606  0.8000000 0.01469508 6.519760    16
## [67]  {Tomatoes,                                                                         
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [68]  {Hot.Dogs,                                                                         
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01175606  0.7619048 0.01542983 6.209296    16
## [69]  {X2pct..Milk,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01542983  0.7500000 0.02057311 6.112275    21
## [70]  {White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01689934  0.7419355 0.02277737 6.046552    23
## [71]  {Bologna,                                                                          
##        Vegetable.Oil}           => {Eggs} 0.01102131  0.7142857 0.01542983 5.821215    15
## [72]  {Tomatoes,                                                                         
##        Vegetable.Oil}           => {Eggs} 0.01175606  0.9411765 0.01249082 7.670306    16
## [73]  {Raisins,                                                                          
##        Vegetable.Oil}           => {Eggs} 0.01028655  0.6666667 0.01542983 5.433134    14
## [74]  {Hot.Dogs,                                                                         
##        Vegetable.Oil}           => {Eggs} 0.01175606  0.7619048 0.01542983 6.209296    16
## [75]  {X2pct..Milk,                                                                      
##        Vegetable.Oil}           => {Eggs} 0.01469508  0.6666667 0.02204262 5.433134    20
## [76]  {Vegetable.Oil,                                                                    
##        White.Bread}             => {Eggs} 0.01175606  0.6666667 0.01763409 5.433134    16
## [77]  {Raisins,                                                                          
##        Cheese.Spread}           => {Eggs} 0.01028655  0.6666667 0.01542983 5.433134    14
## [78]  {X2pct..Milk,                                                                      
##        Cheese.Spread}           => {Eggs} 0.01249082  0.5862069 0.02130786 4.777411    17
## [79]  {White.Bread,                                                                      
##        Cheese.Spread}           => {Eggs} 0.01322557  0.6000000 0.02204262 4.889820    18
## [80]  {Bologna,                                                                          
##        Chicken.Soup}            => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [81]  {Popcorn.Salt,                                                                     
##        Chicken.Soup}            => {Eggs} 0.01249082  0.8500000 0.01469508 6.927246    17
## [82]  {Raisins,                                                                          
##        Chicken.Soup}            => {Eggs} 0.01175606  0.7619048 0.01542983 6.209296    16
## [83]  {Hot.Dogs,                                                                         
##        Chicken.Soup}            => {Eggs} 0.01175606  0.6400000 0.01836885 5.215808    16
## [84]  {X2pct..Milk,                                                                      
##        Chicken.Soup}            => {Eggs} 0.01616458  0.7333333 0.02204262 5.976447    22
## [85]  {Chicken.Soup,                                                                     
##        White.Bread}             => {Eggs} 0.01396032  0.6785714 0.02057311 5.530154    19
## [86]  {Hot.Dogs,                                                                         
##        Shampoo}                 => {Eggs} 0.01396032  0.7600000 0.01836885 6.193772    19
## [87]  {Shampoo,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01763409  0.8275862 0.02130786 6.744580    24
## [88]  {Shampoo,                                                                          
##        White.Bread}             => {Eggs} 0.01836885  0.6756757 0.02718589 5.506554    25
## [89]  {Popcorn.Salt,                                                                     
##        Spaghetti}               => {Eggs} 0.01102131  0.7500000 0.01469508 6.112275    15
## [90]  {Raisins,                                                                          
##        Spaghetti}               => {Eggs} 0.01175606  0.7619048 0.01542983 6.209296    16
## [91]  {Hot.Dogs,                                                                         
##        Spaghetti}               => {Eggs} 0.01175606  0.7272727 0.01616458 5.927055    16
## [92]  {X2pct..Milk,                                                                      
##        Spaghetti}               => {Eggs} 0.01469508  0.6896552 0.02130786 5.620483    20
## [93]  {Spaghetti,                                                                        
##        White.Bread}             => {Eggs} 0.01689934  0.7419355 0.02277737 6.046552    23
## [94]  {Frozen.Shrimp,                                                                    
##        Pancake.Mix}             => {Eggs} 0.01322557  0.7826087 0.01689934 6.378027    18
## [95]  {Frozen.Shrimp,                                                                    
##        Popcorn.Salt}            => {Eggs} 0.01396032  0.8636364 0.01616458 7.038378    19
## [96]  {Frozen.Shrimp,                                                                    
##        Tomatoes}                => {Eggs} 0.01322557  0.7500000 0.01763409 6.112275    18
## [97]  {Hot.Dogs,                                                                         
##        Frozen.Shrimp}           => {Eggs} 0.01396032  0.7307692 0.01910360 5.955550    19
## [98]  {Frozen.Shrimp,                                                                    
##        X2pct..Milk}             => {Eggs} 0.01689934  0.6052632 0.02792065 4.932714    23
## [99]  {Frozen.Shrimp,                                                                    
##        White.Bread}             => {Eggs} 0.01910360  0.7027027 0.02718589 5.726817    26
## [100] {Pancake.Mix,                                                                      
##        Bologna}                 => {Eggs} 0.01396032  0.6785714 0.02057311 5.530154    19
## [101] {Pancake.Mix,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01616458  0.8148148 0.01983835 6.640497    22
## [102] {Pancake.Mix,                                                                      
##        Tomatoes}                => {Eggs} 0.01322557  0.7826087 0.01689934 6.378027    18
## [103] {Raisins,                                                                          
##        Pancake.Mix}             => {Eggs} 0.01102131  0.6521739 0.01689934 5.315022    15
## [104] {Hot.Dogs,                                                                         
##        Pancake.Mix}             => {Eggs} 0.01689934  0.7666667 0.02204262 6.248104    23
## [105] {Pancake.Mix,                                                                      
##        X2pct..Milk}             => {Eggs} 0.02057311  0.7368421 0.02792065 6.005043    28
## [106] {Pancake.Mix,                                                                      
##        White.Bread}             => {Eggs} 0.02424688  0.7173913 0.03379868 5.846524    33
## [107] {Bologna,                                                                          
##        Popcorn.Salt}            => {Eggs} 0.01396032  0.6785714 0.02057311 5.530154    19
## [108] {Bologna,                                                                          
##        Tomatoes}                => {Eggs} 0.01836885  0.7142857 0.02571639 5.821215    25
## [109] {Raisins,                                                                          
##        Bologna}                 => {Eggs} 0.01542983  0.6562500 0.02351212 5.348241    21
## [110] {Hot.Dogs,                                                                         
##        Bologna}                 => {Eggs} 0.01542983  0.7000000 0.02204262 5.704790    21
## [111] {Bologna,                                                                          
##        X2pct..Milk}             => {Eggs} 0.02130786  0.7435897 0.02865540 6.060034    29
## [112] {Bologna,                                                                          
##        White.Bread}             => {Eggs} 0.02277737  0.6595745 0.03453343 5.375334    31
## [113] {Popcorn.Salt,                                                                     
##        Tomatoes}                => {Eggs} 0.01689934  0.7931034 0.02130786 6.463556    23
## [114] {Raisins,                                                                          
##        Popcorn.Salt}            => {Eggs} 0.01616458  0.7333333 0.02204262 5.976447    22
## [115] {Hot.Dogs,                                                                         
##        Popcorn.Salt}            => {Eggs} 0.01836885  0.6756757 0.02718589 5.506554    25
## [116] {X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.02718589  0.8222222 0.03306392 6.700865    37
## [117] {Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.02718589  0.7708333 0.03526819 6.282061    37
## [118] {Raisins,                                                                          
##        Tomatoes}                => {Eggs} 0.01322557  0.7200000 0.01836885 5.867784    18
## [119] {Hot.Dogs,                                                                         
##        Tomatoes}                => {Eggs} 0.01910360  0.7647059 0.02498163 6.232124    26
## [120] {X2pct..Milk,                                                                      
##        Tomatoes}                => {Eggs} 0.02571639  0.7954545 0.03232917 6.482716    35
## [121] {Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.02645114  0.6545455 0.04041146 5.334349    36
## [122] {Hot.Dogs,                                                                         
##        Raisins}                 => {Eggs} 0.01616458  0.6666667 0.02424688 5.433134    22
## [123] {Raisins,                                                                          
##        X2pct..Milk}             => {Eggs} 0.02130786  0.6590909 0.03232917 5.371394    29
## [124] {Raisins,                                                                          
##        White.Bread}             => {Eggs} 0.02424688  0.6875000 0.03526819 5.602919    33
## [125] {Hot.Dogs,                                                                         
##        X2pct..Milk}             => {Eggs} 0.02718589  0.6981132 0.03894195 5.689414    37
## [126] {Hot.Dogs,                                                                         
##        White.Bread}             => {Eggs} 0.02865540  0.7090909 0.04041146 5.778879    39
## [127] {X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.03600294  0.7000000 0.05143277 5.704790    49
## [128] {Merlot.Wine,                                                                      
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [129] {Hot.Dogs,                                                                         
##        Vanilla.Wafers,                                                                   
##        White.Bread}             => {Eggs} 0.01028655  0.8750000 0.01175606 7.130988    14
## [130] {Paper.Plates,                                                                     
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [131] {X2pct..Milk,                                                                      
##        White.Bread,                                                                      
##        Diet.Cola}               => {Eggs} 0.01175606  0.8421053 0.01396032 6.862906    16
## [132] {Frozen.Shrimp,                                                                    
##        White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01028655  0.8750000 0.01175606 7.130988    14
## [133] {Popcorn.Salt,                                                                     
##        White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [134] {X2pct..Milk,                                                                      
##        Tomatoes,                                                                         
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01028655  0.9333333 0.01102131 7.606387    14
## [135] {Tomatoes,                                                                         
##        White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01028655  0.8750000 0.01175606 7.130988    14
## [136] {Hot.Dogs,                                                                         
##        White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01028655  0.7777778 0.01322557 6.338656    14
## [137] {X2pct..Milk,                                                                      
##        White.Bread,                                                                      
##        Sharp.Cheddar.Cheese}    => {Eggs} 0.01396032  0.8260870 0.01689934 6.732361    19
## [138] {X2pct..Milk,                                                                      
##        White.Bread,                                                                      
##        Cheese.Spread}           => {Eggs} 0.01028655  0.6363636 0.01616458 5.186173    14
## [139] {X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        Chicken.Soup}            => {Eggs} 0.01028655  0.8750000 0.01175606 7.130988    14
## [140] {Popcorn.Salt,                                                                     
##        Chicken.Soup,                                                                     
##        White.Bread}             => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [141] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        Chicken.Soup}            => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [142] {X2pct..Milk,                                                                      
##        Chicken.Soup,                                                                     
##        White.Bread}             => {Eggs} 0.01249082  0.7727273 0.01616458 6.297496    17
## [143] {Hot.Dogs,                                                                         
##        Shampoo,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01249082  0.7727273 0.01616458 6.297496    17
## [144] {Hot.Dogs,                                                                         
##        Shampoo,                                                                          
##        White.Bread}             => {Eggs} 0.01102131  0.7142857 0.01542983 5.821215    15
## [145] {Shampoo,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01396032  0.7916667 0.01763409 6.451846    19
## [146] {Raisins,                                                                          
##        Spaghetti,                                                                        
##        White.Bread}             => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [147] {Hot.Dogs,                                                                         
##        Spaghetti,                                                                        
##        White.Bread}             => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [148] {X2pct..Milk,                                                                      
##        Spaghetti,                                                                        
##        White.Bread}             => {Eggs} 0.01175606  0.7272727 0.01616458 5.927055    16
## [149] {Frozen.Shrimp,                                                                    
##        Pancake.Mix,                                                                      
##        X2pct..Milk}             => {Eggs} 0.01028655  0.7777778 0.01322557 6.338656    14
## [150] {Frozen.Shrimp,                                                                    
##        Pancake.Mix,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.7777778 0.01322557 6.338656    14
## [151] {Frozen.Shrimp,                                                                    
##        X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01175606  0.8888889 0.01322557 7.244178    16
## [152] {Frozen.Shrimp,                                                                    
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01175606  1.0000000 0.01175606 8.149701    16
## [153] {Frozen.Shrimp,                                                                    
##        X2pct..Milk,                                                                      
##        Tomatoes}                => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [154] {Frozen.Shrimp,                                                                    
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01028655  0.7777778 0.01322557 6.338656    14
## [155] {Hot.Dogs,                                                                         
##        Frozen.Shrimp,                                                                    
##        X2pct..Milk}             => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [156] {Hot.Dogs,                                                                         
##        Frozen.Shrimp,                                                                    
##        White.Bread}             => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [157] {Frozen.Shrimp,                                                                    
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01322557  0.6923077 0.01910360 5.642100    18
## [158] {Pancake.Mix,                                                                      
##        Bologna,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01102131  0.7894737 0.01396032 6.433974    15
## [159] {Pancake.Mix,                                                                      
##        Bologna,                                                                          
##        White.Bread}             => {Eggs} 0.01175606  0.6666667 0.01763409 5.433134    16
## [160] {Pancake.Mix,                                                                      
##        X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01322557  0.9000000 0.01469508 7.334731    18
## [161] {Pancake.Mix,                                                                      
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01396032  0.9047619 0.01542983 7.373539    19
## [162] {Pancake.Mix,                                                                      
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [163] {Raisins,                                                                          
##        Pancake.Mix,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.7000000 0.01469508 5.704790    14
## [164] {Hot.Dogs,                                                                         
##        Pancake.Mix,                                                                      
##        X2pct..Milk}             => {Eggs} 0.01396032  0.8260870 0.01689934 6.732361    19
## [165] {Hot.Dogs,                                                                         
##        Pancake.Mix,                                                                      
##        White.Bread}             => {Eggs} 0.01469508  0.8333333 0.01763409 6.791417    20
## [166] {Pancake.Mix,                                                                      
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01689934  0.7931034 0.02130786 6.463556    23
## [167] {Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01249082  0.9444444 0.01322557 7.696939    17
## [168] {Bologna,                                                                          
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01322557  0.7826087 0.01689934 6.378027    18
## [169] {Hot.Dogs,                                                                         
##        Bologna,                                                                          
##        Tomatoes}                => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [170] {Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        Tomatoes}                => {Eggs} 0.01322557  0.8571429 0.01542983 6.985458    18
## [171] {Bologna,                                                                          
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01249082  0.7391304 0.01689934 6.023692    17
## [172] {Raisins,                                                                          
##        Bologna,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01102131  0.8333333 0.01322557 6.791417    15
## [173] {Raisins,                                                                          
##        Bologna,                                                                          
##        White.Bread}             => {Eggs} 0.01322557  0.7500000 0.01763409 6.112275    18
## [174] {Hot.Dogs,                                                                         
##        Bologna,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01322557  0.9000000 0.01469508 7.334731    18
## [175] {Hot.Dogs,                                                                         
##        Bologna,                                                                          
##        White.Bread}             => {Eggs} 0.01249082  0.8095238 0.01542983 6.597377    17
## [176] {Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01910360  0.8125000 0.02351212 6.621632    26
## [177] {X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        Tomatoes}                => {Eggs} 0.01396032  0.9500000 0.01469508 7.742216    19
## [178] {Popcorn.Salt,                                                                     
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01396032  0.8636364 0.01616458 7.038378    19
## [179] {Raisins,                                                                          
##        X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01175606  0.7272727 0.01616458 5.927055    16
## [180] {Raisins,                                                                          
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01322557  0.8181818 0.01616458 6.667937    18
## [181] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        Popcorn.Salt}            => {Eggs} 0.01469508  0.8333333 0.01763409 6.791417    20
## [182] {Hot.Dogs,                                                                         
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01542983  0.7777778 0.01983835 6.338656    21
## [183] {X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01983835  0.9000000 0.02204262 7.334731    27
## [184] {Raisins,                                                                          
##        X2pct..Milk,                                                                      
##        Tomatoes}                => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [185] {Raisins,                                                                          
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01249082  0.8095238 0.01542983 6.597377    17
## [186] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        Tomatoes}                => {Eggs} 0.01396032  0.9500000 0.01469508 7.742216    19
## [187] {Hot.Dogs,                                                                         
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01469508  0.7692308 0.01910360 6.269000    20
## [188] {X2pct..Milk,                                                                      
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.02130786  0.8285714 0.02571639 6.752609    29
## [189] {Hot.Dogs,                                                                         
##        Raisins,                                                                          
##        X2pct..Milk}             => {Eggs} 0.01102131  0.6521739 0.01689934 5.315022    15
## [190] {Hot.Dogs,                                                                         
##        Raisins,                                                                          
##        White.Bread}             => {Eggs} 0.01396032  0.7307692 0.01910360 5.955550    19
## [191] {Raisins,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01689934  0.7419355 0.02277737 6.046552    23
## [192] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.02204262  0.7894737 0.02792065 6.433974    30
## [193] {Hot.Dogs,                                                                         
##        Shampoo,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.7368421 0.01396032 6.005043    14
## [194] {Pancake.Mix,                                                                      
##        Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01028655  0.8235294 0.01249082 6.711518    14
## [195] {Pancake.Mix,                                                                      
##        X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01102131  0.9375000 0.01175606 7.640344    15
## [196] {Hot.Dogs,                                                                         
##        Pancake.Mix,                                                                      
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01175606  0.8421053 0.01396032 6.862906    16
## [197] {Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01175606  0.9411765 0.01249082 7.670306    16
## [198] {Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [199] {Raisins,                                                                          
##        Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [200] {Hot.Dogs,                                                                         
##        Bologna,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01102131  0.8823529 0.01249082 7.190912    15
## [201] {X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01175606  1.0000000 0.01175606 8.149701    16
## [202] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        Popcorn.Salt,                                                                     
##        White.Bread}             => {Eggs} 0.01249082  0.8500000 0.01469508 6.927246    17
## [203] {Raisins,                                                                          
##        X2pct..Milk,                                                                      
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01102131  0.9375000 0.01175606 7.640344    15
## [204] {Hot.Dogs,                                                                         
##        X2pct..Milk,                                                                      
##        Tomatoes,                                                                         
##        White.Bread}             => {Eggs} 0.01102131  0.9375000 0.01175606 7.640344    15
## [205] {Hot.Dogs,                                                                         
##        Raisins,                                                                          
##        X2pct..Milk,                                                                      
##        White.Bread}             => {Eggs} 0.01102131  0.6818182 0.01616458 5.556614    15

7 Plot Subrules by Lift

# Plot matrix for the first 6 rules sorted by lift
subrules <- head(sort(rules, by = "lift"), 6)
plot(subrules, method = "matrix", measure = "lift")
## Itemsets in Antecedent (LHS)
## [1] "{Canned.Mixed.Fruit,White.Bread}"     
## [2] "{White.Bread,Chicken.Breast...Frozen}"
## [3] "{Eggs,Chicken.Breast...Frozen}"       
## [4] "{Paper.Plates,White.Bread}"           
## [5] "{Eggs,Napkins}"                       
## [6] "{Merlot.Wine,X2pct..Milk}"            
## Itemsets in Consequent (RHS)
## [1] "{Raisins}"      "{Popcorn.Salt}" "{Shampoo}"

8 Right-Hand Side (RHS) of Rule with Highest Lift

# Identify the rule with the highest lift
highest_lift_rule <- sort(rules, by = "lift", decreasing = TRUE)[1]
rhs(highest_lift_rule)
## itemMatrix in sparse format with
##  1 rows (elements/transactions) and
##  40 columns (items)

```

9 Import Data and Data Management

10 Question 01