Search code examples
raprioriarules

R: overwrite rules that apriori produced


I want to overwrite confidence values of an apriori output, then put the output into is.redundant. I got an error at the last line. How do you do it?

library(arules)
data(Groceries) # read sample data
# find apriori rules
outApriori = apriori(Groceries, 
                     parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4)
                     ,appearance = list(rhs = "whole milk"  ) )
dfApriori = as.data.frame(inspect(outApriori[1:5])) # convert into data.frame
# modify the confidence value conservatively by adding one error sample
(estimateConfidence= dfApriori$count / (1 + round( dfApriori$count / dfApriori$confidence ) ))
dfApriori$confidence = estimateConfidence
outRmRedundant <- dfApriori[!is.redundant(dfApriori)] # Error in (function (classes, fdef, mtable)  : 

# Error in (function (classes, fdef, mtable)  : 
#             unable to find an inherited method for function ‘is.redundant’ for signature ‘"data.frame"’

Solution

  • The function is.redundant() expects a rules object not a data.frame. Here is how you change the quality slot of the rules object:

    library(arules)
    data(Groceries)
    # find apriori rules
    rules <- apriori(Groceries, 
      parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4),
      appearance = list(rhs = "whole milk"))
    
    estimatedConfidence <- quality(rules)$count / (1 + round(quality(rules)$count / quality(rules)$confidence))
    
    quality(rules)$confidence <- estimatedConfidence
    
    rules.nonredundant <- rules[!is.redundant(rules)] 
    inspect(head(rules.nonredundant))
    

    BTW: You might want to look at Laplace Corrected Confidence (http://michael.hahsler.net/research/association_rules/measures.html#laplace) which can be calculated using the function interestMeasure().