如何在 R 中按 lhs 或 rhs 对关联规则进行排序

How to sort association rules by lhs or rhs in R

我有一组先验算法的规则。按提升度、信心或支持度对它们进行排序很容易:

rules.sorted = sort(rules, by="lift")

但假设我有一小组规则,其中包含一些不同的 rhs 元素。 我想查看按那些 rhs 元素排序的规则(按字母顺序)。有办法吗?

即来自:

    lhs       rhs support   confidence lift    
1   {A}    => {B} 0.3919252 0.9431280  1.930940
2   {B}    => {A} 0.3919252 0.8024194  1.930940
3   {E,C}  => {A} 0.3535204 0.7995546  1.924047
4   {F,I}  => {F} 0.3924175 0.9005650  1.868281
5   {H}    => {G} 0.4194978 0.9659864  1.864941
6   {C,D}  => {A} 0.3653373 0.7141482  1.718525

为此:

    lhs       rhs support   confidence lift    

2   {B}    => {A} 0.3919252 0.8024194  1.930940
3   {E,C}  => {A} 0.3535204 0.7995546  1.924047
6   {C,D}  => {A} 0.3653373 0.7141482  1.718525
1   {A}    => {B} 0.3919252 0.9431280  1.930940
4   {F,I}  => {F} 0.3924175 0.9005650  1.868281
5   {H}    => {G} 0.4194978 0.9659864  1.864941

这是我使用的规则示例:

library(arules)
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data", "mush.data");
dataset = read.table("mush.data", header = F, sep=",", na.strings= "*")
tr <- as(dataset, "transactions")
param  = new("APparameter", "confidence" = 0.9, "support" = 0.7, "minlen"= 2L, "maxlen" = 2L, "target" = "rules") 
rules <-apriori(tr,param)
dput(rules)

从您的 rules 对象中提取基本信息到数据框,如下所示:

rules_info <-
  data.frame(
    LHS = labels(lhs(rules)), 
    RHS = labels(rhs(rules)),          
    quality(rules)
  )

然后像普通数据框一样排序:

rules_info[ order(rules_info$RHS), ]

bergant 的解决方案正是您所需要的。我会直接重新排序规则对象中的规则:

rules_sorted <- rules[order(labels(rhs(rules)))]

新规则对象现已排序。