wag
wag

Reputation: 11

R- Using arules sparse matrix output as input for another package?

I used arules to build a sparse matrix out of transaction data and got some nice rules. Now I'd like to use this matrix as an input for market basket analysis. Apparently, the itemMatrix class can be coerced into ncGMatrix class for use in another package, but I'm not sure how. Any help would be appreciated.

Original data had multiple items purchased from each customer (dwid)

dwid    Product.Colorblind
310975  Candy
310975  Fake doodie
310975  House slippers
310975  Canadian flags
310975  Ham
310990  Fake doodie
310990  Candy
310990  Turtle food

I read these in as transaction data and found some nice rules.

dataset <- read.transactions(file="Just Colorblind.csv",format="single",sep=",",cols=c("dwid","Product.Colorblind"),rm.duplicates=TRUE)
summary(dataset)
itemFrequencyPlot(dataset, topN = 40)

rules <- apriori(data = dataset, parameter = list(minlen = 2, support = 0.005, confidence = 0.1))

Now, I'm just trying to make a sparse matrix out of the rules object

binary_activity_matrix <- as(rules.itemMatrix, "ngCMatrix")

Any ideas?

Upvotes: 0

Views: 452

Answers (1)

Michael Hahsler
Michael Hahsler

Reputation: 3075

The rules object contains two sparse matrices, one for the LHS and one for the RHS. You have the following options:

as(lhs(rules.itemMatrix), "ngCMatrix")
as(rhs(rules.itemMatrix), "ngCMatrix")
as(items(rules.itemMatrix), "ngCMatrix")

items produces the union of the LHS and the RHS. See ? rules to learn about these methods.

Upvotes: 0

Related Questions