Using Market Basket Analysis as a Recommender Engine
local graph
Chapter 7 — Using Market Basket Analysis as a Recommender Engine
Section titled “Chapter 7 — Using Market Basket Analysis as a Recommender Engine”“It’s not wise to violate the rules until you know how to observe them.”
- T.S. Eliot
In this chapter, we will cover the following topics:
- Market basket analysis using the arules package
- Data transformation and cleaning techniques using semi-structured market basket transaction data
- Learn how to transform transaction objects into dataframes
- Use cluster analysis for prediction using the flexclus package
- Utilize some text mining using RTextTools and tm packages
If you have survived the last chapter, you will now be introduced to the world of market basket analysis (MBA). Market basket analysis (also sometimes called affinity analysis), is a predictive analytics technique that is used heavily in the retail industry in order to identify baskets of items that are purchased together. The typical use case for this is the supermarket shopping cart in which a shopper would typically purchase an assortment of items such as milk, bread, cheese, and so on, and the algorithm will predict how purchasing certain items together will affect the purchase of other items. It is one of those methods that retailers use to know to start sending you coupons and emails for things that you didn’t know you needed!
One often quoted example of MBA is the relationship between diapers and beer:
“One super market chain discovered in its analysis that customers that bought diapers often bought beer as well, have put the diapers close to beer coolers, and their sales increased dramatically”
- http://en.wikipedia.org/wiki/Market_basket
However, it is not only restricted to the retail industry. MBA can be used in the insurance industry to look at the various products that an insured person currently has, such as a car, home, and so on, and suggest other possible products such as life, disability, or investment products.
MBA is generally considered an unsupervised learning algorithm, in that target variables are usually not specified. However, as you will see later it is possible to refine the association rules, so that specific items can be specified as target variables.
MBA is also considered a type of recommender engine in which purchases of a set of items imply the purchase of others. Certainly, MBA and other recommender engines can share the same types of input data. However, MBA was developed before the advent of collaborative filtering techniques, as pioneered by Amazon, and is more suggestive of the integration of collected web data, while MBA is more associated with the RFID bar coding technologies found in scanners. However, in both cases suggestions of future purchases based on past purchases is the goal.
Critical to the understanding of MBA are the concepts of support, confidence, and lift. These are the measures that evaluated the goodness of fit for a set of association rules. You will also learn some specific definitions that are used in MBA, such as consequence, antecedent, and itemsets.
To introduce these concepts, we will first illustrate these terms through a very simplistic example. We will use only the first 10 transactions contained in the Groceries transaction file, which is contained in the arules package:
library(arules)After the arules library is loaded, you can see a short description of the Groceries dataset by entering ?Groceries at the command line. The following description appears in the help window:
“The Groceries data set contains 1 month (30 days) of real-world point-of-sale transaction data from a typical local grocery outlet. The data set contains 9835 transactions and the items are aggregated to 169 categories”.
For more information about how this dataset was collected, refer to the original publication (Michael Hahsler, 2006).
Once the arules package is loaded, load the Groceries dataset into memory:
data(Groceries)The Groceries is a transaction class object, not a dataframe. This R object represents transaction data used for mining item sets or rules. Logically, it is organized as a list of grocery receipts along with the items that were purchased together. Every individual line is referred to as a transaction and every column of the transaction represents a specific item purchased.
For example, here are three transactions consisting of a varying number of purchases:
| Transaction 1 | Milk | Cereal | |
|---|---|---|---|
| Transaction 2 | Beef | ||
| Transaction 3 | Butter | Sugar | Cream |
However, a transaction object is not physically organized in strict database table format. It is in a special R object format known as transactions.
You can also run a summary(Groceries), which will give you some high level information about the structure of the transactions file, as well as the most frequent individual items found in the market basket:

You will see later on how we can convert dataframes to transaction objects, and vice versa. For more information about this object, you can enter ?transactionInfo at the console line.
To see an example of a simple market basket for the Groceries file, run the following code:
Examine the output produced from the inspect() function, which prints transactions 10-19 from the market basket. Note that each transaction can consist of a single transaction ID, along with a list of purchased items. It can be a single purchased item (Transaction #4, Beef purchased by itself), or consist of multiple purchases (Transaction #1, Milk and Cereal):
inspect(Groceries[10:19])This is the following output:
items[1] {whole milk,cereals}[2] {tropical fruit,other vegetables,white bread,bottled water,chocolate}[3] {citrus fruit,tropical fruit,whole milk,butter,curd,yogurt,flour,bottled water,dishes}[4] {beef}[5] {frankfurter,rolls/buns,soda}[6] {chicken,tropical fruit}[7] {butter,sugar,fruit/vegetable juice,newspapers}[8] {fruit/vegetable juice}[9] {packaged fruit/vegetables}[10]{chocolate}Each transaction numbered 1-10 listed previously represents a basket of items purchased by a shopper. These are typically all items that are associated with a particular transaction or invoice. Each basket is enclosed within braces {}, and is referred to as an itemset. An itemset is a group of items that occur together.
Market basket algorithms construct rules in the form of:
Itemset{x1,x2,x3 ...} --> Itemset{y1,y2,y3...}.This notation states that buyers who have purchased items on the left-hand side of the formula (lhs) have a propensity to purchase items on the right-hand side (rhs). The association is stated using the à symbol, which can be interpreted as implies.
The lhs of the notation is also known as the antecedent, and the rhs is known as the consequence. If nothing appears on either the left-hand side or right-hand side there is no specific association rule for those items; however, it also means that those items have appeared in the basket.
Without an association rule algorithm, you are left with the computationally very expensive task of generating all possible pairs of itemsets, and then trying to mine the data in order to identify the best ones yourself. Associate rule algorithms help with filtering this.
The most popular algorithm for MBA is the apriori algorithm, which is contained within the arules package (the other popular algorithm is the eclat algorithm).
Running apriori is fairly simple. We will demonstrate this using our demo 10 transaction itemset that we just printed.
The apriori algorithm is based upon the principle that if a particular itemset is frequent, then all of its subsets must also be frequent. That principle itself is helpful for reducing the number of itemsets that need to be evaluated, since it only needs to look at the largest items sets first, and then be able to filter down:
- First, some housekeeping. Fix the number of printable digits to 2:
options(digits = 2)- Next, run the apriori algorithm. However, only run it on rows 10 through 19 of the Groceries dataset since we will want to keep the output small for now. We will also adjust the supp =, and conf = parameters to produce enough output so that we can illustrate some points in the next sections:
rules <- apriori(Groceries[10:19], parameter = list(supp = 0.1,conf = 0.6))- The output shows that 2351 rules were created. This is a large number of rules relative to the 10 transactions we are inspecting. The large number of rules is due to the low support level that we specified (0.10). That yields transactions that only contain a single combination. This is also indicated by the warning message produced by the log:
> Apriori>> Parameter specification:> confidence minval smax arem aval originalSupport support minlen maxlen> 0.6 0.1 1 none FALSE TRUE 0.1 1 10> target ext> rules FALSE>> Algorithmic control:> filter tree heap memopt load sort verbose> 0.1 TRUE TRUE FALSE TRUE 2 TRUE>> Absolute minimum support count: 1> Warning in apriori(Groceries[10:19], parameter = list(supp = 0.1, conf =0.6)): You chose a very low absolute support count of 1. You might run outof memory! Increase minimum support.> set item appearances ...[0 item(s)] done [0.00s].> set transactions ...[22 item(s), 10 transaction(s)] done [0.00s].> sorting and recoding items ... [22 item(s)] done [0.00s].> creating transaction tree ... done [0.00s].> checking subsets of size 1 2 3 4 5 6 7 8 9 done [0.00s].> writing ... [2351 rule(s)] done [0.00s].> creating S4 object ... done [0.00s].- Sort the rules by support, which is one of the important evaluation metrics that measures how frequently different items occur together. This will be discussed further in the next section:
rules <- sort(rules, by = "support", decreasing = TRUE) # 'high-confidence' rules.- Look at the first five rules using the arules inspect function. The output columns are designated: index number (blank header), lhs (left-hand side), rhs (right-hand side), support, confidence, and lift. Note that each support and confidence measure is at least equal to the parameter specified in the call to the apriori function specified previously:
inspect(head(rules, 5))This is the following output:

The rules shown previously are expressed as an implication between the antecedent (left-hand side) and the consequence (right-hand side).
The first rule, describes customers who buy a bottle of water also buying tropical fruit. The third rule states that customers who buy cereals have a tendency to buy whole milk.
Three main metrics have been developed that measure the importance, or accuracy of an association rule: support, confidence, and lift.
Support measures how frequently the items occur together. Imagine having a shopping cart in which there can be a very large number of combinations of items. Some items that occur rarely could be excluded from the analysis. When an item occurs frequently you will have more confidence in the association among the items, since it will be a more popular item. Often your analysis will be centered around items with high support.
Calculating support is simple. You first calculate a proportion by counting the number of times that the items in the rule appear in the basket divided by the total number of occurences in the itemsets:
- We can see that for the first rule (index #63), {bottled water} and {tropical fruit} appear together in the same transaction in two different transactions (2 and 3), therefore the support for that rule is 2/10 or 20%
- The last rule shown, rule (index #3) has a support of 0.10, since soda with rolls/buns only appears once in the 10 transactions (1/10)
Confidence is the conditional probability that the event on the right-hand side (rhs or consequence) will occur, given the items on the left-hand has occurred (lhs or antecedent). This can be computed manually by counting the number of occurrences in the transactions.
For example, let’s take a closer look at the following rule:
- {tropical fruit} => {bottled water}
- We can see that tropical fruit occurs in three separate transaction itemsets, itemset 2, 3, and 6 and therefore the denominator of the formula is 3
- Of those three itemsets, bottled water occurs in two of them (itemsets 2 and 3 only, but not 6), so the confidence is 2/3 or 67% and also note that the confidence for the reverse itemset {bottled water} => {tropical fruit} is higher, since every time bottled water is purchased, tropical fruit is purchased as well and you can easily verify that by inspecting and counting the elements by hand
Lift is determined by dividing the confidence just calculated by the independent probability of the consequent. Lift can be a better measure than either support or confidence, since it incorporates features of both.
To calculate the lift for the second rule (index #64), we only need to determine the unconditional probability of the consequence we just calculated. Since {bottled water} appears two out of 10 times (20%) as the consequence, we divide.67 by.20 to yield 3.4, which is the lift for rule 64.
The factors when dealing with the evaluation are as follows:
- When evaluating the lift metric, use 1 as a baseline lift measure, since a lift of 1 implies no relationship between the antecedent and the consequence.
- As the value of lift increases, it will help you target more interesting rules. If you are able to identify rules with a high confidence level that occur more than you expect, that could lead to enhanced revenue from developing customer incentives to purchase the same or similar products again.
Now that we have had a short introduction to the association rules algorithm, we will illustrate applying association rules to a more meaningful example.
We will be using the online retail dataset, which can be obtained from the UCI machine learning repository at:
https://archive.ics.uci.edu/ml/datasets/Online+Retail.
As described by the source, the data is:
“A transnational data set which contains all the transactions occurring between 01/12/2010 and 09/12/2011 for a UK-based and registered non-store online retail. The company mainly sells unique all-occasion gifts. Many customers of the company are wholesalers”.
For more information about how the dataset was created, please refer to the original journal article (Daqing Chen, 2012).
We will input the Groceries data using the read.csv() function.
We can use the file.show() function to directly examine the input file if needed. This is sometimes needed if you find that there are errors in the input. It has been commented out in the code, but you are encouraged to try it out yourself.
The knitr library will be used mostly for the purposes of display formatted text output via the kable function. If you wish, you can replace kable function calls with head or print functions:
rm(list = ls())library(sqldf)library(knitr)
setwd("C:/PracticalPredictiveAnalytics/Data")Set stringsAsFactors to FALSE since we will be manipulating the variables as character strings later:
options(stringsAsFactors = F)OnlineRetail <- read.csv("Online Retail.csv", strip.white = TRUE) #we are done reading, so set the output directorysetwd("C:/PracticalPredictiveAnalytics/Outputs")This code also illustrates the capture.output() function. The capture.output function will save the metadata for the raw input file. This is done because we want to track changes done to the input, and we want to capture the contents of the same dataframe at different points in time. That will enable us to save the values of metadata and compare them at different points:
## Save it in case we need to look at the metadata later on.
OnlineRetail.Metadata <- capture.output(str(OnlineRetail))
# print it now. We can see that the capture.output contains the output of the original str function, and that there are 541,909 observations
Set stringsAsFactors back to TRUE:
options(stringsAsFactors = T)After reading in the file, the nrow() function shows that the transaction file contains 541909 rows:
nrow(OnlineRetail)This is the following output:
> [1] 541909We can use our handy View() function to peruse the contents. Alternatively, you can use the kable() function from the knitr library to display a simple tabular display of the dataframe in the console, as indicated later.
Look at the first few records. The kable() function will attempt to fit a simple table in the space providing, and will also truncate any long strings:
kable(head(OnlineRetail))We can still see the last column is truncated (United Kingdom), but all of the columns fit without wrapping to the next line:

Using an R Notebook with the kable() function.
Note that when using the Rmarkdown package, or an R Notebook in RStudio, the output from the kable() function can be formatted to appear as an HTML table in the markdown file. Otherwise, it will appear as plain ASCII text.
For example, you may choose to run your code in RStudio using an R Notebook. IF you choose to run code in this manner follow these steps to try out the kable() command and see how the results are formatted as HTML.
From the RStudio menu open up a new R notebook, using the menu sequence File/New File/R Notebook.
A skeleton R notebook will appear, instead of an R script:
Code is organized in chunks. That means that all R code must appear between…{r} and… shaded lines. Inserting the following kable() function within this code chunk will output the identical data as before, however, it will be embedded in the R Notebook following the code that you have just written, and formatted as HTML.
Insert the following line into the first code chunk:
kable(head(OnlineRetail),format='html')The kable() function output appears embedded in the R notebook code, just following the code chunk:

We can also look at the distribution plots of InvoiceDate. But first, the date will need to be transformed to date format (Capture.output shows it as a string) and sorted first:
InvoiceDate <- gsub(" .*$", "", OnlineRetail$InvoiceDate)InvoiceDate <- (as.Date(InvoiceDate, format = "%m/%d/%Y"))InvoiceDate <- sort(InvoiceDate, decreasing = FALSE)First, observe from the output of the str() function that InvoiceDate is now truly in date format:
> str(InvoiceDate)This is the following output:
Date[1:541909], format: "2010-12-01" "2010-12-01" "2010-12-01" "2010-12-01" "2010-12-01" "2010-12-01" "2010-12-01" "201... <truncated>We can see from the head and tail commands, as well as the plots, that the invoices encompass the period from 12/1/2011 through 12/9/2011. We can also see several spikes in orders around the December holiday season:
> head(as.data.frame(InvoiceDate))This is the following output:
InvoiceDate1 2010-12-012 2010-12-013 2010-12-014 2010-12-015 2010-12-016 2010-12-01Similarly, these are the last six records in the file. These records contain the most recent dates:
> tail(as.data.frame(InvoiceDate))This is the resulting output:
InvoiceDate541904 2011-12-09541905 2011-12-09541906 2011-12-09541907 2011-12-09541908 2011-12-09541909 2011-12-09The individual dates and labels will be too dense to plot, so we will reduce the dates to Month/Year:
First, extract the year and month from the invoice date ( first seven characters of YYYY-MM-DD), and then obtain the counts using the table() function. Then plot the results. You can see that the last month’s transactions are lower than the previous month’s since the data only goes through December 9th:
par(las = 2)barplot(table(substr(InvoiceDate,1,7)), cex.lab = 1, cex.main = 1.5, cex.names = 1,col = c("blue"),main="Invoices by Year/Month")
Here comes the cleaning part!
Print some of the groceries contained within the description field of OnlineRetail:
kable(OnlineRetail$Description[1:5],col.names=c("Grocery Item Descriptions"))|Grocery Item Descriptions ||:-----------------------------------------||WHITE HANGING HEART T-LIGHT HOLDER ||METAL METAL LANTERN ||CREAM CUPID HEARTS COAT HANGER ||KNITTED UNION FLAG HOT WATER BOTTLE ||RED WOOLLY HOTTIE WHITE HEART. |Although each line contains a separate grocery item, the items are in a uniform format, that is, the number of words describing each item can vary, and some words are adjectives and some are nouns. Additionally, the retailer may deem certain words to be irrelevant to a particular marketing campaign (such as colors, or sizes, which may be standard across all products). This type of data can be referred to as semi-structured data, since it incorporates certain definitions of structured data (the topic is only groceries, all capitalized, products that are pre-known, and so on), as well as some elements of unstructured data (varying lengths of products, variations in text strings). For optimal results, unstructured data such as text usually needs to be scrubbed and cleaned before it is in a form that is suitable for analysis. Fortunately, there are several R functions available that help you do that.
We can start by removing leading and trailing blanks from each of the product descriptions, since they add no value to the analysis and just take up extra space. trimws is a handy function to accomplish this, since it removes both leading and trailing spaces. The nchar() function will count the number of bytes in a character string, so we can run this function on OnlineRetail$Description before and after performing the string trim to see how much space we will actually be saving:
sum(nchar(OnlineRetail$Description))This is the resulting output:
> [1] 14284888Continue the following code:
OnlineRetail$Description <- trimws(OnlineRetail$Description)sum(nchar(OnlineRetail$Description))This is the resulting output:
> [1] 14283213We see that trimws reduced the size of the Description. Had the number increased after using the function, that would be a clue to check your code!
Market basket analysis using color choice analysis is an interesting topic in itself. However, for this analysis, we will remove some of the colors in order to simplify some of the descriptions. We will use the gsub() function to remove some of the specific colors that appear as part of the product description. Since the gsub() function only works on a single string or pattern at one time, set up a function to pass multiple strings to the gsub() function.
This gsub_multiple function will take a character vector (x), and change all strings supplied in from to their corresponding values in to. In our case, we want to eliminate them from the text, so our to strings will always be "":
gsub_multiple <- function(from, to, x) { updated <- x for (i in 1:length(from)) { updated <- gsub(from[i], to[i], updated) } return(updated)}
OnlineRetail$Description <- gsub_multiple(c("RED","PINK","GREEN","SMALL","MEDIUM","LARGE","JUMBO","STRAWBERRY"), rep("",8), OnlineRetail$Description)If you did not want to bother specifying colors, and you wanted to remove colors automatically, you could accomplish that as well.
The colors() function returns a list of colors that are used in the current palette. We can then perform a little code manipulation in conjunction with the gsub() function that we just used to replace all of the specified colors from OnlineRetail$Description with blanks.
We will also use the kable() function, which is contained within the knitr package, in order to produce simple HTML tables of the results:
# compute the length of the field before changes before <- sum(nchar(OnlineRetail$Description))
# get the unique colors returned from the colors function, and remove any digits found at the end of the string
# get the unique colors col2 <- unique(gsub("[0-9]+", "", colors(TRUE)))
#Now we will filter out any colors with a length > 7. This number is somewhat arbitrary but it is just done for illustration, and to reduce the number of colors to some of the more 'popular' colors (RED, WHITE, GREEN, BLACK etc.) We may miss a couple ("STRAWBERRY"), but we can put them back later.
for (i in 1:length(col2)) { col2[i] <- ifelse(nchar(col2[i]) > 7, "", col2[i]) }
col2 <- unique(col2)After running the code, double-check the output to ensure that the colors are the ones that you want:
cat("Unique Colors\n") > Unique Colors kable(head(data.frame(col2), 10))This is the resulting output:

Clean up the colors a little more by capitalizing all of the colors and inserting a delimiter:
col <- topper(paste0(col2, collapse = "|"))Now pass the results to gsub():
cat("Pass to gsub\n", head(col, 9))> Pass to gsub > WHITE||AZURE|BEIGE|BISQUE|BLACK|BLUE|BROWN|CORAL|CYAN|DARKRED|DIMGRAY|GOLD|GRAY|GREEN|HOTPINK|IVORY|KHAKI|LINEN|MAGENTA|MAROON|NAVY|OLDLACE|ORANGE|ORCHID|PERU|PINK|PLUM|PURPLE|RED|SALMON|SIENNA|SKYBLUE|SNOW|TAN|THISTLE|TOMATO|VIOLET|WHEAT|YELLOWFor example, to replace the colors in the dataframe with blanks:
OnlineRetail$Description <- gsub(col, "", OnlineRetail$Description)Check the length to see how much was removed. As before, print the character count before and after, to ensure that the Description is reduced in size:
after <- sum(nchar(OnlineRetail$Description)) print(before)> [1] 13682222print(after)> [1] 13341097Verify that there are no more colors by inspection. We will look at the first five, although you will probably want to look at more, all from different parts of the dataset. We will leave “Cream” in for now, but we would remove that as well if we felt it would not help in the analysis:
kable(OnlineRetail$Description[1:5],col.names=c("Grocery Item Description"))The first five rows are shown here:
|Grocery Item Description ||:-------------------------------------------------||WHITE HANGING HANGING HEART T-LIGHT HOLDER ||WHITE METAL LANTERN ||CREAM CUPID HEARTS COAT HANGER ||KNITTED UNION FLAG HOT WATER BOTTLE ||WOOLLY HOTTIE WHITE HEART. |Since we will want to have a basket of items to perform some association rules on, we will want to filter out the transactions that only have one item per invoice. That might be useful for a separate analysis of customers who only purchased one item, but it does not help with finding associations between multiple items, which is the goal of this exercise.
- Let’s use sqldf to find all of the single item transactions, and then we will create a separate dataframe consisting of the number of items per customer invoice:
library(sqldf)- First construct a query: How many distinct invoices were there? We see that there were 25900 separate invoices:
sqldf("select count(distinct InvoiceNo) fromOnlineRetail")> Loading required package: tcltk> count(distinct InvoiceNo)> 1 25900- How many invoices contain only single transactions? First, extract the single item invoices:
single.trans <- sqldf("select InvoiceNo, count(*) as itemcount fromOnlineRetail group by InvoiceNo having count(*)==1")- Next, add them up. This shows us that there are not a lot of single transaction items (5841):
sum(single.trans$itemcount) > [1] 5841- SQL query: How many have multiple transactions? Instead of using count(*) == 1, use count(*) > 1 to obtain the multiple transactions. There are 536068 of them:
x2 <- sqldf("select InvoiceNo, count(*) as itemcount fromOnlineRetail group by InvoiceNo having count(*) > 1")
sum(x2$itemcount)> [1] 536068- Show a tabulation of the number of items per invoice, in x2, to verify that they all have at least two items:
kable(head(x2))
|InvoiceNo | itemcount||:---------|---------:||536365 | 7||536366 | 2||536367 | 12||536368 | 4||536370 | 20||536372 | 2|Now we can take a look at the distribution of the number of items. We can see by using the mean() function that there is an average of ~27 items. This will be a large enough assortment of items to do a meaningful analysis:
mean(x2$itemcount)This is the following output:
> [1] 27We can also plot a histogram:
hist(x2$itemcount, breaks = 500, xlim = c(0, 50))The histogram shown next shows a definite spike at the low end. We know that the data cannon contains single invoices (count=1), since we have already filtered them out:

To verify this, we can inspect the itemcount frequencies via a table. Run the following table() command at the console:
head(table(x2$itemcount)add,20)We can verify that single item counts no longer appear:

We will want to retain the number of total items for each invoice on the original data frame. That will involve joining the number of items contained in each invoice back to the original transactions, using the merge() function, and specifying Invoicenum as the key.
If you count the number of distinct invoices before and after the merge, you can see that the invoice count is lower than prior to the merge:
#first take a 'before' snapshot
nrow(OnlineRetail)> [1] 541909
#count the number of distinct invoices
sqldf("select count(distinct InvoiceNo) from OnlineRetail")The output shows a total of 25900 distinct invoices:
> count(distinct InvoiceNo)> 1 25900Now merge the counts back with the original data:
OnlineRetail <- merge(OnlineRetail, x2, by = "InvoiceNo")Check the new number of rows, and the new count of distinct invoices (20059 versus 25900). Note these counts compared to the original. The reduction is due to eliminating some of the members who only have one invoiced item:
> nrow(OnlineRetail)[1] 536068> sqldf("select count(distinct InvoiceNo) from OnlineRetail") count(distinct InvoiceNo)1 20059Print the OnlineRetail table along with the merged itemcount:
kable(OnlineRetail[1:5, 1:9],col.names=c("Inv#","StockCode","Desc","Quant","InvDate","UnitPrice","CustID","Country","ItemCount"), padding = 0)Itemcount appears as the last column in this table:
|Inv# |StockCode|Desc |Quant|InvDate |UnitPrice|CustID|Country ||:-----|:--------|:-----------------------------|----:|:-------------|--------:|-----:|:-------------||536365|84406B |CREAM CUPID HEARTS COAT HANGER| 8|12/1/2010 8:26| 2.8| 17850|United Kingdom| 7||536365|22752 |SET 7 BABUSHKA NESTING BOXES | 2|12/1/2010 8:26| 7.6| 17850|United Kingdom| 7||536365|85123A |HANGING HEART T-LIGHT HOLDER | 6|12/1/2010 8:26| 2.5| 17850|United Kingdom| 7||536365|84029E |WOOLLY HOTTIE HEART. | 6|12/1/2010 8:26| 3.4| 17850|United Kingdom| 7||536365|71053 |METAL LANTERN | 6|12/1/2010 8:26| 3.4| 17850|United Kingdom| 7|For long descriptions, sometimes it is beneficial to compress them into camelcase to improve readability. This is especially valuable when viewing descriptions that are labels on x or y axes.
Camelcase is a method that some programmers use for writing compound words, where spaces are first removed, and then each word begins with a capital letter. It is also a way of conserving space.
To accomplish this, we can write a function called .simpleCap, which performs this function. To illustrate how it works, we will pass it a two element character vector c(“A certain good book”,“A very easy book”), and observe the results.
This is a simple example use of this function that maps the two character vector c(“A certain good book”, “A very easy book”) to camelcase. This vector is mapped to two new elements:
[1] "ACertainGoodBook", and [2] "AVeryEasyBook"
# change descriptions to camelcase maybe append to itemnumber for uniqueness.simpleCap <- function(x) { # s <- strsplit(x, ' ')[[1]] s <- strsplit(tolower(x), " ")[[1]]
aa <- paste(toupper(substring(s, 1, 1)), substring(s, 2), sep = "", collapse = " ") gsub(" ", "", aa, fixed = TRUE)
}
a <- c("A certain good book", "A very easy book")a4 <- gsub(" ", "", .simpleCap(a), fixed = TRUE)a4> [1] "ACertainGoodBook"lapply(a, .simpleCap)> [[1]]> [1] "ACertainGoodBook">> [[2]]> [1] "AVeryEasyBook"Let’s use the .simpleCap function to create a new version of description from our OnlineRetail dataset, and call it Desc2, which removes the blanks and capitalizes the first letter of each word:
OnlineRetail$Desc2 <- lapply(as.character(OnlineRetail$Description), .simpleCap)
kable(OnlineRetail[1:5, c(3, 10)], padding = 0)
Often the first and last word of product descriptions contain useful information, and sometimes you can use a single word or phrase in place of the original longer description. This may not always be the case, but it is worth trying. In order to extract the last word from the descriptions, we can use the word function from the stringr package:
library(stringr)OnlineRetail$lastword <- word(OnlineRetail$Description, -1) #supply -1 to extract the last wordOnlineRetail$Description <- trimws(OnlineRetail$Description, "l")OnlineRetail$firstword <- word(OnlineRetail$Description, 1)# use head(OnlineRetail) if you are no using Rmarkdown
kable(OnlineRetail[1:5, c(3, 10:12)], padding = 0)Firstword appears as the last column in the following output:

In order to see if this lastword mapping makes any sense, we will sort the results so that we can see the most frequently occurring ending words. We could ultimately use this information to create category subproducts, such as cases, bags, signs, and so on:
kable(head(as.data.frame(sort(table(OnlineRetail$lastword[]), decreasing = TRUE)), 10))The sorted results appear. Note that this is formatted as a table, although the default output from the kable() function will appear as plain text in the console.
The lastword description looks useable (other than the large count of NAs), but we will still retain the columns as part of the analytics dataset:

Now that we are finished with our transformations, we will create the training and test data frames. We will perform a 50/50 split between training and test:
# Take a sample of full vectornrow(OnlineRetail)> [1] 536068pctx <- round(0.5 * nrow(OnlineRetail))set.seed(1)
# randomize rows
df <- OnlineRetail[sample(nrow(OnlineRetail)), ]rows <- nrow(df)OnlineRetail <- df[1:pctx, ] #training setOnlineRetail.test <- df[(pctx + 1):rows, ] #test setrm(df)
# Display the number of rows in the training and test datasets.
nrow(OnlineRetail)> [1] 268034nrow(OnlineRetail.test)> [1] 268034It is a good idea to periodically save your data frames, so that you can pick up your analysis from various checkpoints.
In this example, I will first sort them both by InvoiceNo, and then save the test and train data sets to disk, where I can always load them back into memory as needed:
setwd("C:/PracticalPredictiveAnalytics/Data") OnlineRetail <- OnlineRetail[order(OnlineRetail$InvoiceNo), ] OnlineRetail.test <- OnlineRetail.test[order(OnlineRetail.test$InvoiceNo), ]
save(OnlineRetail,file='OnlineRetail.full.Rda')save(OnlineRetail.test,file='OnlineRetail.test.Rda')
load('OnlineRetail.full.Rda') load('OnlineRetail.test.Rda')
nrow(OnlineRetail)> [1] 268034nrow(OnlineRetail.test)> [1] 268034nrow(OnlineRetail)> [1] 268034At this point, we have prepared our analytics data sets and are ready to move on to the actual analysis.
If you wish, you can save the entire workspace to disk as follows:
save.image(file = "ch6 part 1.Rdata")If you are still in a session in which OnlineRetail is still in memory, you are OK! However, if you are picking up where we left off, you will need to load the data that we saved in the last session. Start by setting the working directory and then loading the OnlineRetail training dataset:
rm(list = ls())setwd("C:/PracticalPredictiveAnalytics/Data")load("OnlineRetail.full.Rda")# works for small dataOnlineRetail <- OnlineRetail[1:10000,]cat(nrow(OnlineRetail), "rows loaded\n")> 10000 rows loadedThe cat function in the previous step should reflect the number of rows in the training data set, which is 268034.
We have seen in the data prep stage that there are a large number of itemsets generated for each invoice. To begin to demonstrate the algorithm, we will extract one representative word from each product description, and use that word as the consequent (or rhs) to build some association rules. We have already saved the first and last words from each product description. We would examine those words more closely and see if we can filter them to result in a manageable set of transactions.
Let’s first preview the frequency of the first and last word of the descriptions in descending order. That should give us a clue as to what the popular products are:
library(arules)> Loading required package: Matrix>> Attaching package: 'arules'> The following objects are masked from 'package:base':>> abbreviate, write library(arulesViz)> Loading required package: gridPrint the popular first words of the description. We will do that by sorting the frequency of first words in descending order:
kable(head(as.data.frame(sort(table(OnlineRetail$firstword[]), decreasing = TRUE)), 10))| sort(table(OnlineRetail$firstword[]), decreasing = TRUE) | |
|---|---|
| SET | 17381 |
| BAG | 8720 |
| LUNCH | 7692 |
| RETROSPOT | 7155 |
| PACK | 6861 |
| VINTAGE | 6204 |
| HEART | 4799 |
| HANGING | 4457 |
| DOORMAT | 4175 |
| REGENCY | 3452 |
Similarly, print the popular last words of the description:
kable(head(as.data.frame(sort(table(OnlineRetail$lastword[]), decreasing = TRUE)), 10))| sort(table(OnlineRetail$lastword[]), decreasing = TRUE) | |
|---|---|
| 18376 | |
| DESIGN | 12713 |
| HOLDER | 6792 |
| BOX | 6528 |
| RETROSPOT | 6517 |
| SIGN | 6184 |
| CASES | 5465 |
| BAG | 4826 |
| SET | 4418 |
| CHRISTMAS | 3963 |
Looking at the popular terms shows that many transactions concern the purchases of boxes, cases, signs, bags, and so on.
We see from the frequencies of lastword that there are some blank values. The lastword seems to contain mostly nouns, and firstword seems to be a mix of adjectives and nouns as well (bag, heart). If we treat the text strings as a Bag of Words, we can rationalize combining the two into one token. However, we will give lastword priority, and populate it with the value of firstword only if it is missing:
# replace blank values in lastword, with first word.
OnlineRetail$lastword <- ifelse(OnlineRetail$lastword == "", OnlineRetail$firstword,OnlineRetail$lastword)After we are done with this, we will take another look at the frequencies and observe that the blank values have disappeared:
head(as.data.frame(sort(table(OnlineRetail$lastword[]), decreasing = TRUE)), 10)> sort(table(OnlineRetail$lastword[]), decreasing = TRUE) > DESIGN 12713 > HOLDER 6792 > RETROSPOT 6574 > BOX 6528 > SIGN 6184 > BAG 5761 > CASES 5465 > SET 4418 > HEART 4027 > CHRISTMAS 4005Based upon these frequencies, we will filter the data to only include a subset of the top categories. We will exclude some of the terms that do not apply to the physical product, such as design, set, and any associated colors:
# Testing OnlineRetail2 <- OnlineRetail OnlineRetail2 <- subset(OnlineRetail, lastword %in% c("BAG", "CASES", "HOLDER", "BOX", "SIGN", "CHRISTMAS", "BOTTLE", "BUNTING", "MUG", "BOWL", "CANDLES", "COVER", "HEART", "MUG", "BOWL"))Run the table() function again on the results to see the new frequencies:
head(as.data.frame(sort(table(OnlineRetail2$lastword[]), decreasing = TRUE)), 10) > sort(table(OnlineRetail2$lastword[]), decreasing = TRUE) > HOLDER 6792 > BOX 6528 > SIGN 6184 > BAG 5761 > CASES 5465 > HEART 4027 > CHRISTMAS 4005 > BOTTLE 3795 > BUNTING 3066 > MUG 2900Use the nrow() function to see how much of the data was filtered from the original:
cat(nrow(OnlineRetail), "Original before subsetting\n")> 268034 Original before subsettingcat(nrow(OnlineRetail2), "After Subsetting\n")> 55609 After SubsettingWe are almost there! There is an extra step that we need to do in order to prepare our data for market basket analysis.
The association rules package requires that the data be in transaction format. Transactions can either be specified in two different formats:
- One transaction per itemset with an identifier and this shows the entire basket in one line, just as we saw with the Groceries data.
- One single item per line with an identifier.
Additionally, you can create the actual transaction file in two different ways, by either:
- Physically writing a transactions file.
- Coercing a dataframe to transaction format.
For smaller amounts of data, coercing the dataframe to a transaction file is simpler, but for large transaction files, writing the transaction file first is preferable, since append files can be fed from large operational transaction systems. We will illustrate both ways.
Now we are ready to coerce the dataframe. We will create a temporary data frame containing just the transaction ID (InvoiceNo), and the descriptor (lastword).
First, we will verify the column names and numbers for these two variables. We can see that they correspond to columns 1 and 12 of the dataframe by first running a colnames function on OnlineRetail2:
colnames(OnlineRetail2)> [1] "InvoiceNo" "StockCode" "Description" "Quantity" "InvoiceDate"> [6] "UnitPrice" "CustomerID" "Country" "itemcount" "Desc2"> [11] "lastword" "firstword"As a double-check, display the first 25 rows, specifying the indices found previously:
kable(head(OnlineRetail2[, c(1, 11)], 5))
First, create the dataframe with only two columns, named TransactionID and Items:
tmp <- data.frame(OnlineRetail2[, 1], OnlineRetail2[, 11])names(tmp)[1] <- "TransactionID"names(tmp)[2] <- "Items"
tmp <- unique(tmp)nrow(tmp)> [1] 33182Verify the results:
kable(head(tmp))This is the following output:

We will now use the split() function to group the descriptions (lastword, which is column 2), by InvoiceID (column 1). The as() function is the critical keyword, as it converts the results of the split to transaction form:
trans4 <- as(split(tmp[, 2], tmp[, 1]), "transactions")Once the data has been coerced to transaction form, we can use the inspect function to examine the data.
When inspecting transaction files, you do not use normal print, or head functions directly, since the objects are in sparse format. You use an inspect() function instead.
If you happen to have the tm package loaded (which we will use later), you must preface inspect with arules::inspect, since there is also an inspect function in the tm package that serves a different purpose.
If you run an inspect command on the first five records, you can see that the data is in basket format, that is, each invoice shows the itemsets, delimited by {}, that are associated with each invoice:
arules::inspect(trans4[1:5]) > items transactionID > 1 {HOLDER} 536365 > 2 {BOX} 536370 > 3 {BOTTLE,HOLDER,MUG} 536373 > 4 {BOTTLE,HOLDER} 536375 > 5 {HOLDER} 536376Another way of displaying transactions, other than using inspect, is to first coerce it to a matrix and then display the items as Boolean values. If you have many items, you will need to subset the column vectors so that they fit on the screen:
as(trans4, "matrix")[1:5, 1:5] > BAG BOTTLE BOWL BOX BUNTING > 536365 FALSE FALSE FALSE FALSE FALSE > 536370 FALSE FALSE FALSE TRUE FALSE > 536373 FALSE TRUE FALSE FALSE FALSE > 536375 FALSE TRUE FALSE FALSE FALSE > 536376 FALSE FALSE FALSE FALSE FALSEas(trans4, "matrix")[1:5, 6:ncol(trans4)] > CANDLES CASES CHRISTMAS COVER HEART HOLDER MUG SIGN > 536365 FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE > 536370 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > 536373 FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE > 536375 FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE > 536376 FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSEEven before we run any association rules we can obtain counts of the topN items, as shown in the first plot.
If we are looking for items with a specified support level, we can include that as a parameter to the function as well.
The second plot is another way of plotting the items, which specifies support as a filter, and it indicates higher support for bags, boxes, cases, and holders:
par(mfrow = c(1, 2), bg = "white", col = c("blue"))
itemFrequencyPlot(trans4, topN = 10, type = "absolute", cex.names = 0.7) itemFrequencyPlot(trans4, support = 0.2, cex.names = 0.75)
Reset the graphics parameters. The output will show null device, indicating that it has been reset:
dev.off()> null device> 1As shown earlier, the association rules are run using the apriori() function. The apriori() function has several filtering parameters that are used to control the number of rules that are produced. In our example, we will specify the minimum support and confidence threshold that a rule needs to pass in order to be considered.
The number that you pass to apriori depends upon how you want to look at the rules. It can be an initial screening, or it can be a deeper dive, after you have performed several passes. But generally, if we want many rules we can decrease the support and confidence parameters. If we want to focus on items that appear frequently, we raise the support threshold. If we want to concentrate on higher quality and more accurate rules, we would raise the confidence level.
These numbers are not absolutes. They are numbers that you can tweak in order to limit the number of rules that you will look at, versus their quality.
The minlen = 2 parameter is often specified in order to guarantee that there are itemsets included in the left-hand side of the rule:
rulesx <- apriori(trans4, parameter = list(minlen = 2, support = 0.02, confidence = 0.01)) > Apriori > > Parameter specification: > confidence minval smax arem aval originalSupport support minlen maxlen > 0.01 0.1 1 none FALSE TRUE 0.02 2 10 > target ext > rules FALSE > > Algorithmic control: > filter tree heap memopt load sort verbose > 0.1 TRUE TRUE FALSE TRUE 2 TRUE > > Absolute minimum support count: 272 > > set item appearances ...[0 item(s)] done [0.00s]. > set transactions ...[13 item(s), 13617 transaction(s)] done [0.00s]. > sorting and recoding items ... [13 item(s)] done [0.00s]. > creating transaction tree ... done [0.00s]. > checking subsets of size 1 2 3 4 5 done [0.00s]. > writing ... [829 rule(s)] done [0.00s]. > creating S4 object ... done [0.00s].The output from the apriori algorithm will tell us how many rules were generated. If you also perform a str() function on rulesx, that will show you all of the sublists that are contained within the rulesx object, which can get complex. Ordinarily the output is sufficient for analyzing the rules, but these sublists can be used programmatically to print the number of rules generated if you vary the paramaters in a loop. For our example, printing the second column of one of the sublists (rulesx@lhs@data@Dim [2]) shows that here 829 rules have been generated. This is a manageable number of rules to look at:
str(rulesx)> Formal class 'rules' [package "arules"] with 4 slots > ..@ lhs :Formal class 'itemMatrix' [package "arules"] with 3 slots > .. .. ..@ data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots > .. .. .. .. ..@ i : int [1:1752] 8 5 8 7 8 11 8 2 8 4 ... > .. .. .. .. ..@ p : int [1:830] 0 1 2 3 4 5 6 7 8 9 ... > .. .. .. .. ..@ Dim : int [1:2] 13 829 > .. .. .. .. ..@ Dimnames:List of 2 > .. .. .. .. .. ..$ : NULL > .. .. .. .. .. ..$ : NULL > .. .. .. .. ..@ factors : list() > .. .. ..@ itemInfo :'data.frame': 13 obs. of 1 variable: > .. .. .. ..$ labels: chr [1:13] "BAG" "BOTTLE" "BOWL" "BOX" ... > .. .. ..@ itemsetInfo:'data.frame': 0 obs. of 0 variables > ..@ rhs :Formal class 'itemMatrix' [package "arules"] with 3 slots > .. .. ..@ data :Formal class 'ngCMatrix' [package "Matrix"] with 5 slots > .. .. .. .. ..@ i : int [1:829] 5 8 7 8 11 8 2 8 4 8 ... > .. .. .. .. ..@ p : int [1:830] 0 1 2 3 4 5 6 7 8 9 ... > .. .. .. .. ..@ Dim : int [1:2] 13 829 > .. .. .. .. ..@ Dimnames:List of 2 > .. .. .. .. .. ..$ : NULL > .. .. .. .. .. ..$ : NULL > .. .. .. .. ..@ factors : list() > .. .. ..@ itemInfo :'data.frame': 13 obs. of 1 variable: > .. .. .. ..$ labels: chr [1:13] "BAG" "BOTTLE" "BOWL" "BOX" ... > .. .. ..@ itemsetInfo:'data.frame': 0 obs. of 0 variables > ..@ quality:'data.frame': 829 obs. of 3 variables: > .. ..$ support : num [1:829] 0.0239 0.0239 0.0206 0.0206 0.0245 ... > .. ..$ confidence: num [1:829] 0.278 0.21 0.24 0.129 0.285 ... > .. ..$ lift : num [1:829] 2.43 2.43 1.5 1.5 2.03 ... > ..@ info :List of 4 > .. ..$ data : symbol trans4 > .. ..$ ntransactions: int 13617 > .. ..$ support : num 0.02 > .. ..$ confidence : num 0.01rulesx@lhs@data@Dim[2]> [1] 829The rule length distribution indicates the number of itemsets that appear in both the left and right side of the association. The most frequent number of item is 3, which either means that the purchase of two items together, implies purchasing a single item, or conversely, purchasing a single item implies purchasing two additional items.
The quality measures tell you something about the distribution of support, confidence, and lift. The quality function will give the support, lift, and confidence for each of the rules. You can also sort the rules by each of these important measures and observe which rules have the highest measures.
We can see from the low support distribution that there are no particular itemsets that occur more than others. If you inspect the rules sorted by the support level, you will see the highest support level at 0.046. Notice that this agrees with the Max. support level given in the summary. The top three support levels are for those customers who purchase holders, boxes, or signs:
summary(rulesx) > set of 829 rules > > rule length distribution (lhs + rhs):sizes > 2 3 4 5 > 156 438 220 15 > > Min. 1st Qu. Median Mean 3rd Qu. Max. > 2.000 3.000 3.000 3.113 4.000 5.000 > > summary of quality measures: > support confidence lift > Min. :0.02005 Min. :0.1293 Min. :1.000 > 1st Qu.:0.02210 1st Qu.:0.3672 1st Qu.:1.948 > Median :0.02512 Median :0.5110 Median :2.393 > Mean :0.02987 Mean :0.5068 Mean :2.444 > 3rd Qu.:0.03202 3rd Qu.:0.6533 3rd Qu.:2.899 > Max. :0.09885 Max. :0.8571 Max. :5.419 > > mining info: > data ntransactions support confidence > trans4 13617 0.02 0.01head(quality(rulesx)) #also look at the quality measures for each of the rules > support confidence lift > 1 0.02394066 0.2779199 2.432156 > 2 0.02394066 0.2095116 2.432156 > 3 0.02063597 0.2395567 1.500480 > 4 0.02063597 0.1292548 1.500480 > 5 0.02452816 0.2847400 2.026819 > 6 0.02452816 0.1745949 2.026819
tmp <- as.data.frame(inspect(head(sort(rulesx, by = "support"), 10))) > lhs rhs support confidence lift > 155 {BOX} => {HOLDER} 0.09884703 0.3417111 1.063075 > 156 {HOLDER} => {BOX} 0.09884703 0.3075166 1.063075 > 135 {HEART} => {HOLDER} 0.09620328 0.4768839 1.483602 > 136 {HOLDER} => {HEART} 0.09620328 0.2992918 1.483602 > 151 {BAG} => {BOX} 0.09304546 0.3859275 1.334139 > 152 {BOX} => {BAG} 0.09304546 0.3216552 1.334139 > 149 {SIGN} => {HOLDER} 0.09069545 0.4203540 1.307736 > 150 {HOLDER} => {SIGN} 0.09069545 0.2821567 1.307736 > 141 {CASES} => {BOX} 0.08511420 0.4048201 1.399451 > 142 {BOX} => {CASES} 0.08511420 0.2942371 1.399451Similar to previously, sort the rules and examine the highest confidence and lift measures:
tmp <- as.data.frame(arules::inspect(head(sort(rulesx, by = "confidence"), 10))) > lhs rhs support confidence lift > 815 {BAG,BOWL,BOX,SIGN} => {HOLDER} 0.02070941 0.8571429 2.666601 > 631 {BAG,BOTTLE,BOWL} => {BOX} 0.02181097 0.8510029 2.941890 > 643 {BOWL,HEART,SIGN} => {HOLDER} 0.02151722 0.8492754 2.642125 > 820 {BAG,BOX,HEART,SIGN} => {HOLDER} 0.02137035 0.8434783 2.624090 > 632 {BOTTLE,BOWL,BOX} => {BAG} 0.02181097 0.8413598 3.489734 > 816 {BAG,BOWL,HOLDER,SIGN} => {BOX} 0.02070941 0.8392857 2.901385 > 817 {BOWL,BOX,HOLDER,SIGN} => {BAG} 0.02070941 0.8392857 3.481131 > 707 {BOTTLE,HEART,SIGN} => {HOLDER} 0.02144378 0.8366762 2.602929 > 651 {BAG,BOWL,HEART} => {HOLDER} 0.02115003 0.8347826 2.597038 > 719 {BOTTLE,CASES,SIGN} => {BAG} 0.02063597 0.8289086 3.438089
tmp <- as.data.frame(arules::inspect(head(sort(rulesx, by = "lift"), 10)))> lhs rhs support confidence lift > 602 {BAG,HOLDER,SIGN} => {COVER} 0.02012191 0.4667802 5.418710 > 598 {BAG,BOX,SIGN} => {COVER} 0.02019534 0.4661017 5.410833 > 819 {BAG,BOX,HOLDER,SIGN} => {BOWL} 0.02070941 0.6698337 5.254105 > 610 {BAG,BOX,HOLDER} => {COVER} 0.02070941 0.4483307 5.204534 > 606 {BOX,HOLDER,SIGN} => {COVER} 0.02004847 0.4333333 5.030435 > 634 {BAG,BOTTLE,BOX} => {BOWL} 0.02181097 0.6279070 4.925236 > 638 {BAG,HEART,SIGN} => {BOWL} 0.02034222 0.6141907 4.817647 > 662 {BAG,CASES,SIGN} => {BOWL} 0.02144378 0.6134454 4.811801 > 159 {BAG,BOWL} => {COVER} 0.02092972 0.4025424 4.672992 > 670 {CASES,HOLDER,SIGN} => {BOWL} 0.02078284 0.5811088 4.558156Once we have the rules built, we can use special subsetting functions to filter items from itemsets on either the left (lhs) or right (rhs) side of the association rule. This is valuable if you are looking for particular items within the itemsets.
Use the %in% operator to perform an exact match, or %pin% to perform a partial match:
# to see what 'Christmas' purchases imply.
lhs.rules <- subset(rulesx, subset = lhs %pin% "CHRISTMAS") lhs.rules> set of 44 rulesinspect(lhs.rules) > lhs rhs support confidence lift > 4 {CHRISTMAS} => {COVER} 0.02063597 0.1292548 1.500480 > 26 {CHRISTMAS} => {CANDLES} 0.02702504 0.1692732 1.481358 > 47 {CHRISTMAS} => {MUG} 0.02379379 0.1490340 1.060845 > 49 {CHRISTMAS} => {BOWL} 0.02680473 0.1678933 1.316937 > 51 {CHRISTMAS} => {BUNTING} 0.03018286 0.1890524 1.108191 > 53 {CHRISTMAS} => {BOTTLE} 0.04553132 0.2851886 1.797876 > 55 {CHRISTMAS} => {HEART} 0.03708600 0.2322907 1.151475 > 57 {CHRISTMAS} => {CASES} 0.04751414 0.2976081 1.415484 > 59 {CHRISTMAS} => {SIGN} 0.03547037 0.2221711 1.029715 > 61 {CHRISTMAS} => {BAG} 0.04663289 0.2920883 1.211504 > 63 {CHRISTMAS} => {BOX} 0.05500477 0.3445262 1.191016 > 65 {CHRISTMAS} => {HOLDER} 0.05133289 0.3215271 1.000282 > 253 {BOTTLE,CHRISTMAS} => {CASES} 0.02210472 0.4854839 2.309058 > 254 {CASES,CHRISTMAS} => {BOTTLE} 0.02210472 0.4652241 2.932850 > 256 {BOTTLE,CHRISTMAS} => {BAG} 0.02283910 0.5016129 2.080555 > 257 {BAG,CHRISTMAS} => {BOTTLE} 0.02283910 0.4897638 3.087552 > 259 {BOTTLE,CHRISTMAS} => {BOX} 0.02489535 0.5467742 1.890181 > 260 {BOX,CHRISTMAS} => {BOTTLE} 0.02489535 0.4526035 2.853288 > 262 {BOTTLE,CHRISTMAS} => {HOLDER} 0.02504223 0.5500000 1.711069 > 263 {CHRISTMAS,HOLDER} => {BOTTLE} 0.02504223 0.4878398 3.075423 > 265 {CHRISTMAS,HEART} => {BAG} 0.02004847 0.5405941 2.242239 > 266 {BAG,CHRISTMAS} => {HEART} 0.02004847 0.4299213 2.131139 > 268 {CHRISTMAS,HEART} => {BOX} 0.02203128 0.5940594 2.053645 > 269 {BOX,CHRISTMAS} => {HEART} 0.02203128 0.4005340 1.985465 > 271 {CHRISTMAS,HEART} => {HOLDER} 0.02247191 0.6059406 1.885102 > 272 {CHRISTMAS,HOLDER} => {HEART} 0.02247191 0.4377682 2.170036 > 274 {CASES,CHRISTMAS} => {BAG} 0.02291254 0.4822257 2.000142 > 275 {BAG,CHRISTMAS} => {CASES} 0.02291254 0.4913386 2.336904 > 277 {CASES,CHRISTMAS} => {BOX} 0.02548285 0.5363215 1.854047 > 278 {BOX,CHRISTMAS} => {CASES} 0.02548285 0.4632844 2.203473 > 280 {CASES,CHRISTMAS} => {HOLDER} 0.02283910 0.4806801 1.495412 > 281 {CHRISTMAS,HOLDER} => {CASES} 0.02283910 0.4449213 2.116135 > 283 {CHRISTMAS,SIGN} => {BAG} 0.02137035 0.6024845 2.498943 > 284 {BAG,CHRISTMAS} => {SIGN} 0.02137035 0.4582677 2.123973 > 286 {CHRISTMAS,SIGN} => {BOX} 0.02342660 0.6604555 2.283174 > 287 {BOX,CHRISTMAS} => {SIGN} 0.02342660 0.4259012 1.973961 > 289 {CHRISTMAS,SIGN} => {HOLDER} 0.02276566 0.6418219 1.996731 > 290 {CHRISTMAS,HOLDER} => {SIGN} 0.02276566 0.4434907 2.055484 > 292 {BAG,CHRISTMAS} => {BOX} 0.02687817 0.5763780 1.992521 > 293 {BOX,CHRISTMAS} => {BAG} 0.02687817 0.4886515 2.026795 > 295 {BAG,CHRISTMAS} => {HOLDER} 0.02460160 0.5275591 1.641255 > 296 {CHRISTMAS,HOLDER} => {BAG} 0.02460160 0.4792561 1.987825 > 298 {BOX,CHRISTMAS} => {HOLDER} 0.02812661 0.5113485 1.590823 > 299 {CHRISTMAS,HOLDER} => {BOX} 0.02812661 0.5479256 1.894162# what purchases yielded Candles?
rhs.rules <- subset(rulesx, subset = rhs %pin% c("CANDLES")) rhs.rules> set of 26 rulestmp <- as.data.frame(arules::inspect(head(sort(rhs.rules, by = "support"), 10)))> lhs rhs support confidence lift > 46 {HOLDER} => {CANDLES} 0.04920320 0.1530729 1.339584 > 44 {BOX} => {CANDLES} 0.04714695 0.1629855 1.426333 > 38 {CASES} => {CANDLES} 0.03958287 0.1882641 1.647552 > 42 {BAG} => {CANDLES} 0.03789381 0.1571733 1.375469 > 36 {HEART} => {CANDLES} 0.03473599 0.1721878 1.506865 > 40 {SIGN} => {CANDLES} 0.03429537 0.1589517 1.391031 > 34 {BOTTLE} => {CANDLES} 0.02908130 0.1833333 1.604402 > 30 {BOWL} => {CANDLES} 0.02805317 0.2200461 1.925686 > 252 {BOX,HOLDER} => {CANDLES} 0.02768598 0.2800892 2.451140 > 32 {BUNTING} => {CANDLES} 0.02739223 0.1605682 1.405178
inspect(head(sort(rhs.rules, by = "confidence")))> lhs rhs support confidence lift > 219 {BAG,HEART} => {CANDLES} 0.02056253 0.3517588 3.078342 > 213 {BOWL,BOX} => {CANDLES} 0.02019534 0.3467844 3.034809 > 216 {BOTTLE,HOLDER} => {CANDLES} 0.02026878 0.3183391 2.785876 > 222 {BOX,HEART} => {CANDLES} 0.02247191 0.3122449 2.732544 > 237 {BAG,SIGN} => {CANDLES} 0.02232503 0.3111566 2.723020 > 234 {CASES,HOLDER} => {CANDLES} 0.02232503 0.3095723 2.709156The plot() function in the arules package is also very flexible. You can look at various scatterplots across the various metrics, or even count and group a small number of rules and show the metrics as bubbles:
plot(rhs.rules, method = "scatterplot")Here is the scatter plot for 26 rules, with each individual point color coded according to the lift for that rule:

set.seed(1)high_lift <- subset(rules,lift >= 1)plot(high_lift, method = "grouped",control=list(col=grey.colors(10), gp_labels=gpar(col='black',cex=1)))
If you wish to generate as many rules as possible, set support and confidence to a very low number:
many_rules <- apriori(trans4, parameter = list(minlen = 1, support = 0.01, confidence = 0.01)) > Apriori > > Parameter specification: > confidence minval smax arem aval originalSupport support minlen maxlen > 0.01 0.1 1 none FALSE TRUE 0.01 1 10 > target ext > rules FALSE > > Algorithmic control: > filter tree heap memopt load sort verbose > 0.1 TRUE TRUE FALSE TRUE 2 TRUE > > Absolute minimum support count: 136 > > set item appearances ...[0 item(s)] done [0.00s]. > set transactions ...[13 item(s), 13617 transaction(s)] done [0.00s]. > sorting and recoding items ... [13 item(s)] done [0.00s]. > creating transaction tree ... done [0.00s]. > checking subsets of size 1 2 3 4 5 6 7 8 done [0.00s]. > writing ... [8898 rule(s)] done [0.00s]. > creating S4 object ... done [0.00s]. many_rules > set of 8898 rulesPlots are especially helpful for scenarios in which there are many rules generated, and you need to filter on specific support and confidence ranges.
Here is a plot that shows two of the three metrics, along the x and y axis, and the third metric (lift, support, or confidence) as shading:
sel <- plot(many_rules, measure = c("support", "confidence"), shading = "lift", interactive = FALSE)As the following plot suggests, there is a cluster of rules with high lift (>8), high confidence (> 0.6), but all with low support:

Now that you know how to run association rules using the coerce to dataframe method, we will now illustrate the write to file method:
- In the write to file method, each item is written to a separate line, along with the identifying key, which in our case is the InvoiceId
- The advantage to the write to file method is that very large data files can be accumulated separately, and then combined together if needed
- You can use the file.show function to display the contents of the file that will be input to the association rules algorithm:
setwd("C:/PracticalPredictiveAnalytics/Data")load("OnlineRetail.full.Rda")OnlineRetail <- OnlineRetail[1:100,]nrow(OnlineRetail)> [1] 268034head(OnlineRetail)> InvoiceNo StockCode Description Quantity > 5 6365 71053 METAL LANTERN 6 > 6 536365 21730 GLASS STAR FROSTED T-LIGHT HOLDER 6 > 2 536365 22752 SET 7 BABUSHKA NESTING BOXES 2 > 4 536365 84029E WOOLLY HOTTIE HEART. 6 > 1 536365 84406B CREAM CUPID HEARTS COAT HANGER 8 > 8 536366 22632 HAND WARMER POLKA DOT 6 > InvoiceDate UnitPrice CustomerID Country itemcount > 5 12/1/2010 8:26 3.39 17850 United Kingdom 7 > 6 12/1/2010 8:26 4.25 17850 United Kingdom 7 > 2 12/1/2010 8:26 7.65 17850 United Kingdom 7 > 4 12/1/2010 8:26 3.39 17850 United Kingdom 7 > 1 12/1/2010 8:26 2.75 17850 United Kingdom 7 > 8 12/1/2010 8:28 1.85 17850 United Kingdom 2 > Desc2 lastword firstword > 5 MetalLantern LANTERN METAL > 6 GlassStarFrostedT-lightHolder HOLDER GLASS > 2 Set7BabushkaNestingBoxes BOXES SET > 4 WoollyHottieHeart. HEART. WOOLLY > 1 CreamCupidHeartsCoatHanger HANGER CREAM > 8 HandWarmerPolkaDot DOT HAND # concatenate the Invoice Number to the Description separated by a delimiter data2 <- paste(OnlineRetail$InvoiceNo, OnlineRetail$Desc2, sep = "!")
# eliminate duplicates data2 <- unique(data2) #
write(data2, file = "demo_single") file.show('demo_single') #not runUse the read.transaction file to read the delimited file back into memory formatted as a transaction file. This will have the same results as coercing the dataframe to a transaction file, which we did earlier.
The difference is that the transactions are formatted one transaction per line, as specified by the format=‘single’ option. We also specify where the TransactionID and item descriptions are via the cols option. It is possible to have multiple descriptors and transaction IDs, and identify them with the cols options. The sep keyword designates the delimiter, which in this case is the ! character. There is also a remove duplicate transactions option, which is a logical value that determines whether or not you want to eliminate duplicates.
The returned object, trans, is an itemMatrix. You can type trans to see the dimension, or run the dim(trans) to see the dimensions. This will tell you how many transactions the itemMatrix is based upon.
As before, to view the items in the trans object, use the inspect() function:
library(arules) library(arulesViz) setwd("C:/Users/randy/Desktop/ch6")
file.show('demo_single') trans <- read.transactions("demo_single", format = "single", sep = "!", cols = c(1, 2), rm.duplicates = FALSE, quote = "") trans > transactions in sparse format with > 19403 transactions (rows) and > 3462 items (columns) dim(trans) > [1] 19403 3462 inspect(trans[1:5]) > items transactionID > 1 {CreamCupidHeartsCoatHanger, > GlassStarFrostedT-lightHolder, > MetalLantern, > Set7BabushkaNestingBoxes, > WoollyHottieHeart.} 536365 > 2 {HandWarmerPolkaDot} 536366 > 3 {FeltcraftPrincessCharlotteDoll, > KnittedMugCosy, > LoveBuildingBlockWord, > Poppy'sPlayhouseKitchen} 536367 > 4 {CoatRackParisFashion} 536368 > 5 {CircusParadeLunchBox, > LunchBoxILoveLondon, > MiniJigsawSpaceboy, > PandaAndBunniesStickerSheet, > Postage, > RoundSnackBoxesSetOf4Woodland, > SpaceboyLunchBox, > ToadstoolLedNightLight} 536370 dim(trans) > [1] 19403 3462# look up any item in labels to see if it is there.Take a look at some of the frequently purchased items. Use the itemFrequencePlot() function to see a simple bar chart of the top item purchases:
itemFrequencyPlot(trans, topN = 10, cex.names = 1)The following chart shows two heart items among the top purchases:

The itemLabels() function lists all of the labels associated with the itemset. Since the top ranked item has an unusual abbreviation in it (T-light), you could check to see if there are other items that have that term in it. To accomplish this, use the grep() function:
result <- grep("T-light", itemLabels(trans), value = TRUE) str(result) > chr [1:96] "6ChocolateLoveHeartT-lights" ... head(result) > [1] "6ChocolateLoveHeartT-lights" "AgedGlassSilverT-lightHolder" > [3] "AntiqueSilverT-lightGlass" "AssortedColourT-lightHolder" > [5] "BeadedChandelierT-lightHolder" "BonneJamJarT-lightHolder"Apply the rules engine again. We will use a small support and confidence level to generate many rules:
rules1 <- apriori(trans, parameter = list(minlen = 1, support = 0.001, confidence = 0.001)) > Apriori > > Parameter specification: > confidence minval smax arem aval originalSupport support minlen maxlen > 0.001 0.1 1 none FALSE TRUE 0.001 1 10 > target ext > rules FALSE > > Algorithmic control: > filter tree heap memopt load sort verbose > 0.1 TRUE TRUE FALSE TRUE 2 TRUE > > Absolute minimum support count: 19 > > set item appearances ...[0 item(s)] done [0.00s]. > set transactions ...[3462 item(s), 19403 transaction(s)] done [0.03s]. > sorting and recoding items ... [2209 item(s)] done [0.01s]. > creating transaction tree ... done [0.01s]. > checking subsets of size 1 2 3 4 done [0.20s]. > writing ... [63121 rule(s)] done [0.02s]. > creating S4 object ... done [0.02s]. rules1 > set of 63121 rulesSort the rules by the three measures: support, confidence, and lift to get an idea of some of the more valuable rules. Sort by confidence, support, and lift to look at the highest scoring rules in each category:
tmp <- as.data.frame(inspect(tail(sort(rules1, by = "lift")))) > lhs rhs > 38860 {AssortedColourBirdOrnament} => {StorageBagSuki} > 38861 {StorageBagSuki} => {AssortedColourBirdOrnament} > 38893 {BagRetrospot} => {AssortedColourBirdOrnament} > 38892 {AssortedColourBirdOrnament} => {BagRetrospot} > 11539 {AlarmClockBakelike} => {RexCash+carryShopper} > 11538 {RexCash+carryShopper} => {AlarmClockBakelike} > support confidence lift > 38860 0.001030768 0.02724796 0.8976097 > 38861 0.001030768 0.03395586 0.8976097 > 38893 0.001700768 0.03122044 0.8252999 > 38892 0.001700768 0.04495913 0.8252999 > 11539 0.001082307 0.01621622 0.7183636 > 11538 0.001082307 0.04794521 0.7183636 tmp <- as.data.frame(inspect(head(sort(rules1, by = "support")))) > lhs rhs support confidence lift > 2207 {} => {HangingHeartT-lightHolder} 0.07256610 0.07256610 1 > 2208 {} => {HeartOfWicker} 0.07004072 0.07004072 1 > 2209 {} => {AlarmClockBakelike} 0.06674226 0.06674226 1 > 2205 {} => {BagRetrospot} 0.05447611 0.05447611 1 > 2201 {} => {RegencyCakesd3Tier} 0.05251765 0.05251765 1 > 2190 {} => {PartyBunting} 0.04184920 0.04184920 1 tmp <- as.data.frame(inspect(head(sort(rules1, by = "confidence")))) > lhs rhs support confidence lift > 1 {PolkadotCup, > RetrospotCharlotteBag, > SpotCeramicDrawerKnob} => {AlarmClockBakelike} 0.001082307 1.0000000 14.98301 > 2 {AlarmClockBakelike, > Charlie+lolaHotWaterBottle, > ChristmasGinghamTree} => {BabushkaNotebook} 0.001185384 0.9200000 48.24530 > 3 {ChristmasHangingStarWithBell, > RegencyTeacupAndSaucer} => {AlarmClockBakelike} 0.001133845 0.9166667 13.73443 > 4 {PolkadotBowl, > RetrospotCharlotteBag, > SpotCeramicDrawerKnob} => {AlarmClockBakelike} 0.001133845 0.9166667 13.73443 > 5 {AlarmClockBakelikeChocolate, > PolkadotCup} => {AlarmClockBakelike} 0.001030768 0.9090909 13.62092 > 6 {BabushkaNotebook, > Charlie+lolaHotWaterBottle, > HeartMeasuringSpoons} => {AlarmClockBakelike} 0.001339999 0.8965517 13.43304You can also coerce the rules to a dataframe and use the kable() function to print the first 10 rows, or subset as you choose:
rules1 <- sort(rules1, by = "confidence")rules1.df <- as(rules1, "data.frame")cat("using kable to print rules")> using kable to print rules library(knitr) kable(rules1.df[1:10,],digits=6,col.names=c("Rules","Supp","Conf","Lift"),align=c("l","l","l","l"))
The default plot of the rules will give you a scatterplot of all of the rules showing support on the x axis and confidence on the y axis. We can see from the density that confidence can vary from high to low with most of the density occurring at the 0.5 level. Support tends to be low, and the highest support level attains ~0.07:
plot(rules1)
As we did before, we can look at some of the subsets by parsing the left or right side.
For example, we might be interested in seeing what items yielded purchasing chocolate things:
- Subset the rules set using the %pin% operator (partial match), and look for any transactions where chocolate appears in the right-hand side:
purchased.this <- "Chocolate"
lhs.rules <- subset(rules1, subset = rhs %pin%purchased.this)- Printing lhs.rules shows that there are 487 of them:
print(lhs.rules) > set of 487 rules- Sort them by lift, inspect them, and plot the first 15 as a graph:
lhs.rules <- sort(lhs.rules, by = "lift")
inspect(head(sort(lhs.rules, by = "lift"))) > lhs rhs support confidence lift > 1 {CakeTowelSpots} => {CakeTowelChocolateSpots} 0.001185384 0.2911392 89.66626 > 2 {BiscuitsBowlLight, > DollyMixDesignBowl} => {ChocolatesBowl} 0.001030768 0.4444444 68.98844 > 3 {BakingMouldHeartChocolate} => {BakingMouldHeartMilkChocolate} 0.001030768 0.2941176 57.64409 > 4 {BakingMouldHeartMilkChocolate} => {BakingMouldHeartChocolate} 0.001030768 0.2020202 57.64409 > 5 {BiscuitsBowlLight} => {ChocolatesBowl} 0.001700768 0.3586957 55.67817 > 6 {MarshmallowsBowl} => {ChocolatesBowl} 0.002422306 0.2397959 37.22208- The directional graph is a good way to illustrate which purchases influence other purchases once you have narrowed down the number of itemset to a small number:
plot(lhs.rules[1:15], method = "graph", control = list(type ="items", cex = 0.5))- The following plot shows that DollyMixDesignBowl and MarshmallowsBowl appear as larger and darker bubbles, indicating that they are better predictors for chocolate purchase relative to support, confidence, and lift:

- Finally, if you wish to save your workspace, use the save.image command:
save.image(file = "ch6 part 2.Rdata")In the previous sections, we used the lastword technique for categorizing types of purchases by simple keywords. We could also use more sophisticated techniques such as word clustering to try to identify which types of purchasing clusters occur and then use that to subset the association rules. To illustrate text clustering on our OnlineRetail dataset, we will first need to load our training and test dataframes that we previously saved. Also, issue a set.seed command since we will be doing some sampling later on:
setwd("C:/Users/randy/Desktop/ch6") # load the training data load("OnlineRetail.full.Rda") set.seed(1)We previously demonstrated some text mining examples using a package called RTextTools. Another popular text mining package is t m. The t m package has been around for a long time, and it will be useful to know how this package works. Tm requires that all text data be converted to a corpus first. That can be done using the VCorpus() function. We can use vector input, since we already have the data available in an existing data frame, and there is no need to read in additional external data:
library(tm) > Loading required package: NLP attach(OnlineRetail) nrow(OnlineRetail) > [1] 268034 corp <- VCorpus(VectorSource(OnlineRetail$Description))Displaying the corp object shows you some information about the metadata:
head(corp) > <<VCorpus>> > Metadata: corpus specific: 0, document level (indexed): 0 > Content: documents: 6Once we have a corpus, we can proceed to convert it to a document term matrix. When building DTM, care must be given to limiting the amount of data and resulting terms that are processed. If not parameterized correctly, it can take a very long time to run. Parameterization is accomplished via the options. We will remove any stopwords, punctuation, and numbers. Additionally, we will only include a minimum word length of four:
library(tm) dtm <- DocumentTermMatrix(corp, control = list(removePunctuation = TRUE, wordLengths = c(4, 999), stopwords = TRUE, removeNumbers = TRUE, stemming = FALSE, bounds = list(global = c(5, Inf))))We can begin to look at the data by using the inspect() function.
This is different from the inspect() function in an arules package, and if you have the arules package loaded, you will want to preface this inspect with tm::inspect:
inspect(dtm[1:10, 1:10]) > <<DocumentTermMatrix (documents: 10, terms: 10)>> > Non-/sparse entries: 0/100 > Sparsity : 100% > Maximal term length: 8 > Weighting : term frequency (tf) > > Terms > Docs abstract acapulco account acrylic address adult advent afghan aged > 1 0 0 0 0 0 0 0 0 0 > 2 0 0 0 0 0 0 0 0 0 > 3 0 0 0 0 0 0 0 0 0 > 4 0 0 0 0 0 0 0 0 0 > 5 0 0 0 0 0 0 0 0 0 > 6 0 0 0 0 0 0 0 0 0 > 7 0 0 0 0 0 0 0 0 0 > 8 0 0 0 0 0 0 0 0 0 > 9 0 0 0 0 0 0 0 0 0 > 10 0 0 0 0 0 0 0 0 0 > Terms > Docs ahoy > 1 0 > 2 0 > 3 0 > 4 0 > 5 0 > 6 0 > 7 0 > 8 0 > 9 0 > 10 0After the DTM has been created, we can look at the metadata that has been produced by issuing a print(dtm) command. We can see the number of documents and terms by looking at the first line:
print(dtm) > <<DocumentTermMatrix (documents: 268034, terms: 1675)>> > Non-/sparse entries: 826898/448130052 > Sparsity : 100% > Maximal term length: 20 > Weighting : term frequency (tf)Most TDMs are initially filled with a lot of empty space. That is because every word in a corpus is indexed, and there are many words that occur so infrequently that they do not matter analytically. Removing sparse terms is a method in which we can reduce the number of terms to a manageable size, and also save space at the same time.
The removeSparseTerms() function will reduce the number of terms in the description from 268034 to 62:
dtms <- removeSparseTerms(dtm, 0.99)dim(dtms) > [1] 268034 62As an alternative to inspect, we can also View() it in matrix form:
View(as.matrix(dtms))Here is the output from the View command. A 1 indicates that the term occurs, and 0 indicates it did not occur:

The tm package has a useful function called findFreqTerms, which is useful to find the frequency of the popular terms used. The second argument to the function restricts the results to terms that have a minimum frequency specified. We can also compute the occurrences by summing up the 1s and 0s for each term in the TDM. Then we can sort the list and display the highest and lowest frequency occurrences:
data.frame(findFreqTerms(dtms, 10000, Inf)) > findFreqTerms.dtms..10000..Inf. > 1 cake > 2 christmas > 3 design > 4 heart > 5 metal > 6 retrospot > 7 vintage freq <- colSums(as.matrix(dtms)) # there are xx terms length(freq) > [1] 62 ord <- order(freq) # look at the top and bottom number of terms freq[head(ord, 12)] > union skull zinc bird wood wall birthday > 2752 2770 2837 2974 2993 3042 3069 > colour charlotte star antique silver > 3089 3114 3121 3155 3175 freq[tail(ord, 10)] > hanging sign lunch metal cake christmas design > 8437 8580 9107 10478 10623 12534 14884 > vintage retrospot heart > 16755 17445 19520For presentation purposes, a barplot is also useful for displaying the relative frequencies:
barplot(freq[tail(ord, 10)], cex.names = 0.75, col = c("blue"))The most popular term is heart:

We could also do a little code manipulation to only display the topN most frequent terms:
dtmx <- dtms[, names(tail(sort(colSums(as.matrix(dtms))), 12))] inspect(dtmx[1:10, ]) > <<DocumentTermMatrix (documents: 10, terms: 12)>> > Non-/sparse entries: 3/117 > Sparsity : 98% > Maximal term length: 9 > Weighting : term frequency (tf) > > Terms > Docs pack holder hanging sign lunch metal cake christmas design vintage > 1 0 0 0 0 0 1 0 0 0 0 > 2 0 1 0 0 0 0 0 0 0 0 > 3 0 0 0 0 0 0 0 0 0 0 > 4 0 0 0 0 0 0 0 0 0 0 > 5 0 0 0 0 0 0 0 0 0 0 > 6 0 0 0 0 0 0 0 0 0 0 > 7 0 0 0 0 0 0 0 0 0 0 > 8 0 0 0 0 0 0 0 0 0 0 > 9 0 0 0 0 0 0 0 0 0 0 > 10 0 0 0 0 0 0 0 0 0 0 > Terms > Docs retrospot heart > 1 0 0 > 2 0 0 > 3 0 0 > 4 0 1 > 5 0 0 > 6 0 0 > 7 0 0 > 8 0 0 > 9 0 0 > 10 0 0Now we can cluster the term document matrix using k-means. For illustration purposes, we will specify that five clusters be generated:
kmeans5 <- kmeans(dtms, 5)Once k-means is done, we will append the cluster number to the original data, and then create five subsets based upon the cluster:
kw_with_cluster <- as.data.frame(cbind(OnlineRetail, Cluster = kmeans5$cluster))
# subset the five clusters cluster1 <- subset(kw_with_cluster, subset = Cluster == 1) cluster2 <- subset(kw_with_cluster, subset = Cluster == 2) cluster3 <- subset(kw_with_cluster, subset = Cluster == 3) cluster4 <- subset(kw_with_cluster, subset = Cluster == 4) cluster5 <- subset(kw_with_cluster, subset = Cluster == 5)Print out a sample of the data:
> head(cluster1[10:13]) Desc2 lastword firstword Cluster 50 VintageBillboardLove/hateMug MUG VINTAGE 1 86 BagVintagePaisley PAISLEY BAG 1113 ShopperVintagePaisley PAISLEY SHOPPER 1145 ShopperVintagePaisley PAISLEY SHOPPER 1200 VintageHeadsAndTailsCardGame GAME VINTAGE 1210 PaperChainKitVintageChristmas CHRISTMAS PAPER 1Table the frequencies and print out the most popular terms in the clusters. Observe that many of these items have to do with christmas and paisley items, which seem to occur together:
tail(sort(table(cluster1$lastword)), 10) MUG GAME BUNTING CARDS DESIGN LEAF 427 431 456 482 535 717 911 DOILY PAISLEY CHRISTMAS 1073 1699 1844After tabling cluster 2, look at a portion of the file. When looking at both the first and last words together, it seems like this cluster has something to do with hanging holders:
> head(cluster2[10:13]) Desc2 lastword firstword Cluster 6 GlassStarFrostedT-lightHolder HOLDER GLASS 2 57 HangingHeartT-lightHolder HOLDER HANGING 2 62 GlassStarFrostedT-lightHolder HOLDER GLASS 2 70 HangingHeartT-lightHolder HOLDER HANGING 2 81 HangingHeartT-lightHolder HOLDER HANGING 2 156 ColourGlassT-lightHolderHanging HANGING COLOUR 2Rather than looking just at records, look at the frequencies of the most popular words. Heart, hanging, and folder are the three most frequently occurring words in cluster 2:
tail(sort(table(cluster2$lastword)), 10)| ANTIQUE | LANTERN | HLDR | DECORATION | GLASS | T-LIGHT | HANGING | HEART | HOLDER | |
|---|---|---|---|---|---|---|---|---|---|
| 167 | 181 | 226 | 260 | 319 | 361 | 531 | 639 | 668 | 6792 |
Cluster 3 may have to do with customers purchasing sets of different signs. Set, sign, box, and design are the four most common words:
head(cluster3[10:13]) Desc2 lastword firstword Cluster 5 MetalLantern LANTERN METAL 3 2 Set7BabushkaNestingBoxes BOXES SET 3 4 WoollyHottieHeart. HEART. WOOLLY 3 1 CreamCupidHeartsCoatHanger HANGER CREAM 3 8 HandWarmerPolkaDot DOT HAND 3 10 FeltcraftPrincessCharlotteDoll DOLL FELTCRAFT 3> tail(sort(table(cluster3$lastword)),10)CANDLES POLKADOT BUNTING BAG DECORATION SET SIGN BOX DESIGN2307 2539 2610 3084 3156 3844 6184 6277 10975 16627Cluster 4 could be looked at as customers who purchased retrospot products:
> head(cluster4[10:13]) Desc2 lastword firstword Cluster 100 LunchBoxWithCutleryRetrospot RETROSPOT LUNCH 4 102 PackOf72RetrospotCakeCases CASES PACK 4 84 60TeatimeFairyCakeCases CASES 60 4 94 3PieceRetrospotCutlerySet SET 3 4 91 LunchBagRetrospot RETROSPOT LUNCH 4 127 RetrospotMilkJug JUG RETROSPOT 4tail(sort(table(cluster4$lastword)), 10) APRON NAPKINS BANK PC TINS DESIGN 486 511 512 514 531 664 1203 BAG CASES RETROSPOT 1395 4318 6485Finally, cluster 5 seems to be concerned with the purchases of Bottles, possibly having to do with perfumes, elixirs, and tonics:
head(cluster5)
> head(cluster5[10:13]) Desc2 lastword firstword Cluster59 KnittedUnionFlagHotWaterBottle BOTTLE KNITTED 568 KnittedUnionFlagHotWaterBottle BOTTLE KNITTED 5181 AssortedBottleTopMagnets MAGNETS ASSORTED 5206 EnglishRoseHotWaterBottle BOTTLE ENGLISH 5229 RetrospotHeartHotWaterBottle BOTTLE RETROSPOT 5261 HotWaterBottleTeaAndSympathy SYMPATHY HOT 5tail(sort(table(cluster5$lastword)), 10)BABUSHKA MAGNETS TONIC ELIXIR PERFUME OPENER SYMPATHY POORLY CALM BOTTLE 87 95 108 112 132 149 318 345 418 3795The goal in this exercise is to score the test dataset, by assigning clusters based upon the predict method for the training dataset.
The standard kmeans function does not have a prediction method. However, we can use the flexclust package which does. Since the prediction method can take a long time to run, we will illustrate it only on a sample number of rows and columns. In order to compare the test and training results, they also need to have the same number of columns. For illustration purposes, we will set the number at 10.
To begin, take a sample from the OnlineRetail training data:
set.seed(1) sample.size <- 10000 max.cols <- 10
library("flexclust") OnlineRetail <- OnlineRetail[1:sample.size, ]Next, create the document term matrix from the description column in the sampled dataset. We will use the create_matrix function from the RTextTools package, which can create a TDM first without having a separate step of creating a corpus:
require(tm) library(RTextTools) #Create the DTM for the training data
dtMatrix <- create_matrix(OnlineRetail$Description, minDocFreq = 1, removeNumbers = TRUE, minWordLength = 4, removeStopwords = TRUE, removePunctuation = TRUE, stemWords = FALSE, weighting = weightTf)Next, check the dimensions of the data. We can see that there are 1300 terms. Virtually all of them are sparse terms, so we will remove them from the matrix:
dim(dtMatrix)> [1] 10000 1300 dtMatrix <- removeSparseTerms(dtMatrix, 0.99)Removing the sparse terms reduces the number of columns from 1300 to 62!
dim(dtMatrix) > [1] 10000 62We will only keep the same number of terms (max.cols) in test and train:
dtMatrix <- dtMatrix[, 1:max.cols]In order to view rows of the dtMatrix that have at least two occurrences of the first 10 terms, we can use the rowSums function to add up the 1 and 0 indicators for each term:
tmp <- rowSums(as.matrix(dtMatrix)) tmp3 <- data.frame(tmp[tmp>1])Then we will extract the row indices for the >1 condition:
selected <- as.numeric(rownames(tmp3)) head(selected) > head(selected) [1] 3 38 68 83 92 111Print out the first 10 rows of the DTM of the first 10 terms that meet this condition:
kable(tm::inspect(dtMatrix[selected[1:10],]))
Run kmeans function using the kcca package to generate five cluster assignments:
# repeat kmeans using the kcca function. Clusters=5
clust1 = kcca(dtMatrix, k = 5, kccaFamily("kmeans")) clust1 > kcca object of family 'kmeans' > > call: > kcca(x = dtMatrix, k = 5, family = kccaFamily("kmeans")) > > cluster sizes: > > 1 2 3 4 5 > 360 120 152 387 8981Print the number categorized for each cluster:
table(clust1@cluster) > > 1 2 3 4 5 > 360 120 152 387 8981Merge the clusters with the training data, and show some sample records displaying the cluster assigned to each:
kw_with_cluster2 <- as.data.frame(cbind(OnlineRetail, Cluster = clust1@cluster))
head(kw_with_cluster2) > InvoiceNo StockCode Description Quantity> 5 536365 71053 METAL LANTERN 6> 6 536365 21730 GLASS STAR FROSTED T-LIGHT HOLDER 6> 2 536365 22752 SET 7 BABUSHKA NESTING BOXES 2> 4 536365 84029E WOOLLY HOTTIE HEART. 6> 1 536365 84406B CREAM CUPID HEARTS COAT HANGER 8> 8 536366 22632 HAND WARMER POLKA DOT 6> InvoiceDate UnitPrice CustomerID Country itemcount> 5 12/1/2010 8:26 3.39 17850 United Kingdom 7> 6 12/1/2010 8:26 4.25 17850 United Kingdom 7> 2 12/1/2010 8:26 7.65 17850 United Kingdom 7> 4 12/1/2010 8:26 3.39 17850 United Kingdom 7> 1 12/1/2010 8:26 2.75 17850 United Kingdom 7> 8 12/1/2010 8:28 1.85 17850 United Kingdom 2> Desc2 lastword firstword Cluster> 5 MetalLantern LANTERN METAL 5> 6 GlassStarFrostedT-lightHolder HOLDER GLASS 5> 2 Set7BabushkaNestingBoxes BOXES SET 5> 4 WoollyHottieHeart. HEART. WOOLLY 5> 1 CreamCupidHeartsCoatHanger HANGER CREAM 5> 8 HandWarmerPolkaDot DOT HAND 5Run the predict method on the training set. We will eventually apply it to the test data:
pred_train <- predict(clust1)Now let’s switch over to the test data set. First, take an identical sample size as was taken for the training data, and repeat the procedure starting with creating the term document matrix on the sample:
OnlineRetail.test <- OnlineRetail.test[1:sample.size, ]dtMatrix.test <- create_matrix(OnlineRetail.test$Description, minDocFreq = 1,removeNumbers = TRUE, minWordLength = 4, removeStopwords = TRUE, removePunctuation = TRUE,stemWords = FALSE, weighting = weightTf)As we did before, remove sparse terms from the matrix. Then, use the dim() function to see how many non-sparse terms remain:
dtMatrix.test <- removeSparseTerms(dtMatrix.test, 0.99)
dim(dtMatrix.test) # reduced to 61 terms
> [1] 10000 61Take the first max.col (10) terms:
dtMatrix.test <- dtMatrix.test[, 1:max.cols]
dtMatrix.test > <<DocumentTermMatrix (documents: 10000, terms: 10)>> > Non-/sparse entries: 2072/97928 > Sparsity : 98% > Maximal term length: 8 > Weighting : term frequency (tf)Display a portion of files where terms appear at least twice. Note that the first 10 terms of the test DTM is different from the DTM of the training set, although some of the terms occur in both. Since we are taking the first 10 terms just to illustrate the technique, this is normal and expected:
tmp <- rowSums(as.matrix(dtMatrix.test))tmp3 <- data.frame(tmp[tmp>1])selected <- as.numeric(rownames(tmp3))head(selected)#library(knitr)kable(tm::inspect(dtMatrix.test[selected[1:10],]))
Verify that the test and training data have the same number of dimensions. This is important since the predict method will fail if the number of columns are different:
dim(dtMatrix) > [1] 10000 10 dim(dtMatrix.test) > [1] 10000 10Run the prediction function on the training data, and apply it to the test data:
pred_test <- predict(clust1, newdata = dtMatrix.test)First, look at the table the cluster assignments for the test data:
table(pred_test) > pred_test > 1 2 3 4 5 > 171 113 201 146 9369Finally, merge the clusters with the test data, and show the cluster categories assigned to each. For this demonstration, display two transactions for each cluster:
kw_with_cluster2_score <- as.data.frame(cbind(OnlineRetail.test, Cluster=pred_test))head(kw_with_cluster2_score)clust1.score=head(subset(kw_with_cluster2_score,Cluster==1),2)clust2.score=head(subset(kw_with_cluster2_score,Cluster==2),2)clust3.score=head(subset(kw_with_cluster2_score,Cluster==3),2)clust4.score=head(subset(kw_with_cluster2_score,Cluster==4),2)clust5.score=head(subset(kw_with_cluster2_score,Cluster==5),2)head(clust1.score[,10:13])head(clust2.score[,10:13])head(clust3.score[,10:13])head(clust4.score[,10:13])head(clust5.score[,10:13])
> head(clust1.score[,10:13]) Desc2 lastword firstword Cluster89 PackOf60PaisleyCakeCases CASES PACK 196 PackOf60DinosaurCakeCases CASES PACK 1
> head(clust2.score[,10:13]) Desc2 lastword firstword Cluster61 WoodenFrameAntique WOODEN 2140 AntiqueGlassDressingTablePot POT ANTIQUE 2
> head(clust3.score[,10:13]) Desc2 lastword firstword Cluster148 3TierCakeTinAndCream CREAM 3 3143 3TierCakeTinAndCream CREAM 3 3
> head(clust4.score[,10:13]) Desc2 lastword firstword Cluster126 ZincWillieWinkieCandleStick STICK ZINC 4488 LoveBirdCandle CANDLE LOVE 4
> head(clust5.score[,10:13]) Desc2 lastword firstword Cluster3 HangingHeartT-lightHolder HOLDER HANGING 57 KnittedUnionFlagHotWaterBottle BOTTLE KNITTED 5Circling back to the apriori algorithm, we can use the predicted clusters that were generated instead of lastword, in order to develop some rules:
- We will use the coerce to dataframe method to generate the transaction file as previously generated
- Create a rules_clust object, which builds association rules based upon the itemset of clusters {1,2,3,4,5}
- Inspect some of the generated rules by lift:
library(arules)colnames(kw_with_cluster2_score)kable(head(kw_with_cluster2_score[,c(1,13)],5))tmp <-data.frame(kw_with_cluster2_score[,1],kw_with_cluster2_score[,13])names(tmp) [1] <- "TransactionID"names(tmp) [2] <- "Items"tmp <- unique(tmp)trans4 <- as(split(tmp[,2], tmp[,1]), "transactions") rules_clust<- apriori(trans4,parameter = list(minlen=2,support =0.02,confidence = 0.01)) summary(rules_clust)tmp <- as.data.frame(inspect( head(sort(rules_clust, by="lift"),10)
> tmp <- as.data.frame(inspect( head(sort(rules_clust,by="lift"),10) ) ) lhs rhs support confidence lift22 {2,5} => {4} 0.03065693 0.3088235 3.0220591 {2} => {4} 0.03065693 0.3043478 2.9782612 {4} => {2} 0.03065693 0.3000000 2.97826123 {4,5} => {2} 0.03065693 0.3000000 2.97826132 {1,5} => {4} 0.02773723 0.2087912 2.0431719 {4} => {1} 0.02773723 0.2714286 2.02096310 {1} => {4} 0.02773723 0.2065217 2.02096331 {4,5} => {1} 0.02773723 0.2714286 2.02096335 {3,5} => {4} 0.03357664 0.1965812 1.92368711 {4} => {3} 0.03357664 0.3285714 1.891357Running a summary on the rules_clust object indicates an average support of 0.05, and average confidence of 0.43.
This demonstrates that using clustering can be a viable way to develop association rules, and reduce resources and the number of dimensions at the same time:
support confidence liftMin. :0.02044 Min. :0.09985 Min. :0.9891st Qu.:0.02664 1st Qu.:0.19816 1st Qu.:1.006Median :0.03066 Median :0.27143 Median :1.526Mean :0.05040 Mean :0.43040 Mean :1.6083rd Qu.:0.04234 3rd Qu.:0.81954 3rd Qu.:1.891Max. :0.17080 Max. :1.00000 Max. :3.022- Daqing Chen, S. L. (2012). Data mining for the online retail industry: A case study of RFM model-based customer segmentation using data mining. Journal of Database Marketing and Customer Strategy Management, Vol. 19, No. 3.
- Michael Hahsler, K. H. (2006). Implications of probabilistic data modeling for mining association rules. In R. K. M. Spiliopoulou, Data and Information Analysis to Knowledge.
- Engineering, Studies in Classification, Data Analysis, and Knowledge Organization (pp. 598-605). Springer-Verlag.
In this chapter, we learned about a specific type of recommender engine, under the umbrella term market basket analysis.
We saw that market basket analysis enabled you to mine large quantities of transactions containing semi-structured data to derive association rules among the itemsets contained in each basket.
Some additional data cleaning techniques were used on the market basket data, in order to standardize and consolidate some of the descriptions of the purchased items. We also learned how to isolate the most powerful rules, using plotting techniques, along with metrics such as lift, support, and confidence.
Finally, we showed you how to generate clusters from your market basket data training data, and to predict cluster assignments based upon a test data set.