Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package arules in R: getting rules with only one item in the left-hand side

Tags:

r

arules

I am using the package arules in R to generate association rules. I would like to restrict the rules so that in the left-hand side there's only one particular element, let's call it "potatoe".

If I do this:

rules <- apriori(dtm.mat, parameter = list(sup = 0.4, conf =
0.9,target="rules"), appearance = list(lhs = c("potatoe")))

I get "potatoe" on the lhs, but also all other kinds of things. How can I force that the rules contain only one element? The parameter maxlen don't do what I want, because, as far as I can see, I cannot specify the maxlen to apply to the elements on the left.

like image 599
user2405243 Avatar asked Oct 05 '22 02:10

user2405243


1 Answers

Assuming that you've generated the rules ("rules", in your question), here is how to subset it. Basically, you have to coerce the data into a data frame and then subset it.

#Here are the original rules generated with some data I created
# categories are "G", "T", "D", and "potatoe"

> inspect(rules);
lhs        rhs      support    confidencelift
1  {}  => {T}       0.3333333  0.3333333 1.0000000
2  {}  => {G}       0.5000000  0.5000000 1.0000000
3  {}  => {potatoe} 0.5000000  0.5000000 1.0000000
4  {}  => {D}       0.5000000  0.5000000 1.0000000
5  {T} => {G}       0.1666667  0.5000000 1.0000000
6  {G} => {T}       0.1666667  0.3333333 1.0000000
7  {T} => {D}       0.1666667  0.5000000 1.0000000
8  {D} => {T}       0.1666667  0.3333333 1.0000000
9  {G} => {potatoe} 0.1666667  0.3333333 0.6666667
10 {potatoe} => {G} 0.1666667  0.3333333 0.6666667
11 {potatoe} => {D} 0.3333333  0.6666667 1.3333333
12 {D} => {potatoe} 0.3333333  0.6666667 1.3333333

#Coerce into data frame
as(rules, "data.frame");

#Restrict LHS to only certain value (here, "potatoe")
rules_subset <- subset(rules, (lhs %in% c("potatoe")));

#Check to see subset rules
inspect(rules_subset);
lhs            rhs support    confidencelift
1 {potatoe} => {G} 0.1666667  0.3333333 0.6666667
2 {potatoe} => {D} 0.3333333  0.6666667 1.3333333

This method also allows for arbitrarily many LHS values, not just one. Much easier than my previously proposed answer.

like image 111
user2432675 Avatar answered Oct 19 '22 17:10

user2432675