[英]Displaying TraMineR (R) dendrograms in text/table format
我使用以下R代码生成带有基于TraMineR序列的标签的树形图(参见附图):
library(TraMineR)
library(cluster)
clusterward <- agnes(twitter.om, diss = TRUE, method = "ward")
plot(clusterward, which.plots = 2, labels=colnames(twitter_sequences))
完整代码(包括数据集)可以在这里找到。
由于树形图以图形方式提供信息,因此以文本和/或表格格式获取相同信息将非常方便。 如果我调用对象clusterward的任何方面(由agnes创建),例如“order”或“merge”,我会使用数字标记所有内容,而不是从colnames(twitter_sequences)
获得的名称。 另外,我看不出如何输出树形图中以图形方式表示的分组。
总结一下: 如何使用R以及理想情况下的电车/集群库正确显示标签,以文本/表格格式获取集群输出?
问题涉及cluster
包。 agnes
返回的agnes.object
的帮助页面(参见http://stat.ethz.ch/R-manual/R-devel/library/cluster/html/agnes.object.html )表明该对象包含一个order.lab
组件“类似于order
,但包含观察标签而不是观察数字。此组件仅在原始观察结果被标记时可用。”
TraMineR生成的相异矩阵(在您的情况下为twitter.om
)当前不会将序列标签保留为行名和列名。 要获得order.lab
组件,您必须手动分配序列标签作为两个rownames
和colnames
您的twitter.om
矩阵。 我在这里用TraMineR包提供的mvad
数据进行说明。
library(TraMineR)
data(mvad)
## attaching row labels
rownames(mvad) <- paste("seq",rownames(mvad),sep="")
mvad.seq <- seqdef(mvad[17:86])
## computing the dissimilarity matrix
dist.om <- seqdist(mvad.seq, method = "OM", indel = 1, sm = "TRATE")
## assigning row and column labels
rownames(dist.om) <- rownames(mvad)
colnames(dist.om) <- rownames(mvad)
dist.om[1:6,1:6]
## Hierarchical cluster with agnes library(cluster)
cward <- agnes(dist.om, diss = TRUE, method = "ward")
## here we can see that cward has an order.lab component
attributes(cward)
这是获得order
与序列标签而不是数字。 但现在我不清楚你想要的文本/表格形式的集群结果。 从树形图中,您可以决定要切割它的位置,即所需的组数,并使用cutree
切割树形图,例如cl.4 <- cutree(clusterward1, k = 4)
。 结果cl.4
是具有每个序列的聚类成员资格的向量,您将获得组1的成员列表,例如,使用rownames(mvad.seq)[cl.4==1]
。
或者,您可以使用identify
方法(参见?identify.hclust
)从图中以交互方式选择组,但需要将参数作为as.hclust(cward)
传递。 这是示例的代码
## plot the dendrogram
plot(cward, which.plot = 2, labels=FALSE)
## and select the groups manually from the plot
x <- identify(as.hclust(cward)) ## Terminate with second mouse button
## number of groups selected
length(x)
## list of members of the first group
x[[1]]
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.