1 Introduction

Cluster analysis is a multivariate method which aims to classify a sample of subjects (or objects) on the basis of a set of measured variables into a number of different groups such that similar subjects are placed in the same group.

2 Approaches to cluster analysis

There are a number of different methods that can be used to carry out a cluster analysis; these methods can be classified as follows:

  1. Hierarchical methods

  2. Non-hierarchical methods (often known as k-means clustering methods)

3 Example hierarchical clustering methods

  1. Memanggil package yang dibutuhkan

Ada beberapa package yang dibutuhkan.

  1. ggplot

  2. dplyr

  3. cluster

  4. factoextra

JIka belum diinstall, silahkan diinstall terlebih dahulu dengan install.packages(“nama_package”) kemudian dapat dilakukan pemanggilan package:

library(ggplot2)
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(cluster)
library(factoextra)
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
  1. Memanggil atau membangkitkan data dan persiapan data
# Simulate customer data (replace this with your dataset)
set.seed(123)  # For reproducibility
customers <- data.frame(
  CustomerID = 1:200,
  Age = sample(18:70, 200, replace = TRUE),
  Income = sample(15:140, 200, replace = TRUE),
  SpendingScore = sample(1:100, 200, replace = TRUE)
)

head(customers) # View the first few rows of the customers data
##   CustomerID Age Income SpendingScore
## 1          1  48     91            94
## 2          2  32     60            35
## 3          3  68     84            30
## 4          4  31    135            89
## 5          5  20     86            50
## 6          6  59     58            47

Memilih peubah yang akan digerombolkan

customer_features <- customers %>% select( Income, SpendingScore) #remove customberId
head(customer_features) # View the first few rows of the customer_features data
##   Income SpendingScore
## 1     91            94
## 2     60            35
## 3     84            30
## 4    135            89
## 5     86            50
## 6     58            47

Standarisasi peubah yang akan digerombolkan

customer_features_standardize <- scale(customer_features) # mean 0 and standar deviation 1
head(customer_features_standardize)# View the first few rows of the customer_features_standardize data # Check the scaled data
##          Income SpendingScore
## [1,]  0.4217734  1.5901877692
## [2,] -0.4308497 -0.5416249639
## [3,]  0.2292456 -0.7222870599
## [4,]  1.6319480  1.4095256732
## [5,]  0.2842535  0.0003613242
## [6,] -0.4858576 -0.1080359334
  1. Membuat dendogram dan menggerombolkan data

Dalam hierarchical clustering untuk menentukan cara menggabungkan cluster secara iteratif.

# Membuat dendrogram dengan Ward.D
dendrogram <- hclust(d = dist(customer_features_standardize, method = 'euclidean'), method = 'ward.D')
# Membuat gambar dendogram
plot(dendrogram)

Terdapat beberapa metode lain dalam hclust():

  1. “single”: Single linkage (nearest neighbor)

  2. “complete”: Complete linkage (farthest neighbor)

  3. “average”: Average linkage (UPGMA)

  4. “mcquitty”: McQuitty’s method (WPGMA)

  5. “median”: Median linkage (WPGMC)

  6. “centroid”: Centroid linkage (UPGMC)

  7. “ward.D2”: Ward’s minimum variance method, squared distance

Membuat model clustering dengan hclust dimana pada contoh dibawah ini digunakan jarak euclidean dan metode penggerombolan ward

hc_w = hclust(d = dist(customer_features_standardize, method = 'euclidean'), method = 'ward.D2')

Memotong dendrogram menjadi 4 gerombol

y_hc_w = cutree(hc_w, 4)
y_hc_w
##   [1] 1 2 2 1 2 2 3 1 3 3 1 4 1 3 2 2 4 4 3 3 1 2 2 1 1 3 3 3 2 2 2 1 4 2 2 4 3
##  [38] 1 4 3 1 2 1 3 2 2 2 1 4 3 2 3 4 2 2 1 4 2 1 4 3 2 1 1 3 4 2 4 2 3 2 1 1 2
##  [75] 2 2 4 4 2 4 2 1 3 4 1 4 2 1 3 1 3 2 3 2 3 3 1 1 3 2 4 2 1 3 1 4 4 2 1 2 4
## [112] 3 2 4 4 2 2 1 3 4 1 2 2 3 2 2 3 2 2 2 2 1 3 2 4 2 4 3 3 2 2 2 3 2 3 2 1 3
## [149] 3 2 2 1 1 4 1 1 3 1 1 3 2 1 2 2 2 2 2 2 2 1 3 3 3 3 3 2 4 2 2 3 1 4 1 2 4
## [186] 1 2 1 4 2 1 4 1 2 2 3 1 3 2 2
  1. Visualisasi hasil gerombol
library(factoextra)
fviz_dend(hc_w, k = 4, # Cut in 4 groups
          cex = 0.5, # label size
          rect = TRUE # Add rectangle around groups
          )
## 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.

#Peringatan tersebut mengindikasikan bahwa paket factoextra menggunakan fitur lama dari ggplot2 yang tidak didukung lagi dalam versi ggplot2 3.3.4 ke atas
# Alternative 2D plot
ggplot(customers, aes(x = Income, y = SpendingScore, color = factor(y_hc_w))) +
  geom_point(size = 3) +
  labs(title = "Customer Segments", x = "Annual Income", y = "Spending Score") +
  theme_minimal()

Plot ini bertujuan untuk memvisualisasikan distribusi segmen pelanggan berdasarkan dua variabel: Income (pendapatan tahunan) dan SpendingScore (skor belanja), serta menunjukkan cluster mana masing-masing pelanggan berada. Plot ini membantu dalam memahami pola pelanggan dan membedakan karakteristik setiap cluster secara visual.

Jika terdapat 4 cluster, hasil plot mungkin menunjukkan:

Cluster 1: Pelanggan dengan pendapatan tinggi dan skor belanja tinggi.

Cluster 2: Pelanggan dengan pendapatan tinggi dan skor belanja rendah.

Cluster 3: Pelanggan dengan pendapatan rendah dan skor belanja tinggi.

Cluster 4: Pelanggan dengan pendapatan rendah dan skor belanja rendah.

Jika ingin meningkatkan belanja pelanggan, maka Cluster 2 (Utama) dan Cluster 4 (Sekunder) adalah target yang paling potensial.

Cluster 2: Pendapatan Tinggi, Skor Belanja Rendah (Terget Utama)

Potensi:

Pelanggan ini memiliki pendapatan tinggi, artinya mereka mampu berbelanja lebih banyak.

Skor belanja rendah menunjukkan bahwa pelanggan ini belum memanfaatkan potensi mereka untuk belanja.

Strategi:

Berikan promosi eksklusif atau produk premium untuk menarik perhatian mereka.

Tawarkan program loyalitas yang mengapresiasi pembelian besar atau sering.

Gunakan kampanye pemasaran yang menyasar kebutuhan gaya hidup mereka.

Cluster 4: Pendapatan Rendah, Skor Belanja Rendah (Target Sekunder)

Potensi:

Pelanggan ini memiliki pendapatan rendah, sehingga kemampuan belanja mereka terbatas.

Namun, meningkatkan belanja mereka meskipun sedikit dapat memberikan keuntungan dalam volume penjualan.

Strategi:

Fokus pada harga yang terjangkau atau diskon menarik.

Tawarkan paket hemat atau promosi “beli satu gratis satu”.

Pastikan produk yang ditawarkan relevan dengan kebutuhan dasar mereka.