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 ... [619 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.115 0.115 1 115
## [2] {} => {Apple MacBook Air} 0.143 0.143 1 143
## [3] {} => {Microsoft Wireless Desktop Keyboard and Mouse} 0.113 0.113 1 113
## [4] {} => {ViewSonic Monitor} 0.109 0.109 1 109
## [5] {} => {Apple Earpods} 0.181 0.181 1 181
## [6] {} => {CYBERPOWER Gamer Desktop} 0.190 0.190 1 190
# 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.019 0.1743119 2.857573
## [2] {HP Laptop} => {ASUS Chromebook} 0.024 0.1263158 2.070751
## [3] {Dell Desktop,iMac} => {ASUS Chromebook} 0.010 0.1587302 2.602134
## count
## [1] 19
## [2] 24
## [3] 10
# 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
## [1] {ASUS Chromebook} => {ViewSonic Monitor} 0.019
## [2] {ASUS Chromebook} => {CYBERPOWER Gamer Desktop} 0.015
## [3] {ASUS Chromebook} => {Dell Desktop} 0.013
## [4] {ASUS Chromebook} => {Lenovo Desktop Computer} 0.014
## [5] {ASUS Chromebook} => {HP Laptop} 0.024
## [6] {ASUS Chromebook} => {iMac} 0.023
## [7] {ASUS Chromebook,Dell Desktop} => {iMac} 0.010
## [8] {ASUS Chromebook,iMac} => {Dell Desktop} 0.010
## confidence lift count
## [1] 0.3114754 2.857573 19
## [2] 0.2459016 1.294219 15
## [3] 0.2131148 1.357419 13
## [4] 0.2295082 1.416717 14
## [5] 0.3934426 2.070751 24
## [6] 0.3770492 1.520360 23
## [7] 0.7692308 3.101737 10
## [8] 0.4347826 2.769316 10
In this tutorial we have learnt how to select rules with one specific product on the rules