Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - arules apriori Error in length(obj) : Method length not implemented for class rules

I am attempting to make an association rules set using apriori - I am using a different dataset but the starwars dataset contains similar issues. Using arules I was attempting to list the rules and apply an arulesViz plot. From my understanding all strings must be ran as factors, listed as transactions and then apriori should be functioning properly but I get the ouput below after running the following code and rules is not added to environment:

install.packages("arules")
install.packages("arulesViz")
library(arulesViz)
library(arules)
data <- starwars[,c(4:6,8:10)]
data <- data.frame(sapply(data,as.factor))
data <- as(data, "transactions")
rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))


inspect(rules)
inspect(sort(rules))

subrules <- head(sort(rules, by="lift"), 10)
plot(subrules, method="graph")

The following is the output from running apriori

rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target   ext
        0.8    0.1    1 none FALSE            TRUE       5    0.15      1     10  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 78 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[131 item(s), 522 transaction(s)] done [0.00s].
sorting and recoding items ... [0 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 done [0.00s].
writing ... [0 rule(s)] done [0.00s].
creating S4 object  ... done [0.02s].
Error in length(obj) : Method length not implemented for class rules 

I have also ran this with the following argument changes

target = "rules"

And have attempted to run with using only null arguments

Any help is greatly appreciated!

like image 201
Michael Cantrall Avatar asked Mar 15 '18 20:03

Michael Cantrall


1 Answers

If I run your code with starwars data, I get following results -

> data <- starwars[,c(4:6,8:10)]
> data <- data.frame(sapply(data,as.factor))
> data <- as(data, "transactions")
> rules <- apriori(data, parameter = list(supp = 0.15, conf = 0.80))
Apriori

Parameter specification:
 confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target   ext
        0.8    0.1    1 none FALSE            TRUE       5    0.15      1     10  rules FALSE

Algorithmic control:
 filter tree heap memopt load sort verbose
    0.1 TRUE TRUE  FALSE TRUE    2    TRUE

Absolute minimum support count: 13 

set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[147 item(s), 87 transaction(s)] done [0.00s].
sorting and recoding items ... [8 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 done [0.00s].
writing ... [3 rule(s)] done [0.00s].
creating S4 object  ... done [0.00s].

As you can see clearly, that there are 3 rules generated. Which means If I run inspect - I see following:

  lhs                  rhs             support   confidence lift    
[1] {skin_color=fair} => {species=Human} 0.1839080 0.9411765  2.339496
[2] {skin_color=fair} => {gender=male}   0.1609195 0.8235294  1.155598
[3] {eye_color=brown} => {species=Human} 0.1954023 0.8095238  2.012245

But if I run the same by increasing support count, I would have have 0 rules generated(so in your case - absolute support count is 78 for starwars dataset when you have only 87 observations).

So you need to reduce(or adjust) the support or confidence and so that you have atleast 1 rule or more than that. Also, the target = "rules" could not help as you can see that it is generating 0 rules.

like image 105
mukund Avatar answered Sep 20 '22 16:09

mukund