I found data base “Wholesale customers data.csv” on the website www.kaggle.com. Link of the data set: https://www.kaggle.com/datasets/binovi/wholesale-customers-data-set . In this data base there is data about 440 observations.

The data set refers to customers of a wholesale distributor. It includes the annual spending in monetary units on diverse product categories.

The research question: Can we cluster customers of a wholesale distributor based on their annual spending patterns in monetary units to identify homogeneous groups of customers with similar purchasing behaviors and preferences?

Since the research questions is whether we can group customers, the best method is using clustering.

mydata <- read.csv("Wholesale customers data.csv")
# Create a new column 'ID' with numbering starting from 1
mydata$ID <- seq_len(nrow(mydata))

First I added one more column to the dataset representing ID. Unit is one customer and sample size is 440 customers.

head(mydata)
##   Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen ID
## 1       2      3 12669 9656    7561    214             2674       1338  1
## 2       2      3  7057 9810    9568   1762             3293       1776  2
## 3       2      3  6353 8808    7684   2405             3516       7844  3
## 4       1      3 13265 1196    4221   6404              507       1788  4
## 5       2      3 22615 5410    7198   3915             1777       5185  5
## 6       2      3  9413 8259    5126    666             1795       1451  6

Description:

describe(mydata[,-9])
## mydata[, -9] 
## 
##  8  Variables      440  Observations
## --------------------------------------------------------------------------------
## Channel 
##        n  missing distinct     Info     Mean      Gmd 
##      440        0        2    0.656    1.323   0.4381 
##                       
## Value          1     2
## Frequency    298   142
## Proportion 0.677 0.323
## --------------------------------------------------------------------------------
## Region 
##        n  missing distinct     Info     Mean      Gmd 
##      440        0        3    0.623    2.543   0.6951 
##                             
## Value          1     2     3
## Frequency     77    47   316
## Proportion 0.175 0.107 0.718
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## Fresh 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      433        1    12000    12256    401.9    915.6 
##      .25      .50      .75      .90      .95 
##   3127.8   8504.0  16933.8  27090.5  36818.5 
## 
## lowest :      3      9     18     23     37, highest:  56083  56159  68951  76237 112151
## --------------------------------------------------------------------------------
## Milk 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      421        1     5796     6152    593.8    889.8 
##      .25      .50      .75      .90      .95 
##   1533.0   3627.0   7190.2  12229.9  16843.4 
## 
## lowest :    55   112   134   201   254, highest: 38369 43950 46197 54259 73498
## --------------------------------------------------------------------------------
## Grocery 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      430        1     7951     8365    851.5   1381.9 
##      .25      .50      .75      .90      .95 
##   2153.0   4755.5  10655.8  18910.1  24033.5 
## 
## lowest :     3   137   218   223   245, highest: 45828 55571 59598 67298 92780
## --------------------------------------------------------------------------------
## Frozen 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      426        1     3072     3635    136.8    281.3 
##      .25      .50      .75      .90      .95 
##    742.2   1526.0   3554.2   7545.3   9930.7 
## 
## lowest :    25    33    36    38    42, highest: 18028 18711 35009 36534 60869
## --------------------------------------------------------------------------------
## Detergents_Paper 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      417        1     2881     3927     63.7     99.6 
##      .25      .50      .75      .90      .95 
##    256.8    816.5   3922.0   7438.3  12043.2 
## 
## lowest :     3     5     7     9    10, highest: 24171 24231 26701 38102 40827
## --------------------------------------------------------------------------------
## Delicassen 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      440        0      403        1     1525     1677    63.95   180.80 
##      .25      .50      .75      .90      .95 
##   408.25   965.50  1820.25  2945.90  4485.40 
## 
## lowest :     3     7     8    11    18, highest:  8550 14351 14472 16523 47943
## --------------------------------------------------------------------------------

Now I did some descriptive statistics. I put mydata[,-9], because there is no point in analyzing ID.

We can see that there are no missing values in any of the variables (that is good).

We can see that there are 298 customers with channel 1 (Hotel/Restaurant/Cafee) and 142 customers with channel 2 (Retail).

The mean for Fresh is 12000 m.u., that means that on average, the customers of the wholesale distributor spend 12000 m.u. per year on fresh products.

On the other hand, the mean for Milk is 5796 m.u., that means that on average, the customers of the wholesale distributor spend 5796 m.u. per year on milk products.

The customers of the wholesale distributor spend on average 7951 m.u. per year on grocery products.

The customers of the wholesale distributor spend on average 2881 m.u. per year on on detergents and paper products.

summary(mydata[c("Fresh", "Milk", "Grocery", "Detergents_Paper")]) #Describing clustering variables
##      Fresh             Milk          Grocery      Detergents_Paper 
##  Min.   :     3   Min.   :   55   Min.   :    3   Min.   :    3.0  
##  1st Qu.:  3128   1st Qu.: 1533   1st Qu.: 2153   1st Qu.:  256.8  
##  Median :  8504   Median : 3627   Median : 4756   Median :  816.5  
##  Mean   : 12000   Mean   : 5796   Mean   : 7951   Mean   : 2881.5  
##  3rd Qu.: 16934   3rd Qu.: 7190   3rd Qu.:10656   3rd Qu.: 3922.0  
##  Max.   :112151   Max.   :73498   Max.   :92780   Max.   :40827.0

50% of the customers spend less or equal to 8504 m.u. per year on fresh products, while the other 50% spend more or equal to 8504 m.u. 50% of the customers spend less or equal to 3627 m.u. per year on milk products, while the other 50% spend more or equal to 3627 m.u. 50% of the customers spend less or equal to 4756 m.u. per year on grocery products, while the other 50% spend more or equal to 4756 m.u. 50% of the customers spend less or equal to 816.5 m.u. per year on detergents and paper products, while the other 50% spend more or equal to 816.5 m.u.

#In order to do clustering, we need to first standardize all 4 cluster variables
mydata$Fresh_z <- scale(mydata$Fresh)
mydata$Milk_z <- scale(mydata$Milk)
mydata$Grocery_z <- scale(mydata$Grocery)
mydata$Detergents_Paper_z <- scale(mydata$Detergents_Paper)

For this research I took 4 cluster variables: Fresh, Milk, Grocery and Detergents_Paper. Since all 4 variables have different range of values, we standardize them. Now they are comparable.

library(Hmisc)
rcorr(as.matrix(mydata[, c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")]), type = "pearson")
##                    Fresh_z Milk_z Grocery_z Detergents_Paper_z
## Fresh_z               1.00   0.10     -0.01              -0.10
## Milk_z                0.10   1.00      0.73               0.66
## Grocery_z            -0.01   0.73      1.00               0.92
## Detergents_Paper_z   -0.10   0.66      0.92               1.00
## 
## n= 440 
## 
## 
## P
##                    Fresh_z Milk_z Grocery_z Detergents_Paper_z
## Fresh_z                    0.0351 0.8042    0.0325            
## Milk_z             0.0351         0.0000    0.0000            
## Grocery_z          0.8042  0.0000           0.0000            
## Detergents_Paper_z 0.0325  0.0000 0.0000

With this step we are chacking if cluster variables correlate.

mydata$Dissimilarity <- sqrt(mydata$Fresh_z^2 + mydata$Milk_z^2 
                             + mydata$Grocery_z^2 + mydata$Detergents_Paper_z^2) 
#Finding outliers
head(mydata[order(-mydata$Dissimilarity), ], 10) #10 units with the highest value of dissimilarity
##     Channel Region  Fresh  Milk Grocery Frozen Detergents_Paper Delicassen  ID
## 86        2      3  16117 46197   92780   1026            40827       2944  86
## 87        2      3  22925 73498   32114    987            20070        903  87
## 48        2      3  44466 54259   55571   7782            24171       6465  48
## 334       2      2   8565  4980   67298    131            38102       1215 334
## 62        2      3  35942 38369   59598   3254            26701       2017  62
## 182       1      3 112151 29627   18148  16745             4948       8550 182
## 66        2      3     85 20959   45828     36            24231       1423  66
## 184       1      3  36847 43950   20170  36534              239      47943 184
## 212       2      1  12119 28326   39694   4736            19410       2870 212
## 93        2      3   9198 27472   32034   3232            18906       5130  93
##          Fresh_z     Milk_z Grocery_z Detergents_Paper_z Dissimilarity
## 86   0.325499741  5.4740744  8.926367          7.9586127     13.156405
## 87   0.863795224  9.1732079  2.542598          3.6050821     10.215445
## 48   2.567000718  6.5664305  5.010934          4.4652174      9.734225
## 334 -0.271622393 -0.1105995  6.244944          7.3870768      9.677512
## 62   1.893024411  4.4134240  5.434688          4.9958544      8.806602
## 182  7.918723656  3.2289317  1.072982          0.4334249      8.629677
## 66  -0.942119704  2.0544660  3.985696          4.4778017      6.406630
## 184  1.964581022  5.1696185  1.285753         -0.5542311      5.704811
## 212  0.009385561  3.0526535  3.340227          3.4666551      5.700315
## 93  -0.221572299  2.9369412  2.534180          3.3609471      5.137388
#most dissimilar - furthest away from the center

We can see that customer with ID 86 is the most dissimilar.

library(factoextra)


#Calculating Euclidean distances
distance <- get_dist(mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")],
                     method = "euclidian")

distance2 <- distance^2

fviz_dist(distance2) #Showing dissimilarity matrix

get_clust_tendency(mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")], #Hopkins statistics
                   n = nrow(mydata) - 1, 
                   graph = FALSE)
## $hopkins_stat
## [1] 0.9330562
## 
## $plot
## NULL

Here we are checking if data is clustarable, if Hopkins Statistics is less then 0.5 that would mean data is not appropriate. But our Hopkins Statistics is very high so we have nothing to worry about.

library(dplyr) #Allowing use of %>%

WARD <- mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")] %>%
  #scale() %>%                           
  get_dist(method = "euclidean") %>%  #Selecting distance
  hclust(method = "ward.D2") #Selecting algorithm

WARD
## 
## Call:
## hclust(d = ., method = "ward.D2")
## 
## Cluster method   : ward.D2 
## Distance         : euclidean 
## Number of objects: 440
library(factoextra)
fviz_dend(WARD)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

According to the dendrogram, it would make most sense to make 3 groups.

fviz_dend(WARD, 
          k = 2,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

fviz_dend(WARD, 
          k = 3,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE) 

fviz_dend(WARD, 
          k = 4,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

fviz_dend(WARD, 
          k = 5,
          cex = 0.5, 
          palette = "jama",
          color_labels_by_k = TRUE, 
          rect = TRUE)

set.seed(1)
install.packages("NbClust")
## Warning: package 'NbClust' is in use and will not be installed
library(NbClust)
OptNumber <- mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")] %>%
  #scale() %>%
  NbClust(distance = "euclidean",
          min.nc = 2, max.nc = 10, 
          method = "ward.D2", 
          index = "all")

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 2 proposed 2 as the best number of clusters 
## * 9 proposed 3 as the best number of clusters 
## * 2 proposed 4 as the best number of clusters 
## * 4 proposed 6 as the best number of clusters 
## * 2 proposed 7 as the best number of clusters 
## * 1 proposed 8 as the best number of clusters 
## * 3 proposed 10 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  3 
##  
##  
## *******************************************************************

As we saw on the dandogram, here we comfirm that the best number of clusters is 3.

mydata$ClusterWard <- cutree(WARD, 
                             k = 3) #Cutting the dendrogram
head(mydata)
##   Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen ID
## 1       2      3 12669 9656    7561    214             2674       1338  1
## 2       2      3  7057 9810    9568   1762             3293       1776  2
## 3       2      3  6353 8808    7684   2405             3516       7844  3
## 4       1      3 13265 1196    4221   6404              507       1788  4
## 5       2      3 22615 5410    7198   3915             1777       5185  5
## 6       2      3  9413 8259    5126    666             1795       1451  6
##       Fresh_z      Milk_z   Grocery_z Detergents_Paper_z Dissimilarity
## 1  0.05287300  0.52297247 -0.04106815        -0.04351919     0.5290333
## 2 -0.39085706  0.54383861  0.17012470         0.08630859     0.6963629
## 3 -0.44652098  0.40807319 -0.02812509         0.13308016     0.6200049
## 4  0.09999758 -0.62331041 -0.39253008        -0.49802132     0.8947740
## 5  0.83928412 -0.05233688 -0.07926595        -0.23165413     0.8758332
## 6 -0.20457266  0.33368675 -0.29729863        -0.22787885     0.5417675
##   ClusterWard
## 1           1
## 2           1
## 3           1
## 4           2
## 5           2
## 6           2
#Calculating positions of initial leaders
Initial_leaders <- aggregate(mydata[, c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")], 
                             by = list(mydata$ClusterWard), 
                             FUN = mean)

Initial_leaders
##   Group.1    Fresh_z     Milk_z  Grocery_z Detergents_Paper_z
## 1       1 -0.5205590  0.7529972  0.8919869          0.8981549
## 2       2  0.1702706 -0.3503476 -0.4085563         -0.4115614
## 3       3  1.0755395  5.1033075  5.6319063          5.6823687

We have 3 initial leaders because we have 3 groups (clusters). Each leader is defined with 4 means from cluster variables.

Centroid 1 = c(-0.5205590, 0.7529972, 0.8919869, 0.8981549)

library(factoextra)

#Performing K-Means clustering - initial leaders are chosen as centroids of groups, found with hierarhical clustering
K_MEANS <- hkmeans(mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")], 
                   k = 3, #number of clusters previously determend  
                   hc.metric = "euclidean",
                   hc.method = "ward.D2")

K_MEANS
## Hierarchical K-means clustering with 3 clusters of sizes 102, 328, 10
## 
## Cluster means:
##       Fresh_z     Milk_z  Grocery_z Detergents_Paper_z
## 1 -0.34182554  0.7461291  0.9075902          0.8554505
## 2  0.09674229 -0.3514623 -0.4124442         -0.4066619
## 3  0.31347349  3.9174467  4.2707490          4.6129149
## 
## Clustering vector:
##   [1] 2 1 2 2 2 2 2 2 2 1 1 2 1 1 1 2 1 2 2 2 2 2 2 1 1 2 2 2 1 2 2 2 2 2 2 1 2
##  [38] 1 1 2 2 2 1 1 1 1 1 3 1 1 2 2 2 1 2 2 3 1 2 2 2 3 2 1 2 3 2 1 2 2 2 1 2 2
##  [75] 2 2 2 1 2 2 2 1 1 2 2 3 3 2 2 2 2 2 3 2 1 2 2 2 2 2 1 1 2 2 2 2 1 1 2 1 2
## [112] 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2
## [149] 2 2 2 2 2 2 2 1 1 2 1 1 1 2 2 1 2 1 1 2 2 2 1 1 2 1 2 1 2 2 2 2 2 1 1 1 2
## [186] 2 2 1 1 1 2 2 2 1 2 2 2 1 2 2 1 1 2 2 2 1 2 2 2 1 2 3 2 2 1 1 1 2 1 2 2 1
## [223] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 3 2 2 1 2 2 2 2
## [260] 2 2 2 2 2 1 1 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2
## [297] 2 2 2 2 2 1 1 1 1 1 1 2 2 1 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2 1 2 2 2 2 2 1 2
## [334] 3 2 2 2 2 2 2 1 1 2 1 2 2 1 2 2 1 2 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2
## [371] 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2
## [408] 1 2 2 2 2 2 2 2 2 1 2 1 2 1 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 1 2 2
## 
## Within cluster sum of squares by cluster:
## [1] 283.2396 399.8343 142.3068
##  (between_SS / total_SS =  53.0 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "data"        
## [11] "hclust"

Now values of leaders have changed because some customers we reclassified. Ratio between sum of squares and total sum of squares is 53%, which is okey. (If it was less than 50% that would be bad)

fviz_cluster(K_MEANS, 
             palette = "jama", 
             repel = FALSE,
             ggtheme = theme_classic())

Now this is a representation of clusters but it is not in 2D so we can ignore the “overlapping” between groups. But we see that customer ID 182 differs from other customers in his group. Because we want the groups to be as homogeneous as possible, we will remove this customer.

mydata <- mydata %>%
  filter(!ID %in% c(182))

Now sample size is 439 since we removed one customer.

mydata_clu_std <- as.data.frame(scale(mydata[c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z")]))
library(factoextra) 

K_MEANS1 <- hkmeans(mydata_clu_std, #Data
                    k = 3, #Number of groups
                    hc.metric = "euclidean", #Distance for hierar. clus.
                    hc.method = "ward.D2") #Algorithm for hierar. clus.

K_MEANS1
## Hierarchical K-means clustering with 3 clusters of sizes 323, 106, 10
## 
## Cluster means:
##      Fresh_z     Milk_z  Grocery_z Detergents_Paper_z
## 1  0.1517070 -0.3528215 -0.4138333         -0.4139944
## 2 -0.4960250  0.7007826  0.8578182          0.8266401
## 3  0.3577294  3.9678379  4.2739441          4.6096328
## 
## Clustering vector:
##   [1] 1 2 2 1 1 1 1 1 1 2 2 1 1 2 2 1 2 1 1 1 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 2 1
##  [38] 2 2 1 1 1 2 2 2 2 2 3 2 2 1 1 1 2 1 1 3 2 1 1 1 3 1 2 1 3 1 2 1 1 1 2 1 1
##  [75] 1 1 1 2 1 1 1 2 2 1 1 3 3 1 1 1 1 1 3 1 2 1 1 1 1 1 2 2 2 1 1 1 2 2 1 2 1
## [112] 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1
## [149] 1 1 1 1 1 1 1 2 2 1 2 2 2 1 1 2 2 2 2 1 1 1 2 2 1 2 1 2 1 1 1 1 1 2 2 1 1
## [186] 1 2 2 2 1 1 1 2 1 1 1 2 1 1 2 2 1 1 1 2 1 1 1 2 1 3 1 1 2 2 2 1 2 1 1 2 1
## [223] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 3 1 1 2 1 1 1 1 1
## [260] 1 1 1 1 2 2 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1
## [297] 1 1 1 1 2 2 2 2 2 2 1 1 2 1 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 3
## [334] 1 1 1 1 1 1 2 2 1 2 1 1 2 1 1 2 1 2 1 2 1 1 1 2 2 1 1 1 1 1 1 2 1 1 1 1 1
## [371] 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2
## [408] 1 1 1 1 2 1 1 1 2 2 2 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 447.8816 217.8134 145.5470
##  (between_SS / total_SS =  53.7 %)
## 
## Available components:
## 
##  [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
##  [6] "betweenss"    "size"         "iter"         "ifault"       "data"        
## [11] "hclust"

Now values of leaders have changed because some customers we reclassified. Ratio between sum of squares and total sum of squares is 53.7%, which is also okey.

library(factoextra)
fviz_cluster(K_MEANS1, 
             palette = "jama", 
             repel = FALSE,
             ggtheme = theme_classic())

Now the groups look much more homogeneous.

mydata$ClusterK_Means <- K_MEANS1$cluster
head(mydata[c("ID", "ClusterWard", "ClusterK_Means")])
##   ID ClusterWard ClusterK_Means
## 1  1           1              1
## 2  2           1              2
## 3  3           1              2
## 4  4           2              1
## 5  5           2              1
## 6  6           2              1

Here we can see that both customers with ID 2 and ID 3 ware in group 1 yet they got reclassified to group 2. Whereas customers with ID 4, ID 5 and ID 6 ware in group 2 yet they got reclassified to group 1.

#Checking for reclassifications
table(mydata$ClusterWard)
## 
##   1   2   3 
## 115 319   5
table(mydata$ClusterK_Means)
## 
##   1   2   3 
## 323 106  10
table(mydata$ClusterWard, mydata$ClusterK_Means)
##    
##       1   2   3
##   1   9 101   5
##   2 314   5   0
##   3   0   0   5

Group 1 initially included 115 customers, 9 customers stayed in group 1, 101 customers were reclassified to group 2 and 5 were reclassified to group 3.

Group 1 currently has 323 customers, 9 were already there and 314 reclassified from group 2 to group 1.

#final leaders
Centroids <- K_MEANS1$centers
Centroids
##      Fresh_z     Milk_z  Grocery_z Detergents_Paper_z
## 1  0.1517070 -0.3528215 -0.4138333         -0.4139944
## 2 -0.4960250  0.7007826  0.8578182          0.8266401
## 3  0.3577294  3.9678379  4.2739441          4.6096328
library(ggplot2)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.2
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:magrittr':
## 
##     extract
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c(Fresh_z, Milk_z, Grocery_z, Detergents_Paper_z))

Figure$Groups <- factor(Figure$id, 
                        levels = c(1, 2, 3), 
                        labels = c("1", "2", "3"))

Figure$nameFactor <- factor(Figure$name, 
                            levels = c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z"), 
                            labels = c("Fresh_z", "Milk_z", "Grocery_z", "Detergents_Paper_z"))
ggplot(Figure, aes(x = nameFactor, y = value)) +
  geom_hline(yintercept = 0) +
  theme_bw() +
  geom_point(aes(shape = Groups, col = Groups), size = 3) +
  geom_line(aes(group = id), linewidth = 1) +
  ylab("Averages") +
  xlab("Cluster variables") +
  ylim(-2, 5)

We can see that group 3 spends much more on milk, grocery and detergents and paper products compared to other two groups. When it comes to fresh products groups do not differ as much.

Group 1 spends more on fresh products compared to other products. Group 2 spends less on fresh products compared to other products.

#Are all the cluster variables successful at classifying units into groups? Performing ANOVAs.

fit <- aov(cbind(Fresh_z, Milk_z, Grocery_z, Detergents_Paper_z) ~ as.factor(ClusterK_Means), 
           data = mydata)

summary(fit)
##  Response 1 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2  29.88 14.9404  18.812 1.457e-08 ***
## Residuals                 436 346.27  0.7942                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 2 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 244.31 122.157  289.09 < 2.2e-16 ***
## Residuals                 436 184.24   0.423                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 3 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 315.87  157.94  564.54 < 2.2e-16 ***
## Residuals                 436 121.97    0.28                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response 4 :
##                            Df Sum Sq Mean Sq F value    Pr(>F)    
## as.factor(ClusterK_Means)   2 340.91 170.455  759.12 < 2.2e-16 ***
## Residuals                 436  97.90   0.225                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

For response 1: H0: Mu fresh(G1) = Mu fresh(G2) = Mu fresh(G3) H1: At least one is different.

We reject H0 at p-value < 0.001. (If We didn’t reject H0 that would mean there is overlapping)

For response 2: H0: Mu milk(G1) = Mu milk(G2) = Mu milk(G3) H1: At least one is different.

We reject H0 at p-value < 0.001.

For response 3: H0: Mu grocery(G1) = Mu grocery(G2) = Mu grocery(G3) H1: At least one is different.

We reject H0 at p-value < 0.001.

For response 4: H0: Mu detergents_paper(G1) = Mu detergents_paper(G2) = Mu detergents_paper(G3) H1: At least one is different.

We reject H0 at p-value < 0.001.

mydata$Region_F <- factor(mydata$Region,
                          levels = c(1, 2, 3),
                          labels = c("1", "2", "3"))

#Pearson Chi2 test
chisq_results <- chisq.test(mydata$Region_F, as.factor(mydata$ClusterK_Means))
## Warning in chisq.test(mydata$Region_F, as.factor(mydata$ClusterK_Means)):
## Chi-squared approximation may be incorrect
chisq_results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$Region_F and as.factor(mydata$ClusterK_Means)
## X-squared = 0.27312, df = 4, p-value = 0.9915

H0: There is no association between Region and Cluster variables. H1: There is association between Region and Cluster variables.

We can not reject H0 at p-value = 0.992. There is no association between Region and Cluster variables.

mydata$Channel_F <- factor(mydata$Channel,
                          levels = c(1, 2),
                          labels = c("1", "2"))

chisq_results <- chisq.test(mydata$Channel_F, as.factor(mydata$ClusterK_Means))
## Warning in chisq.test(mydata$Channel_F, as.factor(mydata$ClusterK_Means)):
## Chi-squared approximation may be incorrect
chisq_results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata$Channel_F and as.factor(mydata$ClusterK_Means)
## X-squared = 216.58, df = 2, p-value < 2.2e-16

H0: There is no association between Channel and Cluster variables. H1: There is association between Channel and Cluster variables.

We reject H0 at p-value < 0.001. There is association between Channel and Cluster variables.

addmargins(chisq_results$observed)
##                 
## mydata$Channel_F   1   2   3 Sum
##              1   282  15   0 297
##              2    41  91  10 142
##              Sum 323 106  10 439
round(chisq_results$expected, 2)
##                 
## mydata$Channel_F      1     2    3
##                1 218.52 71.71 6.77
##                2 104.48 34.29 3.23

More that 20% of values are above 5 so the assumtion is met. (Otherwise we would need to do Fisher Test of Indep.).

round(chisq_results$res, 2)
##                 
## mydata$Channel_F     1     2     3
##                1  4.29 -6.70 -2.60
##                2 -6.21  9.69  3.76

4.29 - More than expected number of customers are in channel 1 (Hotel/Restaurant/Cafee) at alpha = 0.1

aggregate(mydata$Frozen, 
          by = list(mydata$ClusterK_Means), 
          FUN = "mean")
##   Group.1        x
## 1       1 3409.910
## 2       2 1914.698
## 3       3 3054.600
fit <- aov(Frozen ~ as.factor(ClusterK_Means), 
           data = mydata)

summary(fit)
##                            Df    Sum Sq  Mean Sq F value Pr(>F)  
## as.factor(ClusterK_Means)   2 1.784e+08 89213681   3.897  0.021 *
## Residuals                 436 9.980e+09 22891013                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

H0: Mu frozen(G1) = Mu frozen(G2) = Mu frozen(G3) H1: At least one is different.

We reject H0 at p-value = 0.021. (If We didn’t reject H0 that would mean there is overlapping)

aggregate(mydata$Delicassen, 
          by = list(mydata$ClusterK_Means), 
          FUN = "mean")
##   Group.1        x
## 1       1 1191.412
## 2       2 2340.934
## 3       3 2942.800
fit <- aov(Delicassen ~ as.factor(ClusterK_Means), 
           data = mydata)

summary(fit)
##                            Df    Sum Sq  Mean Sq F value   Pr(>F)    
## as.factor(ClusterK_Means)   2 1.265e+08 63250216   8.318 0.000285 ***
## Residuals                 436 3.315e+09  7604130                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

H0: Mu delicassen(G1) = Mu delicassen(G2) = Mu delicassen(G3) H1: At least one is different.

We reject H0 at p-value = 0.001. (If We didn’t reject H0 that would mean there is overlapping)

RESULTS

We divided 439 customers of a wholesale distributor into 3 groups based on four standardized variables.

For hierarchical clustering, we used Ward’s clustering algorithm and decided to divide them into 3 groups based on the analysis of the dendrogram. The classification was further optimized using K-Means clustering.

Group 1 contains the most customers (73.6% - 323/439), characterized by higher than average annual spending on fresh products and lower than average spending on milk, grocery and detergents and paper products. There are more than expected number of customers from channel 1 (Hotel/Restaurant/Cafee) and less than expected number of customers with channel 2 (Retail) (alpha = 0.1). The customers in this group spend on average 3409.910 m.u. on frozen products and 1191.412 m.u. on delicassen products.