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
## Warning: failed to assign NativeSymbolInfo for lhs since lhs is already
## defined in the 'lazyeval' namespace
## Warning: failed to assign NativeSymbolInfo for rhs since rhs is already
## defined in the 'lazyeval' namespace
library(datasets)
#Loading the dataset
data(Groceries)
## Creating an item frequency plot for the top 20 items
itemFrequencyPlot(Groceries,topN=20,type="absolute")

#get the rules
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.001 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: 9
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.01s].
## sorting and recoding items ... [157 item(s)] done [0.00s].
## creating transaction tree ... done [0.01s].
## checking subsets of size 1 2 3 4 5 6 done [0.07s].
## writing ... [410 rule(s)] done [0.01s].
## creating S4 object ... done [0.00s].
# Show the top 5 rules, but only 2 digits
options(digits=2)
inspect(rules[1:5])
## lhs rhs support confidence lift
## [1] {liquor,red/blush wine} => {bottled beer} 0.0019 0.90 11.2
## [2] {curd,cereals} => {whole milk} 0.0010 0.91 3.6
## [3] {yogurt,cereals} => {whole milk} 0.0017 0.81 3.2
## [4] {butter,jam} => {whole milk} 0.0010 0.83 3.3
## [5] {soups,bottled beer} => {whole milk} 0.0011 0.92 3.6
#Sorting
rules<-sort(rules, by="confidence", decreasing=TRUE)
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8,maxlen=3))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.001 1
## maxlen target ext
## 3 rules FALSE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 9
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[169 item(s), 9835 transaction(s)] done [0.00s].
## sorting and recoding items ... [157 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(Groceries, parameter = list(supp = 0.001, conf =
## 0.8, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
## done [0.00s].
## writing ... [29 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
# Redundancies
subset.matrix <- is.subset(rules, rules)
subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA
redundant <- colSums(subset.matrix, na.rm=T) >= 1
rules.pruned <- rules[!redundant]
rules<-rules.pruned
# Targetting items
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.08), appearance = list(default="lhs",rhs="whole milk"),control = list(verbose=F))
rules<-sort(rules, decreasing=TRUE,by="confidence")
inspect(rules[1:5])
## lhs rhs support confidence lift
## [1] {rice,
## sugar} => {whole milk} 0.0012 1 3.9
## [2] {canned fish,
## hygiene articles} => {whole milk} 0.0011 1 3.9
## [3] {root vegetables,
## butter,
## rice} => {whole milk} 0.0010 1 3.9
## [4] {root vegetables,
## whipped/sour cream,
## flour} => {whole milk} 0.0017 1 3.9
## [5] {butter,
## soft cheese,
## domestic eggs} => {whole milk} 0.0010 1 3.9
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.15,minlen=2), appearance = list(default="rhs",lhs="whole milk"),control = list(verbose=F))
rules<-sort(rules, decreasing=TRUE,by="confidence")
inspect(rules[1:5])
## lhs rhs support confidence lift
## [1] {whole milk} => {other vegetables} 0.075 0.29 1.5
## [2] {whole milk} => {rolls/buns} 0.057 0.22 1.2
## [3] {whole milk} => {yogurt} 0.056 0.22 1.6
## [4] {whole milk} => {root vegetables} 0.049 0.19 1.8
## [5] {whole milk} => {tropical fruit} 0.042 0.17 1.6
#Visualization
plot(rules[1:5],method="graph",interactive=F)
