图形设置
par(ask=TRUE)
opar <- par(no.readonly=FALSE)
输入flexclust包自带数据nutrient,是27个不同种类的肉的5个成分含量
install.packages(“flexclust”)
data(nutrient, package=“flexclust”)
row.names(nutrient) <- tolower(row.names(nutrient))
标准化,生成距离结构,类平均发生成聚类
nutrient.scaled <- scale(nutrient)
d <- dist(nutrient.scaled)
fit.average <- hclust(d, method=“average”)
plot(fit.average, hang=-1, cex=.8, main=“Average Linkage Clustering”)
![China (Lanzhou) [https]](/https://i-blog.csdnimg.cn/blog_migrate/65eee1467bd949faea8b6d6fd190b690.png)
用NbClust()函数判断生成类的个数
install.packages(‘NbClust’)
library(NbClust)
nc <- NbClust(nutrient.scaled, distance=“euclidean”,
min.nc=2, max.nc=15, method=“average”)

设置图形
par(opar)
table(nc$Best.n[1,])
标准图
barplot(table(nc$Best.n[1,]), xlab=“Numer of Clusters”, ylab=“Number of Criteria”,main=“Number of Clusters Chosen by 26 Criteria”)

library(dplyr)
library(ggplot2)
library(purrr)
library(tibble)
library(tidyr)
library("kernlab")
library("ggplot2")
sink("Clustering.Results")
newdata = newdata[,PArameter3]
r = cor(newdata)
d <- as.dist(1-r)
jpeg(file = "cluster-summary.jpg")
hc1 <- hclust(d, "single"); hc2 <- hclust(d, "complete")
hc3 <- hclust(d, "median"); hc4 <- hclust(d, "mcquitty")
opar <- par(mfrow = c(2, 2))
plot(hc1, hang = -1); plot(hc2, hang = -1)
plot(hc3, hang = -1); plot(hc4, hang = -1)
par(opar)
jpeg(file = "single-cluster.jpg")
dend1 <- as.dendrogram(hc1)
opar <- par(mfrow = c(2, 2),mar = c(4,3,1,2))
plot(dend1)
plot(dend1, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
type = "t", center = TRUE)
plot(dend1, edgePar = list(col = 1:2, lty = 2:3), dLeaf=1, edge.root = TRUE)
plot(dend1, nodePar = list(pch = 2:1, cex = .4*2:1, col = 2:3), horiz = TRUE)
par(opar)
dev.off()
jpeg(file = "complete-cluster.jpg")
dend2 <- as.dendrogram(hc2)
opar <- par(mfrow = c(2, 2),mar = c(4,3,1,2))
plot(dend2)
plot(dend2, nodePar=list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
type = "t", center = TRUE)
plot(dend2, edgePar = list(col = 1:2, lty = 2:3), dLeaf = 1, edge.root = TRUE)
plot(dend2, nodePar=list(pch = 2:1,cex = .4*2:1, col = 2:3), horiz = TRUE)
par(opar)
dev.off()
jpeg(file = "median-cluster.jpg")
dend3 <- as.dendrogram(hc3)
opar <- par(mfrow = c(2, 2),mar = c(4, 3, 1, 2))
plot(dend3)
plot(dend3, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
type = "t", center = TRUE)
plot(dend3, edgePar = list(col = 1:2, lty = 2:3), dLeaf = 1, edge.root = TRUE)
plot(dend3, nodePar = list(pch = 2:1,cex = .4*2:1, col = 2:3), horiz = TRUE)
par(opar)
dev.off()
jpeg(file = "mcquitty-cluster.jpg")
dend4 <- as.dendrogram(hc4)
opar <- par(mfrow = c(2, 2),mar = c(4, 3, 1, 2))
plot(dend4)
plot(dend4, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
type = "t", center = TRUE)
plot(dend4, edgePar = list(col = 1:2, lty = 2:3), dLeaf=1, edge.root = TRUE)
plot(dend4, nodePar = list(pch = 2:1,cex = .4*2:1, col = 2:3), horiz = TRUE)
par(opar)
dev.off()
sink()

本文通过使用R语言的flexclust和NbClust包,对27种不同类型的肉类进行成分含量的聚类分析,展示了如何确定最优聚类数量,并生成了四种不同的聚类链接方法的图形结果。

2191

被折叠的 条评论
为什么被折叠?



