install.packages(c("dplyr", "ggplot2", "arules", "arulesViz"),
repos = "https://cloud.r-project.org")
## Installing packages into 'C:/Users/Owner/AppData/Local/R/win-library/4.6'
## (as 'lib' is unspecified)
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.6\00LOCK\dplyr\libs\x64\dplyr.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.6\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
## package 'ggplot2' successfully unpacked and MD5 sums checked
## package 'arules' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'arules'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.6\00LOCK\arules\libs\x64\arules.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.6\arules\libs\x64\arules.dll:
## Permission denied
## Warning: restored 'arules'
## package 'arulesViz' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Owner\AppData\Local\Temp\Rtmp6vdy0p\downloaded_packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(arules)
## Loading required package: Matrix
##
## Attaching package: 'arules'
## The following object is masked from 'package:dplyr':
##
## recode
## The following objects are masked from 'package:base':
##
## abbreviate, write
library(arulesViz)
# Upload Data set
retail <- read.csv("C:/Users/Owner/Desktop/Retail.csv")
# Display First Five Rows
head(retail, n = 5)
## TransactionID CustomerID Date ItemCategory PaymentMethod StoreLocation
## 1 1000 C055 1/29/2024 Electronics Debit Card Mall
## 2 1001 C079 4/18/2024 Home Goods Cash Downtown
## 3 1002 C055 4/22/2024 Grocery Credit Card Downtown
## 4 1003 C012 4/10/2024 Electronics Cash Mall
## 5 1004 C017 1/9/2024 Grocery Credit Card Airport
## ItemSubcategory Amount
## 1 Laptop 379.14
## 2 Bedding 160.29
## 3 Beverages 28.55
## 4 Smartphone 166.59
## 5 Dairy Products 22.63
# Describe The Data
# How is the data set structured?
## The data set is a structured data set that stores both quantitative and categorical data. Additionally, the data would be considered tabular data as each data entry is separate from the others.
# What variables relate to customer demographics, purchase behavior, and product categories?
## The variables that relate to customer demographics would be their Transaction Id and Customer ID. The variables that would relate to purchase behavior could be PaymentMethod, StoreLocation, and Date. Variables that would relate to product categories would be ItemCategory, ItemSubcategory, and Amount.
# Segment Data Set By Payment Method
unique(retail$PaymentMethod)
## [1] "Debit Card" "Cash" "Credit Card" "Mobile Payment"
cash <- subset(retail, PaymentMethod == "Cash")
credit_card <- subset(retail, PaymentMethod == "Credit Card")
debit_card <- subset(retail, PaymentMethod == "Debit Card")
mobile_payment <- subset(retail, PaymentMethod== "Mobile Payment")
## The most commonly used payment type is credit card (137), then mobile payments (122), then cash (128), and finally debit card (113). The most expensive payment was made on a debit card, which was a payment of 499 dollars. Across the board it looks like people used each payment type to purchase each type of item within ItemCategory at each location listed under StoreLocation. There is a slight variance between payment methods for the two before mentioned variables, but nothing that is obviously significant.
# Stacked Bar Chart (Item Category Frequency and Payment Methods)
library(ggplot2)
ggplot(retail, aes(x = ItemCategory, fill = PaymentMethod)) +
geom_bar(position = "dodge") +
labs(
title = "Item Category Frequency By Payment Method",
x = "Item Category",
y = "Frequency",
fill = "Payment Method",
) +
theme_minimal()

# Distribution Of Purchase Amounts Using Histogram/Boxplot
hist(retail$Amount,
main = "Distribution Of Purchase Amount",
xlab = "Cost of Item",
ylab = "Frequency",
col = "lightblue",
border = "black" ,
breaks = 10)

# Visual patterns help support store management in terms of both inventory and marketing decisions because it makes it very easy to conceptualize the data. With visual patterns you can clearly see which items have had an increase in sales and which have had a decrease in sales.
# Use the apriori algorithm to discover association rules among purchased items. Report the top 5 rules with their support, confidence, and lift.
rules <- apriori(retail,
parameter = list(
supp = 0.01,
conf = 0.5,
minlen = 2))
## Warning: Column(s) 1, 2, 3, 4, 5, 6, 7, 8 not logical or factor. Applying
## default discretization (see '? discretizeDF').
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.5 0.1 1 none FALSE TRUE 5 0.01 2
## 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: 5
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[269 item(s), 500 transaction(s)] done [0.00s].
## sorting and recoding items ... [156 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 done [0.04s].
## writing ... [1024 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
top5 <- head(sort(rules, by = "lift", decreasing = TRUE), 5)
inspect(top5)
## lhs rhs support confidence coverage lift count
## [1] {ItemCategory=Home Goods,
## PaymentMethod=Cash,
## Amount=[34.3,91.4)} => {ItemSubcategory=Bedding} 0.010 1.0000000 0.010 29.41176 5
## [2] {TransactionID=[1.33e+03,1.5e+03],
## ItemCategory=Books,
## PaymentMethod=Mobile Payment,
## Amount=[5.86,34.3)} => {ItemSubcategory=Fiction} 0.012 0.8571429 0.014 19.48052 6
## [3] {TransactionID=[1.33e+03,1.5e+03],
## ItemCategory=Books,
## PaymentMethod=Mobile Payment} => {ItemSubcategory=Fiction} 0.014 0.7777778 0.018 17.67677 7
## [4] {ItemCategory=Home Goods,
## StoreLocation=Airport,
## Amount=[34.3,91.4)} => {ItemSubcategory=Bedding} 0.012 0.6000000 0.020 17.64706 6
## [5] {TransactionID=[1.17e+03,1.33e+03),
## ItemCategory=Home Goods,
## StoreLocation=Airport} => {ItemSubcategory=Bedding} 0.012 0.6000000 0.020 17.64706 6
quality(top5)
## support confidence coverage lift count
## 621 0.010 1.0000000 0.010 29.41176 5
## 1024 0.012 0.8571429 0.014 19.48052 6
## 734 0.014 0.7777778 0.018 17.67677 7
## 625 0.012 0.6000000 0.020 17.64706 6
## 629 0.012 0.6000000 0.020 17.64706 6
## Reflect on how such association patterns can help improve cross-selling strategies in retail
### Association patterns are a key part in improving cross-selling strategies in retail because they allow us to test the strength of the association between two products. Knowing this information allows us to make stronger predictions about our buyers and turn a greater profit. For example if customers continue to buy tortilla chips with salsa we can organize the store to sell chips near the salsa, sell them as a bundle, or recommend one when a customer purchases the other.