Ever noticed how a shop keeps bread next to butter, or tea beside sugar? That is not luck. It comes from Association Analysis (also called Market Basket Analysis), a data-mining technique that finds items which are frequently bought together. In this post we will learn the idea, the key measures, and how to do it step-by-step in R using the arules package on a real-style supermarket dataset.
1. What is Association Analysis?
Association analysis discovers rules of the form {X} ⇒ {Y}, read as "customers who buy X also tend to buy Y". Here X is called the antecedent (LHS) and Y the consequent (RHS).
Example rule: {Bread, Milk} ⇒ {Butter}
Common uses: product placement in stores, "frequently bought together" on e-commerce sites, cross-selling, medical symptom analysis, and web-page click patterns.
2. Key Terms
Support
How often an itemset appears in all transactions.
Confidence
How often Y is bought when X is bought — the reliability of the rule.
Lift
How much more likely Y is bought with X than on its own.
Apriori Algorithm
The Apriori algorithm finds frequent itemsets using one simple principle: if an itemset is frequent, all its subsets must also be frequent. So any itemset containing an infrequent item is pruned early, which makes the search fast.
3. The Dataset
We use a small supermarket dataset of 40 transactions and 10 items (Bread, Milk, Butter, Eggs, Cheese, Jam, Tea, Sugar, Biscuits, Coffee). It is stored in long (single) format — one row per item bought:
| TransactionID | Item |
|---|---|
| 1 | Bread |
| 1 | Milk |
| 1 | Butter |
| 2 | Bread |
| 2 | Butter |
| … | … |
Save the file in your R working directory (check it with getwd()).
4. Step-by-Step in R
Step 1: Install and load packages
Step 2: Read the data as transactions
format = "single" tells R each row holds one item, and cols = c(1, 2) means column 1 is the transaction ID and column 2 is the item.
Step 3: Explore the data
The plot shows Bread and Milk are the most purchased items.
Step 4: Generate rules with Apriori
supp or conf. If you get too many, raise them.Step 5: Inspect the strongest rules
Expect rules such as {Tea} ⇒ {Sugar}, {Coffee} ⇒ {Sugar} and {Butter} ⇒ {Bread} near the top, each with lift above 1.
Step 6: Remove redundant rules
A rule is redundant if a simpler rule (fewer items on the left) has the same or higher confidence.
Step 7: Rules for a specific item
Step 8: Visualise the rules
Step 9: Save the rules
5. Interpreting the Results
For a rule like {Tea} ⇒ {Sugar}:
- Support tells you what share of all baskets had both Tea and Sugar.
- Confidence tells you, out of the baskets with Tea, what share also had Sugar.
- Lift above 1 means Tea buyers pick up Sugar more often than an average shopper does.
Business action: place Tea, Sugar and Biscuits close together, or offer a combo discount.
6. Practice Exercises
- Change support to 0.05 and confidence to 0.5. How many rules do you get now?
- Find all rules where Milk is on the right-hand side.
- Which rule has the highest lift? Explain what it means in plain words.
- Use
eclat()to find frequent itemsets and compare them with Apriori's results.
7. Summary
Association analysis finds "bought-together" patterns using support, confidence and lift. In R, the workflow is: read.transactions → explore → apriori → sort/prune → visualise → save. Type the code yourself — that is the best way to learn it!