How do the characteristics of different groups of customers differ from one another based on their spending patterns across various product categories?
Reason for using clustering method
Clustering is the most appropriate method to answer this research question since it allows for the segmentation of customers into distinct groups based on the similarities of their spending behaviors across multiple products like milk, grocery and fresh products among others. By using the clustering method, this study will be able to objectively group customers who show similar purchasing patterns into clusters regardless of region and channel. This will help us uncover underlying patterns in the customer’s behavior.
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(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(gridExtra)
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
mydata<-read.csv("Wholesale customers data.csv")
str(mydata)
## 'data.frame': 440 obs. of 8 variables:
## $ Channel : int 2 2 2 1 2 2 2 2 1 2 ...
## $ Region : int 3 3 3 3 3 3 3 3 3 3 ...
## $ Fresh : int 12669 7057 6353 13265 22615 9413 12126 7579 5963 6006 ...
## $ Milk : int 9656 9810 8808 1196 5410 8259 3199 4956 3648 11093 ...
## $ Grocery : int 7561 9568 7684 4221 7198 5126 6975 9426 6192 18881 ...
## $ Frozen : int 214 1762 2405 6404 3915 666 480 1669 425 1159 ...
## $ Detergents_Paper: int 2674 3293 3516 507 1777 1795 3140 3321 1716 7425 ...
## $ Delicassen : int 1338 1776 7844 1788 5185 1451 545 2566 750 2098 ...
head(mydata)
## Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen
## 1 2 3 12669 9656 7561 214 2674 1338
## 2 2 3 7057 9810 9568 1762 3293 1776
## 3 2 3 6353 8808 7684 2405 3516 7844
## 4 1 3 13265 1196 4221 6404 507 1788
## 5 2 3 22615 5410 7198 3915 1777 5185
## 6 2 3 9413 8259 5126 666 1795 1451
The Wholesale customers Data Set was used in this study. This dataset had 8 variables and 440 observations. The variables present in the data were:
Channel (Hotel/Restaurant/Cafe or Retail channel), Regions (Lisnon, Oporto or Other), Fresh (annual spending (m.u.) on fresh products), Milk (annual spending (m.u.) on milk products), Grocery (annual spending (m.u.) on grocery products), Frozen (annual spending (m.u.) on frozen products), Detergents_Paper (annual spending (m.u.) on detergents and paper products), Delicassen (annual spending (m.u.) on and delicatessen products). The variable Channel and Regions were the only nominal variables in the data while the other variables were numeric.
The Wholesale customers Data Set was obtained from the Kaggle website using the link: https://www.kaggle.com/datasets/binovi/wholesale-customers-data-set.
mydata$Channel<-as.factor(mydata$Channel)
mydata$Region<-as.factor(mydata$Region)
summary(mydata)
## Channel Region Fresh Milk Grocery
## 1:298 1: 77 Min. : 3 Min. : 55 Min. : 3
## 2:142 2: 47 1st Qu.: 3128 1st Qu.: 1533 1st Qu.: 2153
## 3:316 Median : 8504 Median : 3627 Median : 4756
## Mean : 12000 Mean : 5796 Mean : 7951
## 3rd Qu.: 16934 3rd Qu.: 7190 3rd Qu.:10656
## Max. :112151 Max. :73498 Max. :92780
## Frozen Detergents_Paper Delicassen
## Min. : 25.0 Min. : 3.0 Min. : 3.0
## 1st Qu.: 742.2 1st Qu.: 256.8 1st Qu.: 408.2
## Median : 1526.0 Median : 816.5 Median : 965.5
## Mean : 3071.9 Mean : 2881.5 Mean : 1524.9
## 3rd Qu.: 3554.2 3rd Qu.: 3922.0 3rd Qu.: 1820.2
## Max. :60869.0 Max. :40827.0 Max. :47943.0
Based on the results from the summary table above, 298 customers purchased their products from Hotel/Restaurant/Cafes while 142 purchased their products from Retail channel. There were 77 customers who came from Lisnon, 47 from Oporto and 316 from Other regions. The average annual spending on fresh products was 12,000 with very few customers having an average spending of 3 m.u. The average spending was higher on fresh products and lower on Delicassen products.
sum(is.na(mydata))
## [1] 0
There were no missing values in the data.
This study standardized the numeric variables. This was important as the K-means clustering algorithm is sensitive to scale of the variables and to also make variables comparable.
# Select only the continuous variables to be standardized
continuous_vars <- select(mydata, -c(Channel, Region))
# Standardize the continuous variables
standardized_vars <- scale(continuous_vars)
# Create a new data frame with standardized variables and non-standardized variables
standardized_data <- cbind(standardized_vars, select(mydata, Channel, Region))
# View the first few rows of the standardized data
head(standardized_data)
## Fresh Milk Grocery Frozen Detergents_Paper Delicassen
## 1 0.05287300 0.52297247 -0.04106815 -0.5886970 -0.04351919 -0.06626363
## 2 -0.39085706 0.54383861 0.17012470 -0.2698290 0.08630859 0.08904969
## 3 -0.44652098 0.40807319 -0.02812509 -0.1373793 0.13308016 2.24074190
## 4 0.09999758 -0.62331041 -0.39253008 0.6863630 -0.49802132 0.09330484
## 5 0.83928412 -0.05233688 -0.07926595 0.1736612 -0.23165413 1.29786952
## 6 -0.20457266 0.33368675 -0.29729863 -0.4955909 -0.22787885 -0.02619421
## Channel Region
## 1 2 3
## 2 2 3
## 3 2 3
## 4 1 3
## 5 2 3
## 6 2 3
# Create box plots for each continuous variable
boxplot_plots <- lapply(mydata[, -c(1, 2)], function(x) {
ggplot(mydata, aes_string(y = x)) +
geom_boxplot() +
labs(title = paste("Boxplot of", x))
})
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Arrange the box plots in a grid
grid.arrange(grobs = boxplot_plots, ncol = 2)
Based on the results from the figures above, there was presence of outliers in the data.This is because there were data points that were outside the upper and lower whisker plots. This study will normalize the standardized data to reduce the impact of outliers on the results.
Given that the data was not normally distributed and having outliers, this study normalized the data as shown below:
z <- standardized_vars
means <- apply(z,2,mean)
sds <- apply(z,2,sd)
nor <- scale(z,center=means,scale=sds)
fviz_dist(get_dist(nor,method="euclidian"))
get_clust_tendency(standardized_vars,
n = nrow(standardized_vars) - 1,
graph = FALSE)
## $hopkins_stat
## [1] 0.9362977
##
## $plot
## NULL
This study used the Hopkins statistic to measure the cluster tendency of the dataset used. The Hopkins value of 0.94 is close to 1 which indicates that the data has a high tendency to cluster.
distance = dist(nor)
mydata.hclust = hclust(distance)
plot(mydata.hclust,hang=-1, labels=mydata$Region,main='Cluster Dendogram')
wss <- (nrow(nor)-1)*sum(apply(nor,2,var))
for (i in 2:20) wss[i] <- sum(kmeans(nor, centers=i)$withinss)
plot(1:20, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")
fviz_nbclust(nor, kmeans, method = "silhouette")
# compute gap statistic
set.seed(123)
library(cluster)
gap_stat <- clusGap(nor, FUN = kmeans, nstart = 25,
K.max = 10, B = 50)
## Warning: did not converge in 10 iterations
fviz_gap_stat(gap_stat)
The results from the scree plot using elbow method, Gap statistics method and Average Silhouette Method showed that 3 clusters would be the most appropriate number for this analysis.
# Compute k-means clustering with k = 4
set.seed(123)
final <- kmeans(nor, 3, nstart = 25)
print(final)
## K-means clustering with 3 clusters of sizes 44, 3, 393
##
## Cluster means:
## Fresh Milk Grocery Frozen Detergents_Paper Delicassen
## 1 -0.306069120 1.8098555 2.2038591 -0.24975466 2.2309315 0.25139852
## 2 3.840444845 3.2957757 0.9852919 7.20489292 -0.1527927 6.79967230
## 3 0.004950908 -0.2277887 -0.2542638 -0.02703683 -0.2486071 -0.08005229
##
## Clustering vector:
## [1] 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 1 3 3 3 3 3 3 3 3
## [38] 3 1 3 3 3 3 1 3 1 1 1 3 1 3 3 3 3 3 3 1 3 3 3 3 1 3 3 3 1 3 3 3 3 3 3 3 3
## [75] 3 3 3 1 3 3 3 3 3 3 3 1 1 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3
## [112] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3
## [149] 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 1 3 1 3 3 3 3 3 1 3 1 3 3 3 3 3 3 3 2 3 2 3
## [186] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 3 3 3 1 3 3 3 1 3 1 3 3 3 3 1 3 3 3 3 3
## [223] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3
## [260] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [297] 3 3 3 3 3 1 3 3 1 3 1 3 3 1 3 3 1 3 3 3 3 3 3 1 3 3 3 3 3 2 3 3 3 3 3 1 3
## [334] 1 3 3 3 3 3 3 3 3 3 1 3 3 3 3 3 1 3 1 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [371] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [408] 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3
##
## Within cluster sum of squares by cluster:
## [1] 441.0021 214.5396 944.8291
## (between_SS / total_SS = 39.2 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
The results above above shows the cluster mean which represents the average values of each of the variables that are within each cluster.
fviz_cluster(final, data = nor)
The results in the figure above shows the distinct nature of the three clusters that were generated by K-means clustering model.
This study checked whether there was any relationship between the region and the clusters and the channel and the clusters. Chi-square test was the most appropriate method since both variables were non-numeric.
c<-prop.table(table(final$cluster))
c
##
## 1 2 3
## 0.100000000 0.006818182 0.893181818
The results shows that 10% of the customers were classified under cluster 1, 0.68% under cluster 2 and 89.32% under cluster 3.
df1<-data.frame(nor, mydata$Channel, mydata$Region, final$cluster)
a<-table(df1$mydata.Region, df1$final.cluster)
a
##
## 1 2 3
## 1 7 0 70
## 2 8 1 38
## 3 29 2 285
chisq.test(a)
## Warning in chisq.test(a): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: a
## X-squared = 4.9976, df = 4, p-value = 0.2875
The results from the Chi-square above shows that there is no significance difference in the cluster of customers based on the region that they came from, X^2(4) = 5.0, p > 0.05. The study rejects the hypothesis that customer clusters and regions are related.
b<-table(df1$mydata.Channel, df1$final.cluster)
b
##
## 1 2 3
## 1 0 3 295
## 2 44 0 98
chisq.test(b)
## Warning in chisq.test(b): Chi-squared approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: b
## X-squared = 103.44, df = 2, p-value < 2.2e-16
The results from the Chi-square above shows that there is a significance difference in the cluster of customers based on the shopping place (channel), X^2(2) = 103.44, p < 0.05. The study fails to reject the hypothesis that customer clusters and the place that they shop are related.
This study classified 440 wholesale customers based on 6 normalized and standardized variables on spending on different products: Fresh, Milk, Grocery, Frozen, Detergents_Paper, and Delicassen.
This study used three different methods to decide the number of optimal clusters that can be extracted from the data. This study decided to go with 3 clusters as the scree plot using the elbow method, Gap statistics method, and Average Silhouette Method showed that 3 clusters would be the most appropriate number. After fitting the clusters, associations were drawn between the cluster and region and cluster and channel.
From the study, most of the wholesale customers were grouped under cluster 3 (89.32%). Cluster 3 which comprised a majority of the customers had an average spending across all the products when compared to the other two categories. There was none of the spending on any product that was higher or lower in this cluster. Cluster 1 comprised people who had a relatively lower expenditure on frozen and fresh products compared to milk, grocery, Detergents_Paper, and Delicassen compared to other clusters. Cluster 2 comprised customers who spend much on Fresh, Milk, Frozen, and Delicassen products compared to the other clusters. Their spending on Detergents_Paper is notably lower in this cluster compared to Cluster 1.
There was not enough evidence in the data to show that customers from a particular region spend differently on products when compared to customers from other regions. There was a deviation in the place that people shopped based on the cluster. All the customers under cluster 1 shopped in the Retail channel, all the customers under cluster 2 shopped in Hotel/Restaurant/Café while a majority of the customers in cluster 3 shopped in Hotel/Restaurant/Café.