2012-08-13 18 views
6

我用下述R代碼來生成樹形圖(見附圖)配有標籤基於佔美娜序列:顯示佔美娜(R)系統樹圖中的文本/表格式

library(TraMineR) 
library(cluster) 
clusterward <- agnes(twitter.om, diss = TRUE, method = "ward") 
plot(clusterward, which.plots = 2, labels=colnames(twitter_sequences)) 

完整的代碼(包括數據集)可以發現here

由於樹形圖的信息是圖形化的,所以在文本和/或表格格式中獲取相同的信息會很方便。如果我稱之爲聚合對象的任何方面(由agnes創建),比如「order」或「merge」,我會使用數字而不是從colnames(twitter_sequences)得到的名稱來標記所有內容。另外,我沒有看到如何在樹形圖中輸出以圖形表示的分組。

總結:如何以文本/表格格式獲得羣集輸出,使用R正確顯示標籤並理想地使用traminer/cluster庫?

enter image description here

回答

4

問題涉及cluster包。 (見http://stat.ethz.ch/R-manual/R-devel/library/cluster/html/agnes.object.html)的幫助頁面通過agnes 返回agnes.object指出,這個對象包含order.lab組件「類似order,但含有觀察,而不是標籤觀察的數字。這部分是唯一可用的,如果原來的看法被標記。」

由TraMineR生成的不相似矩陣(您的情況爲twitter.om)當前沒有將序列標籤保留爲行和列名稱。要獲得order.lab組件,您必須手動將序列標籤分配爲twitter.om矩陣的rownamescolnames。我在此用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]] 

希望這會有所幫助。

+0

非常感謝!這非常有幫助! – histelheim 2012-08-14 22:25:59