The goal of this tutorial is to read the rules containing one specific product on the right or left side of the rule.
# 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)
# 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 ...[125 item(s), 1000 transaction(s)] done [0.00s].
## sorting and recoding items ... [87 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [764 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
inspect(head(RulesName))
## lhs rhs support confidence lift count
## [1] {} => {Microsoft Wireless Desktop Keyboard and Mouse} 0.105 0.105 1 105
## [2] {} => {Apple MacBook Air} 0.159 0.159 1 159
## [3] {} => {Apple MacBook Pro} 0.124 0.124 1 124
## [4] {} => {Acer Desktop} 0.110 0.110 1 110
## [5] {} => {ViewSonic Monitor} 0.125 0.125 1 125
## [6] {} => {Apple Earpods} 0.168 0.168 1 168
# We can choose one specific product on the right side of the transaction
inspect(subset(RulesName, (rhs %in% c("ASUS Chromebook"))))
## lhs rhs support confidence lift
## [1] {ViewSonic Monitor} => {ASUS Chromebook} 0.017 0.136 2.615385
## count
## [1] 17
# We can also choose one specific product to appear on the left side of the transaction
inspect(subset(RulesName, (lhs %in% c("ASUS Chromebook"))))
## lhs rhs support confidence
## [1] {ASUS Chromebook} => {ViewSonic Monitor} 0.017 0.3269231
## [2] {ASUS Chromebook} => {Apple Earpods} 0.011 0.2115385
## [3] {ASUS Chromebook} => {Lenovo Desktop Computer} 0.013 0.2500000
## [4] {ASUS Chromebook} => {HP Laptop} 0.017 0.3269231
## [5] {ASUS Chromebook} => {iMac} 0.022 0.4230769
## lift count
## [1] 2.615385 17
## [2] 1.259158 11
## [3] 1.879699 13
## [4] 1.626483 17
## [5] 1.620984 22
In this tutorial we have learnt how to select rules with one specific product on the rules