Email             :
RPubs            : https://rpubs.com/JoHndes9
Department  : Business Statistics
Address         : ARA Center, Matana University Tower
                         Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua, Tangerang, Banten 15810.



1 pendahuluan

Hari ini saya akan melakukan latihan clustering pada data USArrest. Cara clustering yang akan digunakan hierarchical dan partition.

data("USArrests") #memasukan data USArrest
#memanggil package yang akan digunakan
library(cluster)
library(dplyr)
library(plotly)
library(factoextra)
library(ggplot2)

2 cluster hierarchical

Cluster hierarchical dapat dibagi menjadi dua metode: aglorometative dan divisi

2.1 metode aglorometative

2.1.1 mengclusterkan data

pada metode ini akan digunakan metode complete

d=dist(USArrests)
hc=hclust(d,method="complete") #membuat dendogram cluster
plot(hc,cex=0.5,hang = -1)

pada pohon ini akan dipotong ditengah dan dijadikan 3 cluster

2.1.2 visualisasi data

cluster=cutree(hc,h=150)
clustered=cbind(USArrests,cluster=cluster)
plot(USArrests,col=cluster)

2.2 metode divisi

2.2.1 mengclusterkan data

pada metode ini akan digunakan metode top-down

div=diana(USArrests)
plot(div)

pada pohon ini akan dipotong ditengah dan dijadikan 3 cluster

2.2.2 visualisasi data

div_clustr=cutree(div,k=3)
plot(USArrests,col=div_clustr)

3 cluster partition

3.1 menghitung banyak cluster

untuk menentukan jumlah cluster dapat digunakan elbow method, shiloutte method, dan shiloutte method

fviz_nbclust(USArrests,kmeans,method = "wss") #elbow method

fviz_nbclust(USArrests,kmeans,method = "silhouette") #shiloutte method

fviz_nbclust(USArrests,kmeans,method = "gap_stat") #shiloutte method

berdasarkan hasul ketiga tes tersebut maka akan dibagi menjadi dua cluster

3.2 mengclusterkan data

untuk mengclusterkan data akan digunakan metode k-means

set.seed(4023192)
cluster_arrest=kmeans(USArrests,centers = 2)
cluster_arrest
## K-means clustering with 2 clusters of sizes 21, 29
## 
## Cluster means:
##      Murder  Assault UrbanPop     Rape
## 1 11.857143 255.0000 67.61905 28.11429
## 2  4.841379 109.7586 64.03448 16.24828
## 
## Clustering vector:
##        Alabama         Alaska        Arizona       Arkansas     California 
##              1              1              1              1              1 
##       Colorado    Connecticut       Delaware        Florida        Georgia 
##              1              2              1              1              1 
##         Hawaii          Idaho       Illinois        Indiana           Iowa 
##              2              2              1              2              2 
##         Kansas       Kentucky      Louisiana          Maine       Maryland 
##              2              2              1              2              1 
##  Massachusetts       Michigan      Minnesota    Mississippi       Missouri 
##              2              1              2              1              2 
##        Montana       Nebraska         Nevada  New Hampshire     New Jersey 
##              2              2              1              2              2 
##     New Mexico       New York North Carolina   North Dakota           Ohio 
##              1              1              1              2              2 
##       Oklahoma         Oregon   Pennsylvania   Rhode Island South Carolina 
##              2              2              2              2              1 
##   South Dakota      Tennessee          Texas           Utah        Vermont 
##              2              1              1              2              2 
##       Virginia     Washington  West Virginia      Wisconsin        Wyoming 
##              2              2              2              2              2 
## 
## Within cluster sum of squares by cluster:
## [1] 41636.73 54762.30
##  (between_SS / total_SS =  72.9 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"

3.3 mengvisualisasikan cluster

clusplot(USArrests,cluster_arrest$cluster,color = T,shade = T)

plot(USArrests,col=cluster_arrest$cluster)