###Market Basket Analysis
##The association analysis offers great insights into customer purchases and helps track those natural bundles of items purchased together.
##The association rules it has generated clearly identify strong relationships between items in a menu item based on metrics such as support, confidence and lift.
library(arules)
## Warning: package 'arules' was built under R version 4.1.3
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 4.1.3
##
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
##
## abbreviate, write
library(arulesViz)
df <- read.csv("D:/Download D/combined_item_sales-reformatdate.xlsx - Sheet1.csv")
head(df)
## Date Name Category
## 1 1/11/2023 7:54 Curry Chicken Potato with Turmeric Rice Rice Set
## 2 1/11/2023 7:54 Shredded Chicken Hor Fun Noodles
## 3 1/11/2023 8:02 Nyonya Kuih (3pcs) Local Delicacies
## 4 1/11/2023 8:06 Hot Hainanese Coffee Hot Drinks
## 5 1/11/2023 8:06 Hainanese Butter & Kaya Toast Breakfast Series
## 6 1/11/2023 8:06 Hainanese Butter & Kaya Toast Breakfast Series
## Price Qty Total Net Device Location Order.No
## 1 13.9 1 13.9 13.9 H Restaurant Puchong iPad Puchong 231101X98H
## 2 12.9 1 12.9 12.9 H Restaurant Puchong iPad Puchong 231101X98H
## 3 6.9 3 20.7 20.7 H Restaurant Puchong Pastry iPad Puchong 231101XSGR
## 4 4.5 1 4.5 4.5 H Restaurant Puchong Pastry iPad Puchong 231101XSGR
## 5 3.5 2 7.0 7.0 H Restaurant Puchong Pastry iPad Puchong 231101XSGR
## 6 3.5 1 3.5 3.5 H Restaurant Puchong Pastry iPad Puchong 231101XSGR
#Change the dataset to transactional dataset format
transactions <- split(df$Name, df$Order.No)
transactions <- lapply(transactions, function(x) unique(x))
transactions_with_int_ids <- setNames(transactions, as.character(1:length(transactions)))
trans <- as(transactions_with_int_ids, "transactions")
inspect(trans[1:5])
## items transactionID
## [1] {Ice Hainanese Coffee,
## Kampung Style Fried Meehoon} 1
## [2] {Brown Rice,
## Claypot Lou Shu Fun,
## Fried Egg,
## GonLou Dry Noodles with BBQ Chicken,
## Hainan Roasted & White-Cut Chicken Rice,
## Hainanese Butter & Kaya Toast,
## Luncheon VMeat,
## Mango Feast,
## O Hot Hainanese Coffee O,
## Signature Nasi Lemak with Rendang Chicken} 2
## [3] {Cold Soymilk,
## Loh Mee,
## Passion Fruit Juice,
## Signature Hainan Roasted Chicken Rice} 3
## [4] {Black sesame Bao,
## Ginger Mushroom Bao} 4
## [5] {Hot Hainanese Coffee,
## Shredded Chicken Hor Fun,
## Signature VChar Siew Rice} 5
frequentItems <- eclat (trans, parameter = list(supp = 0.07, maxlen = 15))
## Eclat
##
## parameter specification:
## tidLists support minlen maxlen target ext
## FALSE 0.07 1 15 frequent itemsets TRUE
##
## algorithmic control:
## sparse sort verbose
## 7 -2 TRUE
##
## Absolute minimum support count: 2108
##
## create itemset ...
## set transactions ...[302 item(s), 30116 transaction(s)] done [0.01s].
## sorting and recoding items ... [10 item(s)] done [0.00s].
## creating sparse bit matrix ... [10 row(s), 30116 column(s)] done [0.00s].
## writing ... [10 set(s)] done [0.00s].
## Creating S4 object ... done [0.00s].
#Item frequency plot
itemFrequencyPlot(trans, topN=10, type="absolute", main="Item Frequency")

#Hainanese Butter & Kaya Toast, which is a Malaysian authentic favourite breakfast shows the top 1 sale in the restaurant, this may be caused by the affordable price and light meal to be breakfast for all customers and a side for lunch.
#The sales followed by lunch main dish such as nasi lemak with rendang chicken and curry mee hoon & noodles. This indicates that these two foods are the favourite lunch option in the restaurant.
#Beverage such as hot Hainanese coffee and iced Hainanese coffee are the top beverages in the restaurant as it is also an authentic beverage that is suitable for breakfast and lunch.
#Association rules with minimum support 0.01 and minimum lift 0.5
frequent_itemsets <- apriori(trans, parameter = list(support = 0.01, confidence = 0.5))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.5 0.1 1 none FALSE TRUE 5 0.01 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 301
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[302 item(s), 30116 transaction(s)] done [0.00s].
## sorting and recoding items ... [87 item(s)] done [0.01s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.02s].
## writing ... [7 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
inspect(frequent_itemsets)
## lhs rhs support confidence coverage lift count
## [1] {Hot Milo} => {Hainanese Butter & Kaya Toast} 0.01215301 0.5069252 0.02397397 2.211904 366
## [2] {Half-boiled Kampung Egg (2pcs)} => {Hainanese Butter & Kaya Toast} 0.06999602 0.6374357 0.10980874 2.781370 2108
## [3] {Half-boiled Kampung Egg (2pcs),
## Pangkor Shell Curry Puff} => {Hainanese Butter & Kaya Toast} 0.01006110 0.6433121 0.01563953 2.807011 303
## [4] {Half-boiled Kampung Egg (2pcs),
## Ice Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.01646965 0.6657718 0.02473768 2.905011 496
## [5] {Half-boiled Kampung Egg (2pcs),
## Hot Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.02128437 0.7226607 0.02945278 3.153238 641
## [6] {Half-boiled Kampung Egg (2pcs),
## Special Curry Mee Hoon & Noodles} => {Hainanese Butter & Kaya Toast} 0.01361403 0.6602254 0.02062027 2.880810 410
## [7] {Half-boiled Kampung Egg (2pcs),
## Signature Nasi Lemak with Rendang Chicken} => {Hainanese Butter & Kaya Toast} 0.01703413 0.6364764 0.02676318 2.777184 513
# View the association rules sorting by confidence, lift and support
arules::inspect(frequent_itemsets)
## lhs rhs support confidence coverage lift count
## [1] {Hot Milo} => {Hainanese Butter & Kaya Toast} 0.01215301 0.5069252 0.02397397 2.211904 366
## [2] {Half-boiled Kampung Egg (2pcs)} => {Hainanese Butter & Kaya Toast} 0.06999602 0.6374357 0.10980874 2.781370 2108
## [3] {Half-boiled Kampung Egg (2pcs),
## Pangkor Shell Curry Puff} => {Hainanese Butter & Kaya Toast} 0.01006110 0.6433121 0.01563953 2.807011 303
## [4] {Half-boiled Kampung Egg (2pcs),
## Ice Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.01646965 0.6657718 0.02473768 2.905011 496
## [5] {Half-boiled Kampung Egg (2pcs),
## Hot Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.02128437 0.7226607 0.02945278 3.153238 641
## [6] {Half-boiled Kampung Egg (2pcs),
## Special Curry Mee Hoon & Noodles} => {Hainanese Butter & Kaya Toast} 0.01361403 0.6602254 0.02062027 2.880810 410
## [7] {Half-boiled Kampung Egg (2pcs),
## Signature Nasi Lemak with Rendang Chicken} => {Hainanese Butter & Kaya Toast} 0.01703413 0.6364764 0.02676318 2.777184 513
rules_conf <- sort(frequent_itemsets, by="confidence", decreasing=TRUE)
inspect(head(rules_conf))
## lhs rhs support confidence coverage lift count
## [1] {Half-boiled Kampung Egg (2pcs),
## Hot Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.02128437 0.7226607 0.02945278 3.153238 641
## [2] {Half-boiled Kampung Egg (2pcs),
## Ice Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.01646965 0.6657718 0.02473768 2.905011 496
## [3] {Half-boiled Kampung Egg (2pcs),
## Special Curry Mee Hoon & Noodles} => {Hainanese Butter & Kaya Toast} 0.01361403 0.6602254 0.02062027 2.880810 410
## [4] {Half-boiled Kampung Egg (2pcs),
## Pangkor Shell Curry Puff} => {Hainanese Butter & Kaya Toast} 0.01006110 0.6433121 0.01563953 2.807011 303
## [5] {Half-boiled Kampung Egg (2pcs)} => {Hainanese Butter & Kaya Toast} 0.06999602 0.6374357 0.10980874 2.781370 2108
## [6] {Half-boiled Kampung Egg (2pcs),
## Signature Nasi Lemak with Rendang Chicken} => {Hainanese Butter & Kaya Toast} 0.01703413 0.6364764 0.02676318 2.777184 513
rules_lift <- sort(frequent_itemsets, by="lift", decreasing=TRUE)
inspect(head(rules_lift))
## lhs rhs support confidence coverage lift count
## [1] {Half-boiled Kampung Egg (2pcs),
## Hot Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.02128437 0.7226607 0.02945278 3.153238 641
## [2] {Half-boiled Kampung Egg (2pcs),
## Ice Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.01646965 0.6657718 0.02473768 2.905011 496
## [3] {Half-boiled Kampung Egg (2pcs),
## Special Curry Mee Hoon & Noodles} => {Hainanese Butter & Kaya Toast} 0.01361403 0.6602254 0.02062027 2.880810 410
## [4] {Half-boiled Kampung Egg (2pcs),
## Pangkor Shell Curry Puff} => {Hainanese Butter & Kaya Toast} 0.01006110 0.6433121 0.01563953 2.807011 303
## [5] {Half-boiled Kampung Egg (2pcs)} => {Hainanese Butter & Kaya Toast} 0.06999602 0.6374357 0.10980874 2.781370 2108
## [6] {Half-boiled Kampung Egg (2pcs),
## Signature Nasi Lemak with Rendang Chicken} => {Hainanese Butter & Kaya Toast} 0.01703413 0.6364764 0.02676318 2.777184 513
rules_support <- sort(frequent_itemsets, by="support", decreasing=TRUE)
inspect(head(rules_support))
## lhs rhs support confidence coverage lift count
## [1] {Half-boiled Kampung Egg (2pcs)} => {Hainanese Butter & Kaya Toast} 0.06999602 0.6374357 0.10980874 2.781370 2108
## [2] {Half-boiled Kampung Egg (2pcs),
## Hot Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.02128437 0.7226607 0.02945278 3.153238 641
## [3] {Half-boiled Kampung Egg (2pcs),
## Signature Nasi Lemak with Rendang Chicken} => {Hainanese Butter & Kaya Toast} 0.01703413 0.6364764 0.02676318 2.777184 513
## [4] {Half-boiled Kampung Egg (2pcs),
## Ice Hainanese Coffee} => {Hainanese Butter & Kaya Toast} 0.01646965 0.6657718 0.02473768 2.905011 496
## [5] {Half-boiled Kampung Egg (2pcs),
## Special Curry Mee Hoon & Noodles} => {Hainanese Butter & Kaya Toast} 0.01361403 0.6602254 0.02062027 2.880810 410
## [6] {Hot Milo} => {Hainanese Butter & Kaya Toast} 0.01215301 0.5069252 0.02397397 2.211904 366