Search code examples
rarulesmarket-basket-analysis

arules subset not working in the new version (1.5-2) of arules package


I recently updated by Arules package to version 1.5-2, ever since I am not able to subset item list from Rule set, I get the following error when I try to subset "Error in structure(seq(length(labels)), names = labels)[i] : invalid subscript type 'S4'" here is an example

library(arules)
data("Groceries")

#generate Rules without second record to create an itemset
rules <- apriori(Groceries[-2],parameter = list(supp = 0.05, conf = 0.2,target = "rules"))

#Create an itemlist with second record
enter code here`basket <- Groceries[2]
rulesMatchLHS <- is.subset(rules@lhs,basket)
suitableRules <- rulesMatchLHS & !(is.subset(rules@rhs,basket))
#output
inspect(rules[suitableRules])

Thanks in advance


Solution

  • Your subset vector looks like a sparse matrix, but a dense logical vector is needed:

    suitableRules
    
    7 x 1 sparse Matrix of class "lgCMatrix"
                       {tropical fruit,yogurt,coffee}
    {}                                              |
    {yogurt}                                        |
    {whole milk}                                    .
    {rolls/buns}                                    .
    {whole milk}                                    .
    {other vegetables}                              .
    {whole milk}                                    .
    
    as.logical(suitableRules)
    [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
    
    inspect(rules[as.logical(suitableRules)])
        lhs         rhs          support   confidence lift     count
    [1] {}       => {whole milk} 0.2555420 0.2555420  1.000000 2513 
    [2] {yogurt} => {whole milk} 0.0560301 0.4018964  1.572722  551