Data Pelanggan
# Data pelanggan
ID <- 1:30
Umur <- c(22,25,27,29,31,33,35,37,39,41,43,46,47,49,51,53,
24,26,28,30,32,34,36,38,40,42,44,46,48,50)
Pendapatan <- c(3.0,3.8,4.2,4.8,5.5,6.0,6.5,7.0,7.5,8.0,
8.5,9.0,9.5,10.0,10.5,11.0,
3.5,4.0,4.6,5.2,5.8,6.3,6.8,7.3,7.8,8.3,8.8,9.3,9.8,10.3)
Skor_Belanja <- c(78,70,65,60,55,50,48,45,42,40,38,35,32,30,28,25,
75,68,63,58,52,49,46,43,41,39,36,33,31,29)
Frekuensi_Belanja <- c(11,9,8,7,6,6,5,5,4,4,3,3,2,2,2,2,
10,9,8,7,6,6,5,4,4,3,3,2,2,2)
Loyalitas <- c(9,8,8,7,7,6,6,6,6,5,5,5,5,4,4,4,
8,8,7,7,7,6,6,6,5,5,5,5,4,4)
pelanggan <- data.frame(ID, Umur, Pendapatan, Skor_Belanja,
Frekuensi_Belanja, Loyalitas)
pelanggan
## ID Umur Pendapatan Skor_Belanja Frekuensi_Belanja Loyalitas
## 1 1 22 3.0 78 11 9
## 2 2 25 3.8 70 9 8
## 3 3 27 4.2 65 8 8
## 4 4 29 4.8 60 7 7
## 5 5 31 5.5 55 6 7
## 6 6 33 6.0 50 6 6
## 7 7 35 6.5 48 5 6
## 8 8 37 7.0 45 5 6
## 9 9 39 7.5 42 4 6
## 10 10 41 8.0 40 4 5
## 11 11 43 8.5 38 3 5
## 12 12 46 9.0 35 3 5
## 13 13 47 9.5 32 2 5
## 14 14 49 10.0 30 2 4
## 15 15 51 10.5 28 2 4
## 16 16 53 11.0 25 2 4
## 17 17 24 3.5 75 10 8
## 18 18 26 4.0 68 9 8
## 19 19 28 4.6 63 8 7
## 20 20 30 5.2 58 7 7
## 21 21 32 5.8 52 6 7
## 22 22 34 6.3 49 6 6
## 23 23 36 6.8 46 5 6
## 24 24 38 7.3 43 4 6
## 25 25 40 7.8 41 4 5
## 26 26 42 8.3 39 3 5
## 27 27 44 8.8 36 3 5
## 28 28 46 9.3 33 2 5
## 29 29 48 9.8 31 2 4
## 30 30 50 10.3 29 2 4
# 1. Standardisasi data
pelanggan_std <- scale(pelanggan[,-1])
pelanggan_std
## Umur Pendapatan Skor_Belanja Frekuensi_Belanja Loyalitas
## [1,] -1.73538340 -1.76713522 2.10634476 2.2512133 2.17095758
## [2,] -1.40022351 -1.42120337 1.56625636 1.5008088 1.46303663
## [3,] -1.17678359 -1.24823744 1.22870111 1.1256066 1.46303663
## [4,] -0.95334367 -0.98878855 0.89114586 0.7504044 0.75511568
## [5,] -0.72990375 -0.68609818 0.55359061 0.3752022 0.75511568
## [6,] -0.50646382 -0.46989077 0.21603536 0.3752022 0.04719473
## [7,] -0.28302390 -0.25368336 0.08101326 0.0000000 0.04719473
## [8,] -0.05958398 -0.03747595 -0.12151989 0.0000000 0.04719473
## [9,] 0.16385594 0.17873146 -0.32405304 -0.3752022 0.04719473
## [10,] 0.38729587 0.39493887 -0.45907514 -0.3752022 -0.66072622
## [11,] 0.61073579 0.61114627 -0.59409724 -0.7504044 -0.66072622
## [12,] 0.94589567 0.82735368 -0.79663039 -0.7504044 -0.66072622
## [13,] 1.05761563 1.04356109 -0.99916354 -1.1256066 -0.66072622
## [14,] 1.28105555 1.25976850 -1.13418564 -1.1256066 -1.36864717
## [15,] 1.50449548 1.47597591 -1.26920774 -1.1256066 -1.36864717
## [16,] 1.72793540 1.69218332 -1.47174089 -1.1256066 -1.36864717
## [17,] -1.51194347 -1.55092781 1.90381161 1.8760111 1.46303663
## [18,] -1.28850355 -1.33472040 1.43123426 1.5008088 1.46303663
## [19,] -1.06506363 -1.07527151 1.09367901 1.1256066 0.75511568
## [20,] -0.84162371 -0.81582262 0.75612376 0.7504044 0.75511568
## [21,] -0.61818378 -0.55637373 0.35105746 0.3752022 0.75511568
## [22,] -0.39474386 -0.34016632 0.14852431 0.3752022 0.04719473
## [23,] -0.17130394 -0.12395891 -0.05400884 0.0000000 0.04719473
## [24,] 0.05213598 0.09224849 -0.25654199 -0.3752022 0.04719473
## [25,] 0.27557590 0.30845590 -0.39156409 -0.3752022 -0.66072622
## [26,] 0.49901583 0.52466331 -0.52658619 -0.7504044 -0.66072622
## [27,] 0.72245575 0.74087072 -0.72911934 -0.7504044 -0.66072622
## [28,] 0.94589567 0.95707813 -0.93165249 -1.1256066 -0.66072622
## [29,] 1.16933559 1.17328554 -1.06667459 -1.1256066 -1.36864717
## [30,] 1.39277551 1.38949295 -1.20169669 -1.1256066 -1.36864717
## attr(,"scaled:center")
## Umur Pendapatan Skor_Belanja Frekuensi_Belanja
## 37.533333 7.086667 46.800000 5.000000
## Loyalitas
## 5.933333
## attr(,"scaled:scale")
## Umur Pendapatan Skor_Belanja Frekuensi_Belanja
## 8.950952 2.312594 14.812390 2.665229
## Loyalitas
## 1.412587
# 2. K-Means dan Elbow Method
wssplot <- function(data, nc=10, seed=1234){
wss <- (nrow(data)-1)*sum(apply(data,2,var))
for (i in 2:nc){
set.seed(seed)
wss[i] <- sum(kmeans(data, centers=i)$withinss)
}
plot(1:nc, wss, type="b",
xlab="Number of Clusters",
ylab="Within groups sum of squares",
main="WSS Plot")
}
wssplot(pelanggan_std)

set.seed(123)
clustering <- kmeans(pelanggan_std, 3)
clustering
## K-means clustering with 3 clusters of sizes 13, 8, 9
##
## Cluster means:
## Umur Pendapatan Skor_Belanja Frekuensi_Belanja Loyalitas
## 1 0.9630834 0.9537519 -0.8901072 -0.89471296 -0.9330035
## 2 -1.2466086 -1.2752634 1.3721621 1.36010801 1.2860564
## 3 -0.2830239 -0.2440741 0.0660108 0.08337827 0.2045105
##
## Clustering vector:
## [1] 2 2 2 2 3 3 3 3 3 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 1 1
##
## Within cluster sum of squares by cluster:
## [1] 8.577301 6.578311 3.667056
## (between_SS / total_SS = 87.0 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
clustering$cluster
## [1] 2 2 2 2 3 3 3 3 3 1 1 1 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 1 1
clustering$size
## [1] 13 8 9
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.5.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.5.3
## Welcome to factoextra!
## Want to learn more? See two factoextra-related books at https://www.datanovia.com/library/principal-component-methods
fviz_cluster(clustering, data=pelanggan_std)

# 3. PCA menjadi 2 komponen utama
hasilpca <- prcomp(pelanggan_std)
hasilpca
## Standard deviations (1, .., p=5):
## [1] 2.21694768 0.20691257 0.18993842 0.07653714 0.01989011
##
## Rotation (n x k) = (5 x 5):
## PC1 PC2 PC3 PC4 PC5
## Umur 0.4478841 0.3084361 0.51817153 0.16391215 0.639451342
## Pendapatan 0.4491514 0.2284119 0.40873908 0.01844469 -0.760712317
## Skor_Belanja -0.4491174 0.3240183 0.07081994 0.82232207 -0.109893976
## Frekuensi_Belanja -0.4456908 0.7081664 0.12657908 -0.53274456 0.004577939
## Loyalitas -0.4442028 -0.4961931 0.73715188 -0.11297015 -0.017919526
hasilpca$sdev^2
## [1] 4.9148570347 0.0428128132 0.0360766017 0.0058579339 0.0003956165
hasilpca$sdev^2 / sum(hasilpca$sdev^2)
## [1] 9.829714e-01 8.562563e-03 7.215320e-03 1.171587e-03 7.912331e-05
pca2 <- data.frame(hasilpca$x[,1:2])
pca2$Cluster <- factor(clustering$cluster)
pca2
## PC1 PC2 Cluster
## 1 -4.484648449 0.26062401 2
## 2 -3.287687971 0.08787015 2
## 3 -2.791098777 -0.17878512 2
## 4 -1.941205183 -0.07442113 2
## 5 -1.386350078 -0.31144579 3
## 6 -0.723102640 0.04874693 3
## 7 -0.298052634 -0.14240702 3
## 8 -0.009906429 -0.08973017 3
## 9 0.445463963 -0.30275894 3
## 10 1.017750243 0.12305823 1
## 11 1.442800248 -0.06809572 1
## 12 1.780984053 0.01903960 1
## 13 2.186316846 -0.22844764 1
## 14 2.758603126 0.19736953 1
## 15 3.016428945 0.27192119 1
## 16 3.304575151 0.32459803 1
## 17 -3.714817597 0.39886075 2
## 18 -3.138165660 0.09833273 2
## 19 -2.288272066 0.20269671 2
## 20 -1.752838932 -0.04420482 2
## 21 -1.187085411 -0.31298116 3
## 22 -0.584478745 0.09096120 3
## 23 -0.129108353 -0.12206757 3
## 24 0.326262038 -0.33509633 3
## 25 0.898548318 0.09072084 1
## 26 1.323598324 -0.10043312 1
## 27 1.611744529 -0.04775627 1
## 28 2.067114921 -0.26078504 1
## 29 2.639401201 0.16503213 1
## 30 2.897227020 0.23958379 1
# Visualisasi PCA
library(ggplot2)
ggplot(pca2, aes(x=PC1, y=PC2, color=Cluster)) +
geom_point(size=3) +
labs(title="Visualisasi K-Means berdasarkan PCA",
x="PC1",
y="PC2")

# 4. Karakteristik masing-masing cluster
hasil_cluster <- cbind(pelanggan,
Cluster=clustering$cluster)
hasil_cluster
## ID Umur Pendapatan Skor_Belanja Frekuensi_Belanja Loyalitas Cluster
## 1 1 22 3.0 78 11 9 2
## 2 2 25 3.8 70 9 8 2
## 3 3 27 4.2 65 8 8 2
## 4 4 29 4.8 60 7 7 2
## 5 5 31 5.5 55 6 7 3
## 6 6 33 6.0 50 6 6 3
## 7 7 35 6.5 48 5 6 3
## 8 8 37 7.0 45 5 6 3
## 9 9 39 7.5 42 4 6 3
## 10 10 41 8.0 40 4 5 1
## 11 11 43 8.5 38 3 5 1
## 12 12 46 9.0 35 3 5 1
## 13 13 47 9.5 32 2 5 1
## 14 14 49 10.0 30 2 4 1
## 15 15 51 10.5 28 2 4 1
## 16 16 53 11.0 25 2 4 1
## 17 17 24 3.5 75 10 8 2
## 18 18 26 4.0 68 9 8 2
## 19 19 28 4.6 63 8 7 2
## 20 20 30 5.2 58 7 7 2
## 21 21 32 5.8 52 6 7 3
## 22 22 34 6.3 49 6 6 3
## 23 23 36 6.8 46 5 6 3
## 24 24 38 7.3 43 4 6 3
## 25 25 40 7.8 41 4 5 1
## 26 26 42 8.3 39 3 5 1
## 27 27 44 8.8 36 3 5 1
## 28 28 46 9.3 33 2 5 1
## 29 29 48 9.8 31 2 4 1
## 30 30 50 10.3 29 2 4 1
aggregate(hasil_cluster[,2:6],
by=list(Cluster=hasil_cluster$Cluster),
FUN=mean)
## Cluster Umur Pendapatan Skor_Belanja Frekuensi_Belanja Loyalitas
## 1 1 46.15385 9.292308 33.61538 2.615385 4.615385
## 2 2 26.37500 4.137500 67.12500 8.625000 7.750000
## 3 3 35.00000 6.522222 47.77778 5.222222 6.222222