Search code examples
rarules

R arules: Generating closed assoc. rules with appearance restrictions


I have a set of transactions which contain items from two classes (A and B), and I am wanting to generate closed association rules where the antecedent is only comprised of items from Class A and the consequent from Class B. For example, I am looking for rules of the form:

  • {A1} => {B2}
  • {A2, A3} => {B1}
  • {A3, A1} => {B3}

I can accomplish either of these easily independently, but am having trouble producing rules with both of these constraints.

rules <- apriori(
  trans,
  parameter = list(minlen=2, maxlen=4),
  appearance = list(lhs = class_A, rhs = class_B, default='none')
)

The code above generates rules which conform to the appearance constraints, but which are not closed. I have been unable to find a parameter to apriori to generate closed rules.

I am able to generate closed rules with the following code, but have been unsuccessful in passing ruleInduction an appearance parameter.

closed_is <- apriori(
  trans,
  parameter = list(minlen=2, maxlen=4, target="closed frequent itemsets"),
)

closed_rules <- ruleInduction(
  closed_is,
  transactions = trans
)

I have attempted to use subset to apply the desired lhs/rhs class constraint after generating close rules, but have been unsuccessful. For example,

target_lhs_rules <- subset(closed_rules, subset = lhs %in% as.character(class_A))

gives all of the rules which have at least one item from Class A, but not only of Class A.

target_lhs_rules <- subset(closed_rules, subset = lhs %ain% as.character(class_A))

results in no rules, since all items in Class A do not appear in any rules.

Surely the arules package covers this case, but I have not been able to find the way to do so. Any help overcoming this would be much appreciated!


Solution

  • %ain% is not the right operator, since, as you said, it requires all items to be in the itemset. You need the following code:

    setGeneric("%oin%",
        function(x, table) standardGeneric("%oin%"))
    
    setMethod("%oin%", signature(x = "itemMatrix", table = "character"),
      function(x, table) {
        pos <- match(table, itemLabels(x))
        if (any(is.na(pos)))
          stop("table contains an unknown item label" )
        size(x[, -pos]) == 0
      }
    )
    

    %oin% returns true as long as the itemset only contains items specified in table. I will add this code to the next release of arules.