r - 使用 R 中的 clusGAP 函数查找集群的数量
问题描述
你能帮我找到使用该clusGap
功能的理想集群数量吗?此链接中有一个类似的示例:https ://www.rdocumentation.org/packages/factoextra/versions/1.0.7/topics/fviz_nbclust
但我想为我的情况做这件事。我的代码如下:
library(cluster)
df <- structure(
list(Propertie = c(1,2,3,4,5,6,7,8), Latitude = c(-24.779225, -24.789635, -24.763461, -24.794394, -24.747102,-24.781307,-24.761081,-24.761084),
Longitude = c(-49.934816, -49.922324, -49.911616, -49.906262, -49.890796,-49.8875254,-49.8875254,-49.922244),
waste = c(526, 350, 526, 469, 285, 433, 456,825)),class = "data.frame", row.names = c(NA, -8L))
df<-scale(df)
hcluster = clusGap(df, FUN = hcut, K.max = 100, B = 50)
Clustering k = 1,2,..., K.max (= 100): .. Error in sil.obj[, 1:3] : incorrect number of dimensions
解决方案
这里的问题是您已指定K.max
为 100,但是,您的数据集中只有 8 个观察值。如clusGap
文档中所述,K.max
是
要考虑的最大集群数,因此,在您的情况下,K.max
不能大于七个。
我不清楚在如此小的数据集上聚类是否合适。不过,请参阅下面的工作实现。我已经修改了plot_clusgap
R/Bioconductorphyloseq
包中的函数以可视化结果。
library(data.table)
library(cluster)
library(factoextra) # for hcut function
df <- data.table(properties = c(1,2,3,4,5,6,7,8),
latitude = c(-24.779225, -24.789635, -24.763461, -24.794394, -24.747102,-24.781307,-24.761081,-24.761084),
longitude = c(-49.934816, -49.922324, -49.911616, -49.906262, -49.890796,-49.8875254,-49.8875254,-49.922244),
waste = c(526, 350, 526, 469, 285, 433, 456,825))
df <- scale(df)
# perform clustering, B = 500 is recommended
hcluster = clusGap(df, FUN = hcut, K.max = 7, B = 500)
# extract results
dat <- data.table(hcluster$Tab)
dat[, k := .I]
# visualize gap statistic
p <- ggplot(dat, aes(k, gap)) + geom_line() + geom_point(size = 3) +
geom_errorbar(aes(ymax = gap + SE.sim, ymin = gap - SE.sim), width = 0.25) +
ggtitle("Clustering Results") +
labs(x = "Number of Clusters", y = "Gap Statistic") +
theme(plot.title = element_text(size = 16, hjust = 0.5, face = "bold"),
axis.title = element_text(size = 12, face = "bold"))
这是结果图:
我应该注意到所有的差距统计值都是负数。这表明最佳聚类数是k = 1(即没有聚类)。
推荐阅读
- javascript - 尽管启用了 cors,但 Express js cors 预检请求未成功
- javascript - 链式 JS 类中的解构参数
- xcode - 如何在swiftui中自动向下滚动
- java - 如果枚举仅在一侧,则使用 == 进行比较
- php - 将数组从 js 发送到 php 到 SQL 数据库
- android - VS Code/Flutter/Android - 密钥库文件未设置用于签署配置版本
- pandas - 从 Pandas 中的 Pickled DataFrame 加载单个系列
- c# - 上一个任务完成后调用异步方法
- r - 如何使用 LaTeX 替代 {} 以将 R/exams 完形填空问题导入带有 LaTeX 代码的 Moodle?
- c - 在不链接 c 标准库的情况下使用编译器内置函数