Association Rules in R

Market Basket Analysis (MBA) or Affinity Analysis or Association Analysis is an analysis to understand combination and sequence of activities.  A simple non-technical description of Market Basket Analysis (MBA), you are advised to read blog - Retail Market Basket Analysis.

Detailed steps required for Market Basket Analysis are explained in this blog - Market Basket Analysis using R

Main objective of the blog is to prepare data for running association rules using R. Package arules provide required functions. We have created a hypothetical example and data for showing steps to transform data for running association rule. Data frame has information on quantities bought on each order at a retail store. Example data is in the below format.
order  <- c("O1","O1","O1","O2","O3","O3","O4","O5","O5","O5","O5","O6")
product <- c("P1","P2","P3","P4","P1","P4","P2","P1","P2","P4","P5","P6")
quantity <- c(1,2,3,1,3,4,2,4,5,6,7,1)

retail <- data.frame(order,product,quantity)

 

Since, we want to find association among products, we can ignore the quantities of product bought together. If you are keen to consider quantities, one of the option is to create duplicates based on combinations if that is going to be tricky. Transform data for Association or Market Basket Analysis and find combinations of the products bought together in an order. We can use aggregate and paste for getting output in the required format.
# Products in an order
order.level <-aggregate(retail$product, 
                        by=list(retail$order),
                        FUN = paste)

order.level <-order.level$x    # keep only product list

 

Install and load arules package for running association rules or Market Basket Analysis (MBA).
## install if not done already
# install.packages("arules")
## load library
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## 
## The following objects are masked from 'package:base':
## 
##     %in%, write
 

## help documentation
library(help=arules)

 

apriori function  requires class of input data to be transactions ; hence We need to convert list to transactions before using association algorithm. Now change the class of object order.level to transactions.
## Convert into "transaction" class
retail.trans <- as(order.level, "transactions")

 

Run apriori function to find associations or association rules between products bought together.

assoc.rules <- apriori(retail.trans, parameter = list(supp = 0.0001, conf = 0.2))

 

## 
## parameter specification:
##  confidence minval smax arem  aval originalSupport support minlen maxlen
##         0.2    0.1    1 none FALSE            TRUE   1e-04      1     10
##  target   ext
##   rules FALSE
## 
## algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE

 

## Warning: You chose a very low absolute support count of 0. You might run out of memory! Increase minimum support.
## apriori - find association rules with the apriori algorithm
## version 4.21 (2004.05.09)        (c) 1996-2004   Christian Borgelt
## set item appearances ...[0 item(s)] done [0.00s].

 

## set transactions ...[6 item(s), 6 transaction(s)] done [0.00s].
## sorting and recoding items ... [6 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.01s].
## writing ... [38 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].

 

 Considering, size of data , not useful insights can be generated in this example. We have written  a detailed blog on Market Basket Analysis (MBA) using R.

Leave a Comment