Association Analysis in R (Market Basket Analysis)

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.

Support(X) = (Transactions containing X) / (Total transactions)

Confidence

How often Y is bought when X is bought — the reliability of the rule.

Confidence(X ⇒ Y) = Support(X ∪ Y) / Support(X)

Lift

How much more likely Y is bought with X than on its own.

Lift(X ⇒ Y) = Confidence(X ⇒ Y) / Support(Y)
Reading Lift: Lift > 1 → positive association (bought together)  |  Lift = 1 → independent  |  Lift < 1 → negative association (rarely together).

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:

TransactionIDItem
1Bread
1Milk
1Butter
2Bread
2Butter
……

Save the file in your R working directory (check it with getwd()).

4. Step-by-Step in R

Step 1: Install and load packages

# Run install only once install.packages(c("arules", "arulesViz")) library(arules) # Apriori and rule mining library(arulesViz) # Visualising rules

Step 2: Read the data as transactions

trans <- read.transactions("market_basket.csv", format = "single", sep = ",", header = TRUE, cols = c(1, 2)) trans # 40 transactions, 10 items

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

summary(trans) # density, most frequent items, basket sizes inspect(trans[1:5]) # first 5 baskets itemFrequency(trans) # support of every item itemFrequencyPlot(trans, topN = 10, type = "absolute", col = "steelblue", main = "Top 10 Items Purchased")

The plot shows Bread and Milk are the most purchased items.

Step 4: Generate rules with Apriori

rules <- apriori(trans, parameter = list(supp = 0.1, # in at least 10% of baskets conf = 0.6, # correct at least 60% of the time minlen = 2)) # at least 2 items per rule summary(rules) length(rules) # number of rules found
Tip: If you get zero rules, lower supp or conf. If you get too many, raise them.

Step 5: Inspect the strongest rules

rules_lift <- sort(rules, by = "lift", decreasing = TRUE) inspect(head(rules_lift, 10)) # Same rules sorted by confidence inspect(head(sort(rules, by = "confidence"), 10))

Expect rules such as {Tea} ⇒ {Sugar}, {Coffee} ⇒ {Sugar} and {Butter} ⇒ {Bread} near the top, each with lift above 1.

Step 6: Remove redundant rules

rules_clean <- rules[!is.redundant(rules)] length(rules_clean) inspect(sort(rules_clean, by = "lift"))

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

# What leads customers to buy Butter? butter_rules <- subset(rules_clean, rhs %in% "Butter") inspect(butter_rules) # What else do Tea buyers purchase? tea_rules <- subset(rules_clean, lhs %in% "Tea") inspect(tea_rules)

Step 8: Visualise the rules

plot(rules_clean) # scatter: support vs confidence, shaded by lift plot(rules_clean, method = "graph") # network of items and rules plot(rules_clean, method = "grouped") # grouped matrix plot(head(rules_lift, 10), method = "paracoord") # parallel coordinates

Step 9: Save the rules

rules_df <- as(rules_clean, "data.frame") head(rules_df) write.csv(rules_df, "association_rules.csv", row.names = FALSE)

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

  1. Change support to 0.05 and confidence to 0.5. How many rules do you get now?
  2. Find all rules where Milk is on the right-hand side.
  3. Which rule has the highest lift? Explain what it means in plain words.
  4. 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!