Search code examples
rarulesrecommendation-enginerecommenderlab

R how to get association rules (LHS, RHS, support, confidence, lift) from recommenderlab object?


i'm current construct product recommendation using R recommenderlab, after compute AR recommender, i'm hoping to understand the association rules, but i couldn't found any why to extract the complete association rules from the recommender object.

Below is the sample dataset

m <- matrix(sample(c(0,1), 50, replace=TRUE), nrow=5, ncol=10,
            dimnames=list(users=paste("u", 1:5, sep=''),
                           items=paste("i", 1:10, sep='')))

Convert matrix into binaryRatingMatrix

b <- as(m, "binaryRatingMatrix")

create a user-based CF recommender using training data

r <- Recommender(getData(scheme, "train"), "AR")

Looking into the AR recommender object r@model$rule_base i found "rule_base"

Formal class 'Recommender' [package "recommenderlab"] with 5 slots
  ..@ method  : chr "AR"
  ..@ dataType: chr "binaryRatingMatrix"
  ..@ ntrain  : int 5
  ..@ model   :List of 9
  .. ..$ description    : chr "AR: rule base"
  .. ..$ rule_base      :Formal class 'rules' [package "arules"] with 4 slots
  .. .. .. ..@ lhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
  .. .. .. .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
  .. .. .. .. .. .. .. ..@ i       : int [1:145] 1 1 1 1 2 0 0 0 5 5 ...
  .. .. .. .. .. .. .. ..@ p       : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. .. .. .. .. ..@ Dim     : int [1:2] 10 79
  .. .. .. .. .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. ..@ factors : list()
  .. .. .. .. .. ..@ itemInfo   :'data.frame':  10 obs. of  1 variable:
  .. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
  .. .. .. .. .. ..@ itemsetInfo:'data.frame':  0 obs. of  0 variables
  .. .. .. ..@ rhs    :Formal class 'itemMatrix' [package "arules"] with 3 slots
  .. .. .. .. .. ..@ data       :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots
  .. .. .. .. .. .. .. ..@ i       : int [1:79] 6 4 9 3 8 4 9 3 6 3 ...
  .. .. .. .. .. .. .. ..@ p       : int [1:80] 0 1 2 3 4 5 6 7 8 9 ...
  .. .. .. .. .. .. .. ..@ Dim     : int [1:2] 10 79
  .. .. .. .. .. .. .. ..@ Dimnames:List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. ..@ factors : list()
  .. .. .. .. .. ..@ itemInfo   :'data.frame':  10 obs. of  1 variable:
  .. .. .. .. .. .. ..$ labels: chr [1:10] "i1" "i2" "i3" "i4" ...
  .. .. .. .. .. ..@ itemsetInfo:'data.frame':  0 obs. of  0 variables
  .. .. .. ..@ quality:'data.frame':    79 obs. of  4 variables:
  .. .. .. .. ..$ support   : num [1:79] 0.2 0.2 0.2 0.2 0.4 0.4 0.4 0.4 0.4 0.4 ...
  .. .. .. .. ..$ confidence: num [1:79] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. ..$ lift      : num [1:79] 1.67 1.25 1.25 1.25 1.67 ...
  .. .. .. .. ..$ count     : num [1:79] 1 1 1 1 2 2 2 2 2 2 ...
  .. .. .. ..@ info   :List of 4
  .. .. .. .. ..$ data         : symbol data
  .. .. .. .. ..$ ntransactions: int 5
  .. .. .. .. ..$ support      : num 0.1
  .. .. .. .. ..$ confidence   : num 0.8
  .. ..$ support        : num 0.1
  .. ..$ confidence     : num 0.8
  .. ..$ maxlen         : num 3
  .. ..$ sort_measure   : chr "confidence"
  .. ..$ sort_decreasing: logi TRUE
  .. ..$ apriori_control:List of 1
  .. .. ..$ verbose: logi FALSE
  .. ..$ verbose        : logi FALSE
  ..@ predict :function (model, newdata, n = 10, data = NULL, type = c("topNList", "ratings", "ratingMatrix"), ...)  

Question: how can i extract the association rules from recommender object as data frame?

  • Get association rules dataframe with columns (LHS, RHS, support, confidence, lift, count)

Solution

  • you can use

    #Convert rules into data frame
    rules3 = as(rules, "data.frame")