1 Goal


The goal of this tutorial is to access the dataframe containing the parameters of the rules created from transactions. This dataframe can be used later to plot, write csv files or be analysed.


2 Loading the data


# We need to load two libraries to perform this task
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Loading required package: grid
# In this tutorial we are going to use transactions file format.
# The file looks like:
# apple, orange, pear
# apple, pear
# orange
# etc

# First we load the data
products <- read.transactions("transactions.csv", sep =",", format("basket"),  rm.duplicates = TRUE)
## Warning in readLines(file, encoding = encoding): incomplete final line
## found on 'transactions.csv'
## distribution of transactions with duplicates:
## items
##   1   2 
## 191  10
products <- sample(products, 1000)

3 Create rules from transactions


# We create the rules using the arules library
RulesName<- apriori (products, parameter = list(supp = 0.01, conf = 0.1))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.1    0.1    1 none FALSE            TRUE       5    0.01      1
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 10 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[124 item(s), 1000 transaction(s)] done [0.00s].
## sorting and recoding items ... [85 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [621 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(RulesName))
##     lhs    rhs                        support confidence lift count
## [1] {}  => {Apple MacBook Pro}        0.101   0.101      1    101  
## [2] {}  => {Apple MacBook Air}        0.145   0.145      1    145  
## [3] {}  => {3-Button Mouse}           0.100   0.100      1    100  
## [4] {}  => {Lenovo Desktop Computer}  0.130   0.130      1    130  
## [5] {}  => {ViewSonic Monitor}        0.117   0.117      1    117  
## [6] {}  => {CYBERPOWER Gamer Desktop} 0.167   0.167      1    167
# Now we access the dataframe containing the parameters of the rules
head(RulesName@quality)
##   support confidence lift count
## 1   0.101      0.101    1   101
## 2   0.145      0.145    1   145
## 3   0.100      0.100    1   100
## 4   0.130      0.130    1   130
## 5   0.117      0.117    1   117
## 6   0.167      0.167    1   167
rules_df <- as.data.frame(RulesName@quality)
head(rules_df)
##   support confidence lift count
## 1   0.101      0.101    1   101
## 2   0.145      0.145    1   145
## 3   0.100      0.100    1   100
## 4   0.130      0.130    1   130
## 5   0.117      0.117    1   117
## 6   0.167      0.167    1   167
# Now we can plot support vs confidence
plot(rules_df$support, rules_df$confidence)


4 Conclusion


In this tutorial we have learnt how to access the dataframe containing the parameters from the rules created from transaction data.