R 中 clusplot 时出错

问题描述 投票:0回答:2

在使用 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)

显示的错误是

mkCheckX(x, diss) 中的错误:x 不是数据矩阵

矩阵 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")
    
r cluster-analysis
2个回答
2
投票

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 的矩阵,所以你得到了你看到的错误。


0
投票
在使用 R 通过mutualcluster 执行聚类时,我遇到了一个错误。

相互.集群

1<-mutualCluster(jarakeuclid, method="average", plot=T) Error in if (class(dmat) == "dist") dmat <- as.matrix(dmat) : the condition has length >

© www.soinside.com 2019 - 2024. All rights reserved.