有没有办法根据级别创建规则? (R,交易)

时间:2018-03-21 15:16:55

标签: r transactions apriori

在交易中,您可以拥有itemInfo标签和级别。我可以使用不同级别的交易创建“Apriori”规则吗?我希望Level1中的某些值仅出现在规则的左侧(lhs)。我只能使用标签选择项目。

使用公共数据集“Groceries”的一个示例:

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

你可以看到Groceries @ iteminfo有标签 level2 level1 。我可以应用函数apriori来创建只有右侧(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))

但我不知道如何根据不同的等级做同样的动作(例如,在这个例子中可能是“婴儿食品”或“袋子”)而不是标签。

1 个答案:

答案 0 :(得分:0)

确实你可以!这是从aggregate()arules的手册页进行的,并根据您的问题进行了调整:

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