r - R 中的 clusplot 期间出错

标签 r cluster-analysis

在使用 R 执行聚类时,我遇到了一个错误。我有一个数据集d,它是一个距离矩阵。变量fit通过以下方式获得

fit <- kmeans(d,k=2) # assume that number of cluster lie between 1 and nrow(x)
clusplot(d, fit$cluster, color=TRUE, shade = TRUE, lines=0)

显示的错误是

Error in mkCheckX(x, diss) : x is not a data matrix

矩阵 d 由下式给出

structure(c(2, 4, 6, 2, 4, 2), Size = 4L, Diag = FALSE, Upper = FALSE,
          method = "euclidean", call = dist(x = DATA, method = "euclidean"),
          class = "dist")

最佳答案

clusplot 函数接受其第一个参数为矩阵或数据框,或相异矩阵(或距离矩阵),具体取决于 diss 的值参数,默认为 FALSE。请参阅 ?clusplot 了解更多信息。

所以,您需要使用:

d = dist(DATA) # for a distance matrix or d = daisy(DATA) for a dissimilarity matrix
clusplot(d, diss=TRUE, fit$cluster, color=TRUE, shade = TRUE, lines=0)

clusplot(DATA, fit$cluster, color=TRUE, shade = TRUE, lines=0)

您收到错误是因为您的矩阵 d 未被函数 mkCheckX 识别为矩阵,因为 R 是类 dist< 的对象 (不是矩阵!)。如果您尝试 is.matrix(d),您应该得到 FALSE

此外,不要期望使用这两种方法得到相同的结果,因为在提供数据矩阵时,聚类是以不同的方式生成的(基于主成分分解,请查看代码)。

如果您查看 dist 的帮助,您可以看到可以使用不同的方法(“euclidean”、“maximum”、“manhattan”、“canberra”、“binary”或“minkowski” )来计算距离,并且您应该通过改变计算距离的方式来期望不同的聚类。

总之,你的距离矩阵不是 R 的矩阵,所以你得到了你看到的错误。

关于r - R 中的 clusplot 期间出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20796173/

相关文章:

algorithm - 基于多个变量对用户进行聚类

machine-learning - 尝试MOA流聚类算法denstream.WithDBSCAN时出现java.lang.NullPointerException(如何正确使用它?)

r - 聚类预测

r - 为 DBSCAN (R) 选择 eps 和 minpts?

在 R 中保留 dcast 中的变量

r - geom_map "map_id"函数如何工作?

r - 在 Centos 上安装 rgdal 和 gdal 时遇到问题

python - 偏好值的变化不影响亲和性传播聚类的结果

r - 如何根据另一行重新排列矩阵,data.frame或vector的行

r - 使用 R plotly 的二次回归线