- 26th Aug 2021
- 06:03 am
#Q2. Perform K-means clustering on file19.txt on the above web page.
library(factoextra) library(fpc) library(dplyr)
#Q2.2. K-means clustering (2.5 points divided evenly among the components)
clust<-read.csv("Cluster.csv") str(clust) cluste<-scale(clust[-1]) cluster<-data.frame(cluste) rownames(cluster)<-clust$Name res.dst<-get_dist(cluster, method="pearson") fviz_dist(res.dst,lab_size=8) res.km<-eclust(cluster, "kmeans",nstart=20) fviz_gap_stat(res.km$gap_stat) fviz_silhouette(res.km) res.km$nbclust fviz_nbclust(res.km) fviz_cluster(res.km) clusters<-res.km$cluster Clust1<-cbind(cluster, clusters) table(Clust1$clusters) aggregate(clust[-1], by=list(cluster=res.km$cluster), mean) #Q2.3. Hierarchical clustering (3 points divided evenly among the components) set.seed(1122) clust_1<-sample(cluster,size=35, replace=TRUE) rownames(clust_1)<-clust$Name res.hclust<-eclust(clust_1, "hclust", hc_method="single") fviz_dend(res.hclust, rect = TRUE) fviz_silhouette(res.hclust) fviz_cluster(res.hclust) fviz_dend(res.hclust) res.hclust_2<-eclust(clust_1[,2:9], "hclust", hc_method="complete") fviz_dend(res.hclust_2, rect = TRUE) fviz_silhouette(res.hclust_2) fviz_cluster(res.hclust_2) fviz_dend(res.hclust_2) res.hclust_3<-eclust(clust_1[,2:9], "hclust", hc_method="average") fviz_dend(res.hclust_3, rect = TRUE) fviz_silhouette(res.hclust_3) fviz_cluster(res.hclust_3) fviz_dend(res.hclust_3) #Complete produces least singleton sets res.hclust_4<-eclust(clust_1[,2:9], "hclust",k=3, hc_method="complete") fviz_dend(res.hclust_4, rect = TRUE) fviz_silhouette(res.hclust_4) fviz_cluster(res.hclust_4) fviz_dend(res.hclust_4)