0.1 Introduction

in this project I am going to do optimization in placements of products in spare part shop. the right arrangement in placements of the product increases productivity and decreases space, cost of labor to collect the products and increases efficiency. I collected this data form the local spare part shop and collect some survey on their problems, according to their information, the customers go to the garage for maintenance per specific period of time (depends on the properties of the customer: some per 2 years and some per 3 years) to restore their cars. at this time they tends to change all the parts that shows any damage, so they buy a lot of parts.the aim of this paper is to study the behavior of the transactions and develop efficient placement for the products.

0.2 Data Reading and Deep Cleaning

0.2.1 Reading the Data from the csv file

setwd("C:\\Users\\asus\\Desktop\\Data Science\\Unsupervised Learning\\Unsupervised learning_R projects\\Association_sommerville")
store<-read.csv("Spare_part raw data.csv", header=TRUE, sep=";")

0.2.2 Cleaning the Data

since the data is raw and stored in one column separated by comma, I split the data and in to 16 columns (I take the average number of items for one purchase/ transaction which is 15)

# Split the data in the specified column by commas
colnames(store) <- "Transactions"
store$Transactions <- strsplit(as.character(store$Transactions), ",")
# Convert the list to a data frame with 16 columns
store_new <- as.data.frame(do.call(cbind, store$Transactions))
# Transpose the data frame
store_new <- t(store_new)
head(store_new)

since the data Gathered is surrounded by quotations, back slash and also has unnecessary data at the last row, it needs some additional cleaning. save the cleaned Data.

store_new <- store_new[, -1]
store_new <- gsub('"', '', store_new)
store_new <- gsub("\\\\", '', store_new)
store_cleaned <- store_new[-nrow(store_new), ]
write.csv(store_cleaned, file="store_Cleaned.csv")

0.3 Association

0.3.1 Loading the required package and Read the cleaned data as a transaction

library(arules)
library(arulesViz)
# remark: in this approach we read data as transactions
trans<-read.transactions("store_Cleaned.csv", format="basket", sep=",", skip=0)
trans
## transactions in sparse format with
##  151 transactions (rows) and
##  175 items (columns)

Determining the most sold product to place it in the convenient way so that the customers can easily access it.

library(arules)
library(arulesViz)
trans<-read.transactions("store_Cleaned.csv", format="basket", sep=",", skip=0)
item_freq <- itemFrequency(trans)
most_sold_item <- names(item_freq[item_freq == max(item_freq)])
print(paste("The most sold item is:", most_sold_item))
## [1] "The most sold item is: Brake Fluid"

0.3.2 Setting Rules

For the rule I select the support to be 10% that means the items sets that appear at least 10 % of the total transaction are going to be selected. confidence level 80% (Determine the probability of rhs item to be purchased if lhs item is purchased).

Additionally for the practicality of the rule I am going to take only the top 20 rules (arranged by confidence level in the decreasing order) to minimize the complexity of rules during product arrangement.

# Mine frequent item sets
itemsets <- eclat(trans, parameter = list(support = 0.8))
## Eclat
## 
## parameter specification:
##  tidLists support minlen maxlen            target  ext
##     FALSE     0.8      1     10 frequent itemsets TRUE
## 
## algorithmic control:
##  sparse sort verbose
##       7   -2    TRUE
## 
## Absolute minimum support count: 120 
## 
## create itemset ... 
## set transactions ...[175 item(s), 151 transaction(s)] done [0.00s].
## sorting and recoding items ... [11 item(s)] done [0.00s].
## creating bit matrix ... [11 row(s), 151 column(s)] done [0.00s].
## writing  ... [1057 set(s)] done [0.00s].
## Creating S4 object  ... done [0.00s].
# Generate association rules
rules <- ruleInduction(itemsets, confidence = 0.5)
# Sort rules by confidence and get the top 20
top_rules <- head(rules[order(quality(rules)$confidence, decreasing = TRUE)], 20)
# Print the resulting rules
inspect(top_rules)
##      lhs                       rhs                 support confidence     lift
## [1]  {Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8013245          1 1.006667
## [2]  {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8013245          1 1.013423
## [3]  {Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8013245          1 1.006667
## [4]  {Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8013245          1 1.013423
## [5]  {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Serpentine Belt,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8013245          1 1.006667
## [6]  {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Engine Oil,                                                             
##       Serpentine Belt,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8013245          1 1.013423
## [7]  {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Serpentine Belt,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8013245          1 1.006667
## [8]  {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Serpentine Belt,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8013245          1 1.013423
## [9]  {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Brake Fluid}     0.8013245          1 1.006667
## [10] {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Headlight Bulbs} 0.8013245          1 1.013423
## [11] {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8079470          1 1.006667
## [12] {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8079470          1 1.013423
## [13] {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Headlight Bulbs} 0.8013245          1 1.013423
## [14] {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Brake Fluid}     0.8013245          1 1.006667
## [15] {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Brake Fluid}     0.8079470          1 1.006667
## [16] {Brake Pads,                                                             
##       Engine Oil,                                                             
##       Tire Pressure Gauge,                                                    
##       Transmission Fluid}   => {Headlight Bulbs} 0.8079470          1 1.013423
## [17] {Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Serpentine Belt,                                                        
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Brake Fluid}     0.8013245          1 1.006667
## [18] {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Serpentine Belt,                                                        
##       Transmission Fluid,                                                     
##       Wiper Blades}         => {Headlight Bulbs} 0.8013245          1 1.013423
## [19] {Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Headlight Bulbs,                                                        
##       Serpentine Belt,                                                        
##       Transmission Fluid}   => {Brake Fluid}     0.8079470          1 1.006667
## [20] {Brake Fluid,                                                            
##       Brake Pads,                                                             
##       Cabin Air Filter,                                                       
##       Engine Oil,                                                             
##       Serpentine Belt,                                                        
##       Transmission Fluid}   => {Headlight Bulbs} 0.8079470          1 1.013423

0.3.3 Graphical Visualization

plot(top_rules, method="graph", engine="htmlwidget") 
ruleExplorer(top_rules)
Shiny applications not supported in static R Markdown documents

As we see from the shiny graph there are some products that are common during the maintenance (purchased with a lot of products) these are: - Fuel system cleaner,Engine Degreaser, car polish, transmission Fluid, spark plug, wiper blade.

so it is preferable to arrange this product at the first accessible aisle, so the customer go to other accessories easily. The customer who buys Engine Degreaser/car polish/ Fuel system cleaner are going to buy (spark plug, brake pad, transmission fluid, wiper blade and brake fluid), so depending on this association we can arrange placements of the product to increase the customer satisfaction and to decrease the trouble of finding the products in the store.

Descriptions from the shiny graph

Example :- - If we look at the Fuel system cleaner it has 5 rules connected to it 1. rule 11 {Fuel System Cleaner} => {Spark Plugs}

    support = 0.205 this transaction occurs at 20.5% of the overall transaction
    confidence = 1, If the customer buys Fuel System Cleaner, it is 100% sure that he will                         buy the spark plug
    coverage = 0.205, this rule occurs in the 20.5 % of overall transaction
    lift = 1.09  Indicates that these two products are more likely to occur together than                           expected by chance.
   count = 31, the number of times this rule occurs

we can see all the rules(the remaining 4) for this product and also for other products.