1、距離計算
## method for class 'TermDocumentMatrix' dissimilarity(x, y = NULL, method)
## method for class 'PlainTextDocument' dissimilarity(x, y = NULL, method)
參數說明:
x:文檔-詞矩陣或者文本文檔;
y:文本文檔,僅當x是文本文檔時y才是文本文檔;
method:距離計算方法,所有的method均來自於proxy包,proxy包的文檔在這里。
method的種類主要有:
(1)jaccard:jaccard系數(默認方法),又叫做Jaccard相似性系數,用來比較樣本集中的相似性和分散性的一個概率。
Jaccard = 樣本集交集個數 / 樣本集合集個數;
(2)Matching:Simple Matching Coefficient,SMC(簡單匹配系數)。該度量同等地對出現和不出現計數。
如下圖所示.其中,q 表示向量中對應位置都為 1 的元素個數;t 表示向量中對應位置都為 0 的元素個數;s 表示向量 i 某
個位置為 1,而在向量 j 的對應位置為 0 的元素個數;r 表示在向量 i 某個位置為 0,而在向量 j 的對應位置為 1 的
元素個數.來自(http://www.jos.org.cn/1000-9825/17/79.pdf)
(3)dice:Dice's coefficient 類似於jaccard系數,但是在分子上加權,Dice系數S為
(4)affinity:
Affinity between the two items i and j is defined by Aggarwal et al. (2002) as
A(i,j) = sup({i,j})/(sup({i}) + sup({j}) - sup({i,j})),
where sup(.) is the support measure. This means that affinity is the Jaccard similarity between items.
( 暫時還沒搞明白)
(5) cosine:余弦相似度,計算兩個向量間的余弦夾角
(6)pearson: pearson Correlation Coefficient皮爾遜相關系數,見:http://zh.wikipedia.org/zh-cn/%E7%9B%B8%E5%85%B3
例子:
data("crude") tdm <- TermDocumentMatrix(crude) dissimilarity(tdm, method = "cosine") #計算距離的方法為cosine(余弦夾角)方法 dissimilarity(crude[[1]], crude[[2]], method = "eJaccard") #dJaccard方法
2、層次聚類
hclust(d, method = "complete", members=NULL)
參數說明:
d:聚類的矩陣
method:聚類的方法
(1)ward:方法簡述:基於方差分析思想,如果分類合理,則同類樣品間離差平方和應當較小,類與類間離差平方和應當較大。
(2)single:最短距離法,計算兩類觀測間最近一對的距離;
(3)complete:最長距離法,計算兩類觀測間最遠一對的距離;
(4)average:平均距離法,測量兩類每對觀測間的平均距離;
(5)mcquitty:McQuitty相似分析法,(待補充)
(6)median:中間距離法,
(7)centroid:重心法,兩類間的距離定義為兩類重心之間的距離,對樣品分類而言,每一類中心就是屬於該類樣品的均值。
特點:該距離隨聚類地進行不斷縮小。該法的譜系樹狀圖很難跟蹤,且符號改變頻繁,計算較煩。
上述部分內容來自於:http://blog.csdn.net/yillc/article/details/6746509
3、圖形展示
plot(x, labels = NULL, hang = 0.1, axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "Cluster Dendrogram", sub = NULL, xlab = NULL, ylab = "Height", ...) plclust(tree, hang = 0.1, unit = FALSE, level = FALSE, hmin = 0, square = TRUE, labels = NULL, plot. = TRUE, axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "", sub = NULL, xlab = NULL, ylab = "Height")