fviz_cluster()不接受k-medoid(PAM)结果 [英] fviz_cluster() not accepting for k-medoid (PAM) results
问题描述
尝试使用 fviz_cluster()
可视化k-medoid(PAM)簇结果,但是函数不接受它们.
Trying to visualize k-medoid (PAM) cluster results with fviz_cluster()
, however function isn't accepting them.
它在?fviz_clust
内声明"object arguments =类" partition"的对象;由群集包中的 pam()
, clara()
或 fanny()
函数创建"
It states within ?fviz_clust
"object argument = an object of class "partition" created by the functions pam()
, clara()
or fanny()
in cluster package"
我尝试通过其他方式访问聚类向量;
I've tried accessing the clustering vector through other means;
pam_gower_2$clustering
pam_gower_2[[3]]
但是然后我得到一个单独的错误:
but then I get a separate error:
错误:$运算符对于原子向量无效"
Error: $ operator is invalid for atomic vectors"
pam_gower_2的类是分区吗?正如争论所期望的那样.
The class of pam_gower_2 is partition? As the argument expects.
class(pam_gower_2)
> class(pam_gower_2)
[1] "pam" "partition"
这是我正在使用的代码:
Here's the code I'm using:
df_gower <- df[, c(2:21)]
df_gower <- df_gower[, c(1:4, 11:12, 14:15, 5:10, 16:20)]
gower_dist <- daisy(df_gower, metric="gower", type=list(ordratio=c(2:4, 6), symm=c(7:8), asymm=c(5), logratio=c(13)))
gower_mat <- as.matrix(gower_dist)
tendency_gower <- get_clust_tendency(gower_mat, 100, graph=T)
tendency_gower$hopkins_stat
fviz_nbclust(gower_mat, pam, method="wss")
fviz_nbclust(gower_mat, pam, method="silhouette")
pam_gower_2 <- pam(gower_mat, k=2, diss=T)
# all of the above functions as expected
fviz_cluster(pam_gower_2, gower_mat)
以上行会产生以下错误:
above line produces the following error:
如果(!is.null(names(x)))list(names(x),:数据"必须是矢量类型,为"NULL"
Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), :'data' must be of a vector type, was 'NULL'
将非常感谢反馈/修复,为何不起作用的原因或可视化的替代方法.
Would greatly appreciate feedback/ fix, reasons as to why this doesn't work, or an alternative method for visualizing.
谢谢:)
推荐答案
以下是 fviz_cluster
的文档:
data:已用于集群的数据.仅当object是kmeans或dbscan类时才需要.
data: the data that has been used for clustering. Required only when object is a class of kmeans or dbscan.
因此,您只需要将 pam
的结果传递给 fviz_cluster
.
You therefore only need to pass the results of pam
to fviz_cluster
.
这是 fviz_cluster
和 pam
的最小示例:
library("factoextra")
library("cluster")
data("USArrests")
res <- pam(USArrests, 4)
fviz_cluster(res)
如果将 pam
应用于距离矩阵,则会出现错误.一种解决方法是事后设置结果的 data
字段.这是使用距离矩阵( diss
)的修改示例:
If you apply pam
with a distance matrix, you have your error. A workaround is to set the data
field of the result afterwards. Here is the modified example using a distance matrix (diss
):
library("factoextra")
library("cluster")
data("USArrests")
diss = dist(USArrests)
res <- pam(diss, 4)
res$data = USArrests
fviz_cluster(res)
这篇关于fviz_cluster()不接受k-medoid(PAM)结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!