Analisis gerombol (Cluster Analysis) merupakan metode dalam
statistika multivariat yang digunakan untuk mengelompokkan objek
berdasarkan kemiripan karakteristiknya.
Tujuan dari analisis ini adalah agar objek dalam satu kelompok memiliki
kemiripan yang tinggi, sedangkan antar kelompok berbeda secara
signifikan.
Pada latihan ini akan dilakukan Analisis Gerombol
Berhierarki menggunakan dataset iris.
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.5.2
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
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
# Menggunakan dataset bawaan R
data(iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
# Hanya ambil variabel numerik
iris_data <- iris[, 1:4]
Standarisasi diperlukan agar setiap variabel memiliki skala yang sama.
iris_scaled <- scale(iris_data)
Digunakan ukuran jarak Euclidean distance untuk menghitung kemiripan antar observasi.
dist_iris <- dist(iris_scaled, method = "euclidean")
Metode Ward.D2 digunakan untuk meminimalkan variasi dalam cluster yang terbentuk.
hc_iris <- hclust(dist_iris, method = "ward.D2")
Dendrogram memperlihatkan bagaimana setiap observasi bergabung ke dalam kelompok secara bertingkat.
fviz_dend(hc_iris,
k = 3,
rect = TRUE,
rect_border = "steelblue",
main = "Dendrogram Data Iris (Metode Ward.D2)",
xlab = "Observasi",
ylab = "Jarak (Euclidean)")
## 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.
Dendrogram dipotong menjadi 3 cluster, sesuai dengan jumlah spesies
pada data iris.
cluster_iris <- cutree(hc_iris, k = 3)
table(cluster_iris)
## cluster_iris
## 1 2 3
## 49 30 71
Tambahkan hasil cluster ke dalam data.
iris$Cluster <- as.factor(cluster_iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Cluster
## 1 5.1 3.5 1.4 0.2 setosa 1
## 2 4.9 3.0 1.4 0.2 setosa 1
## 3 4.7 3.2 1.3 0.2 setosa 1
## 4 4.6 3.1 1.5 0.2 setosa 1
## 5 5.0 3.6 1.4 0.2 setosa 1
## 6 5.4 3.9 1.7 0.4 setosa 1
Visualisasi berikut menampilkan hasil pengelompokan dalam ruang dua dimensi.
fviz_cluster(list(data = iris_scaled, cluster = cluster_iris),
geom = "point",
ellipse.type = "convex",
palette = "jco",
ggtheme = theme_minimal(),
main = "Visualisasi Cluster Iris (Hierarchical Clustering)")
iris.