[英]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.