Introduction

In this exercise the data about mall customers will be analyzed. The dataset was published on kaggle.com website and contains data about customers with membership cards. The target is to identify groups of customers to be able to adjust marketing strategies to them.

Loading and transforming the data

data <- read.csv('Data/Mall_Customers.csv')
colnames(data)[4:5] <- c('Annual income (k$)', 'Spending score')
colSums(is.na(data))
##         CustomerID             Gender                Age Annual income (k$) 
##                  0                  0                  0                  0 
##     Spending score 
##                  0

The dataset contains 5 columns and no empty cells. Columns represent:
CustomerID - identification number of a customer
Gender
Age
Annual income (k$) - annual income in thousands of USD
Spending score - a score calculated by creator of the dataset. It indicates how much does the person spend in the mall.

Summarising dataset

summary(data)
##    CustomerID        Gender               Age        Annual income (k$)
##  Min.   :  1.00   Length:200         Min.   :18.00   Min.   : 15.00    
##  1st Qu.: 50.75   Class :character   1st Qu.:28.75   1st Qu.: 41.50    
##  Median :100.50   Mode  :character   Median :36.00   Median : 61.50    
##  Mean   :100.50                      Mean   :38.85   Mean   : 60.56    
##  3rd Qu.:150.25                      3rd Qu.:49.00   3rd Qu.: 78.00    
##  Max.   :200.00                      Max.   :70.00   Max.   :137.00    
##  Spending score 
##  Min.   : 1.00  
##  1st Qu.:34.75  
##  Median :50.00  
##  Mean   :50.20  
##  3rd Qu.:73.00  
##  Max.   :99.00

The dataset contains 200 observations. Each customer is unique. Average age is almost 39. Annual incomes of customers are between 15 000 USD and 137 000 USD. Average income is 60 650 USD which is close to median: 61 500.

In this exercise we’ll be focused mostly on income and spending score.

After looking at the graph it can already be seen the data might be clusterable and number of clusters should be equal 5. This will be checked quantitavely.
Scaling the date before clustering:

data1 <- data.matrix(data[4:5])
data_scaled <- scale(data1)

Checking for clusterability

hopkins(data_scaled)
## [1] 0.6551663

Hopkins statistic is equal more than 0.9. It means the data can be clustered well.

Determining optimal number of clusters

1) rule of thumb

num_of_clusters_1 <- as.integer((length(data_scaled)/2)^(1/2))
num_of_clusters_1
## [1] 14

Rule of thums suggests using 14 clusters. The number is high and the method not very sophisticated so two more methods will be used.

2) elbow point

Optimal_Clusters_KMeans(data_scaled, max_clusters=10, plot_clusters = TRUE)

##  [1] 1.0000000 0.6905173 0.3942600 0.2730706 0.1639210 0.1384434 0.1250347
##  [8] 0.1183331 0.1012700 0.0761618

Elbow point is at 5 which coincides with what is suggested by the plot.
One more method will be used.

3) silhouette

Optimal_Clusters_KMeans(data_scaled, max_clusters=10, plot_clusters=TRUE, 
                        criterion="silhouette")

##  [1] 0.0000000 0.3407757 0.4665847 0.4943499 0.5546572 0.5368598 0.5041056
##  [8] 0.4825703 0.4421549 0.4429769

This method suggests using 5 clusters as well.
Because 14 is significantly higher number of clusters than 5 and both quantitative methods suggested using 5 clusters we’ll proceed with the lower number. This will make results easier to interpret.

Clustering

K-means

clustering1 <- kmeans(data_scaled, 5)
scaled_df <- as.data.frame(data_scaled)
scaled_df$cluster1 <- clustering1$cluster
fviz_cluster(clustering1, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

fviz_silhouette(silhouette(scaled_df$cluster1, dist(data_scaled)))
##   cluster size ave.sil.width
## 1       1   81          0.60
## 2       2   35          0.51
## 3       3   23          0.51
## 4       4   39          0.51
## 5       5   22          0.60

PAM

clustering2 <- pam(data_scaled, 5)
scaled_df$cluster2 <- clustering2$clustering
fviz_cluster(clustering2, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

fviz_silhouette(silhouette(scaled_df$cluster2, dist(data_scaled)))
##   cluster size ave.sil.width
## 1       1   21          0.56
## 2       2   21          0.63
## 3       3   84          0.58
## 4       4   39          0.52
## 5       5   35          0.51

CLARA

clara <- eclust(data_scaled, "clara", k=5) 

scaled_df$cluster3 <- clara$clustering
fviz_silhouette(clara)
##   cluster size ave.sil.width
## 1       1   21          0.56
## 2       2   21          0.63
## 3       3   84          0.58
## 4       4   39          0.52
## 5       5   35          0.51

DBSCAN

epsilon = 0.1

dbscan1 <- dbscan(data_scaled, eps = 0.1)
fviz_cluster(dbscan1, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

epsilon = 0.2

dbscan2 <- dbscan(data_scaled, eps = 0.2)
fviz_cluster(dbscan2, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

epsilon = 0.3

dbscan3 <- dbscan(data_scaled, eps = 0.3)
fviz_cluster(dbscan3, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

epsilon = 0.4

dbscan4 <- dbscan(data_scaled, eps = 0.4)
fviz_cluster(dbscan4, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

epsilon = 0.5

dbscan5 <- dbscan(data_scaled, eps = 0.5)
fviz_cluster(dbscan5, data = data[4:5],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)

Looking on the graphs it can be seen density based clustering algorithm is not suitable for this dataset. Because shapes of clusters are convex we can use different methods, also objects in clusters are not that tightly packed.

Clustering quality

Silhouette values were calculated and presented after each method used. Basing on them it can be concluded that clustering quality is on sufficient level. It might be useful to check on some other quality measures.

Shadow

d1<-cclust(data_scaled, 5, dist="euclidean")
shadow(d1)
##         1         2         3         4         5 
## 0.5090079 0.4510214 0.4252903 0.4875834 0.5199328

Shadow is on the level around 0.5 which means observations are not very significantly separated but they’re also not mixed all together.

Duda-Hart index

dudahart2(data_scaled, clustering1$cluster)
## $p.value
## [1] 0
## 
## $dh
## [1] 0.08238763
## 
## $compare
## [1] 0.5131782
## 
## $cluster1
## [1] FALSE
## 
## $alpha
## [1] 0.001
## 
## $z
## [1] 3.090232

Duda-Hart index suggests heterogeneity of clusters.

Results

All three methods (K-means, PAM and CLARA) gave very similar results. It can be seen people with lowest and highest income are split into 2 groups: those who spend the most and those who spend the least. People with average income are assigned to 1 cluster of people spending average amount of money in the mall. To better understand why such split can be observed another variable might be added to analysis. Let’s try to look at clusters with dependence of age.

fviz_cluster(clustering1, data = data[c('Age', 'Spending score')],
             geom = "point",
             ellipse.type = "convex", 
             ggtheme = theme_bw()
)


As per the graph, we can see age might be also influential when it comes to money spending in the mall. Among people who spend the most there are no oldest customers. It might be a clue for mall owners to adjust advertisements to older people too.