#///////////////////////////////////////////////////////////
#////   12/6(목)/2018 Clustering by K-Means in R 
#///////////////////////////////////////////////////////////
  
library("factoextra")
## Warning: package 'factoextra' was built under R version 3.4.4
## Loading required package: ggplot2
## Welcome! Related Books: `Practical Guide To Cluster Analysis in R` at https://goo.gl/13EFCZ
data("USArrests") #50개 행, 4개 컬럼(Murder, Assault, UrbanPop, Rape)
my_data <-USArrests
my_data.scaled <- scale(my_data)   #정규화. (2) fviz_cluster를 사용할 때, 정규화가 되어 있어야 한다.
#(∵ fviz_cluster의 argument중 stand=TRUE (default)이고, 
#  PCA하기 전에 정규화되어 있어야 함. 
#  그래프에서 Principal Component사용한다.
fviz_nbclust(my_data.scaled, kmeans, method="gap_stat")  #3이 나옴

#kmeans(x, centers, iter.max=10, nstart=1, algorithm,=c(“Hartigan-Wong”,”Lloyd”,”Forgy”,
#                ”MacQueen”, trace=FALSE)
  
  
km.res <- kmeans(my_data.scaled, 3, nstart=25)
fviz_cluster(km.res, data=my_data.scaled, ellipse.type="convex", 
             palette="jco", ggtheme=theme_minimal())