I am doing a function that given a transaction x and a set of rules y, if x as a whole is a subset of y then I am interested in it cause I can do a recommendation based on the rules (I am using "Groceries" dataset) I am trying to do this using %ain%
but as crazy as it seems RStudio is not recognizing it, I will leave you my code and the error it throws.
install.packages("arules")
library(arules)
myfunction <- function(t,w,z){
lav <- which (t %ain% w,arr.ind=TRUE)
lav <- z[lav,]
lav <- unique(lav)
return (lav)
}
data("Groceries")
x <- list(c("pip fruit","root vegetables","yogurt","soda","fruit/vegetable juice"))
reglas = apriori(Groceries, parameter=list(supp=0.0006, conf=0.98))
t <- as(x,"transactions")
z <- slot(reglas,"rhs")
w <- slot(reglas,"lhs")
inspect(myfunction(t,w,z))
and this is the error:
error in evaluating the argument 'x' in selecting a method for function 'which': Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘%ain%’ for signature ‘"transactions", "itemMatrix"’
The error says it all.
error in evaluating the argument 'x' in selecting a method for function 'which': Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘%ain%’ for signature ‘"transactions", "itemMatrix"’
?'%ain%'
says %ain%
is defined as signature(x = "itemMatrix", table = "character")
.
In your case, your x
has class 'transactions', not 'itemMatrix'. And your table w
has class "itemMatrix", not "character".
If you want to see if any of the itemsets in w
contain any of the items in t
('pip fruit', etc), you will have to
w %ain% t # not t %ain% w
where t
is a CHARACTER vector (i.e. x[[1]]
in your example), so you'd have to write something that extracts a character vector from your 'transations' class.
If the opposite direction is actually what you want (t %ain% w
), you will have to somehow coerce your t
(class "transactions") into an itemMatrix, and coerce your w
(class "itemMatrix") into a character vector.
Also, I think you might be misunderstanding what %ain% does: It
returns a logical vector indicating if a row (itemset) in ‘x’ contains any of the items specified in ‘table’.
so the arr.ind
in the which
probably has no effect here - the result of %ain%
is not a matrix but a vector.