0

In transactions, you can have in the itemInfo labels and levels. Could I create rules with "Apriori" using the different levels of the transaction? I would like that certain values in Level1 only appears in the left side of the rule (lhs). I'm only able to select items using the labels.

One example using the public dataset "Groceries":

data("Groceries")
str(Groceries)
str(Groceries@itemInfo)

You can see that Groceries@iteminfo has labels, level2 and level1. I could apply the function apriori for creating rules that only had "whole milk" (one of the labels) on the right side (rhs).

library("arules")    
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.08), 
               appearance = list(default="lhs",rhs="whole milk"),
               control = list(verbose=F))
inspect(head(rules))

But I don't know how to do the same action based on the different levels (for example, in this example could be "baby food" or " bags") and not in the labels.

Nell
  • 21
  • 4
  • Welcome to SO! Please do not use images to communicate textual content. And please create a [minimal example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of what you have tried. – Ralf Stubner Mar 21 '18 at 15:45
  • Thanks, I'll take it into account, I added what I've tried to do using labels. – Nell Mar 21 '18 at 16:14
  • You still need to give sample data: dput(head(df,20)). As well as show us what an ideal output looks like. – leeum Mar 21 '18 at 16:26
  • Sorry for the mistakes, I've tried to fix it adding an example of what I would like to do (using a public dataset). – Nell Mar 21 '18 at 21:42

1 Answers1

1

Indeed you can! This is taken form the manual page for aggregate() in arules and adapted to your problem:

library("arules")
data("Groceries")
itemInfo(Groceries)

### add level 2 names as additional items
Groceries_multilevel <- addAggregate(Groceries, by = "level2", 
    postfix = "_lvl2")
itemLabels(Groceries_multilevel)

rules <- apriori(data=Groceries_multilevel, 
    parameter=list(supp=0.001,conf = 0.1), 
    appearance = list(default="lhs", rhs="bags_lvl2"))

### filter items so you do not get rules like plastic bags -> bags_lvl2
rules <- filterAggregate(rules)

inspect(head(rules, by = "lift"))

   lhs                          rhs             support confidence     lift count
[1] {rolls/buns,                                                                      
 soda,                                                                            
 canned beer,                                                                     
 sausage_lvl2}                => {bags_lvl2} 0.001016777  0.6666667 6.738609    10
[2] {rolls/buns,                                                                      
 canned beer,                                                                     
 non-alc. drinks_lvl2,                                                            
 sausage_lvl2}                => {bags_lvl2} 0.001321810  0.6190476 6.257280    13
[3] {hard drinks_lvl2,                                                                
 non-alc. drinks_lvl2,                                                            
 sausage_lvl2}                => {bags_lvl2} 0.001016777  0.5882353 5.945832    10
Michael Hahsler
  • 2,965
  • 1
  • 12
  • 16