Association Rules Worksheet Charles Pierre 2022-07-05
rm(list = ls())
library(mlbench)
## Warning: package 'mlbench' was built under R version 4.4.1
library(readr)
library(data.table)
## Warning: package 'data.table' was built under R version 4.4.1
library(stats)
library(arules)
## Warning: package 'arules' was built under R version 4.4.1
## Loading required package: Matrix
##
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
##
## abbreviate, write
library(arulesViz)
## Warning: package 'arulesViz' was built under R version 4.4.1
library(kernlab)
## Warning: package 'kernlab' was built under R version 4.4.1
##
## Attaching package: 'kernlab'
## The following object is masked from 'package:arules':
##
## size
transactions <- list(
T1 = c('paper','pens','pencils'),
T2 = c('pens','ruler'),
T3 = c('pencils','cards','ruler'),
T4 = c('paper','cards'),
T5 = c('pens','pencils','cards'),
T6 = c('paper','ruler','calculator'),
T7 = c('paper','pens','cards','folder'),
T8 = c('pencils','ruler','folder'),
T9 = c('paper','pencils','cards','ruler'),
T10 = c('pens','cards')
)
myrules <- apriori(data = transactions, parameter=list(support=0.25, target = 'frequent itemsets'))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## NA 0.1 1 none FALSE TRUE 5 0.25 1
## maxlen target ext
## 10 frequent itemsets TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 2
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[7 item(s), 10 transaction(s)] done [0.00s].
## sorting and recoding items ... [5 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## sorting transactions ... done [0.00s].
## writing ... [9 set(s)] done [0.00s].
## creating S4 object ... done [0.00s].
inspect(myrules)
## items support count
## [1] {pens} 0.5 5
## [2] {ruler} 0.5 5
## [3] {pencils} 0.5 5
## [4] {paper} 0.5 5
## [5] {cards} 0.6 6
## [6] {cards, pens} 0.3 3
## [7] {pencils, ruler} 0.3 3
## [8] {cards, pencils} 0.3 3
## [9] {cards, paper} 0.3 3
Sort Association Rules by Performance Metrics:
transactionsrules <-apriori(transactions, parameter = list(support=0.25, confidence = 0.25, minlen = 2))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.25 0.1 1 none FALSE TRUE 5 0.25 2
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 2
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[7 item(s), 10 transaction(s)] done [0.00s].
## sorting and recoding items ... [5 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [8 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
inspect(transactionsrules)
## lhs rhs support confidence coverage lift count
## [1] {pens} => {cards} 0.3 0.6 0.5 1.0 3
## [2] {cards} => {pens} 0.3 0.5 0.6 1.0 3
## [3] {ruler} => {pencils} 0.3 0.6 0.5 1.2 3
## [4] {pencils} => {ruler} 0.3 0.6 0.5 1.2 3
## [5] {pencils} => {cards} 0.3 0.6 0.5 1.0 3
## [6] {cards} => {pencils} 0.3 0.5 0.6 1.0 3
## [7] {paper} => {cards} 0.3 0.6 0.5 1.0 3
## [8] {cards} => {paper} 0.3 0.5 0.6 1.0 3
Plot Association Rules on Selected Metrics Dimensions
plot(transactionsrules, measure = c("support", "lift"), shading = "confidence")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.