是否应该使用R中的距离(不相似性)或相似性进行聚类?

Der*_*ang 1 r cluster-analysis

我正在解决集群问题,proxy R中的 软件包提供dist和simil函数.

为了我的目的,我需要一个距离矩阵,所以我最初使用dist,这是代码:

distanceMatrix <- dist(dfm[,-1], method='Pearson')
clusters <- hclust(distanceMatrix)  
clusters$labels <- dfm[,1]#colnames(dfm)[-1]
plot(clusters, labels=clusters$labels)
Run Code Online (Sandbox Code Playgroud)

但在我绘制图像后,我发现聚类结果不是我预期的方式,因为我知道它应该是什么样子.

所以我尝试了simil,代码如下:

distanceMatrix <- simil(dfm[,-1], method='Pearson')
clusters <- hclust(pr_simil2dist(distanceMatrix))   
clusters$labels <- dfm[,1]#colnames(dfm)[-1]
plot(clusters, labels=clusters$labels)
Run Code Online (Sandbox Code Playgroud)

此代码使用simil计算相似度矩阵,然后使用pr_simil2dist将其转换为距离矩阵,然后绘制它并得到我预期的结果!

我对dist和simil之间的关系感到困惑.根据文档中描述的关系,两个代码片段不应该有相同的结果吗?

我哪里错了?

编辑:

您可以使用以下值的dfm尝试我的代码,对不起的缩进很抱歉.

                             Blog china kids music yahoo want wrong
                         Gawker     0    1     0     0    7     0
                  Read/WriteWeb     2    0     1     3    1     1
                 WWdN: In Exile     0    2     4     0    0     0
           ProBlogger Blog Tips     0    0     0     0    2     0
                    Seth's Blog     0    0     1     0    3     1
 The Huffington Post | Raw Feed     0    6     0     0   14     5
Run Code Online (Sandbox Code Playgroud)

编辑:

实际上,样本数据来自一个非常大的数据框tail,使用dist和simil + pr_simil2dist得到完全不同的矩阵.完整的数据可以在这里找到.

如果我犯了其他愚蠢的错误,这里是我的函数的完整代码:

我用来读取数据的代码:

dfm<- read.table(filename, header=T, sep='\t', quote='')
Run Code Online (Sandbox Code Playgroud)

群集代码:

hcluster <- function(dfm, distance='Pearson'){
    dfm <- tail(dfm)[,c(1:7)] # I use this to give the sample data.
    distanceMatrix <- simil(dfm[,-1], method=pearson)
    clusters <- hclust(pr_simil2dist(distanceMatrix))   
    clusters$labels <- dfm[,1]#colnames(dfm)[-1]
    plot(clusters, labels=clusters$labels)
}
Run Code Online (Sandbox Code Playgroud)

使用dist的矩阵:

           94         95         96         97         98
95 -0.2531580                                            
96 -0.2556859 -0.4629100                                 
97  0.9897783 -0.1581139 -0.2927700                      
98  0.8742800 -0.2760788 -0.1022397  0.9079594           
99  0.9114339 -0.5020405 -0.2810414  0.8713293  0.8096980
Run Code Online (Sandbox Code Playgroud)

使用simil + pr_simil2dist的矩阵:

           94         95         96         97         98
95 1.25315802                                            
96 1.25568595 1.46291005                                 
97 0.01022173 1.15811388 1.29277002                      
98 0.12572004 1.27607882 1.10223973 0.09204062           
99 0.08856608 1.50204055 1.28104139 0.12867065 0.19030202
Run Code Online (Sandbox Code Playgroud)

你可以看到两个矩阵中的相应元素加起来为1,我认为这是不对的.所以一定有一些我做错了.

编辑:

在read.table函数中指定名称以读取数据框后,dist方式和simil + pr_simil2dist方式给出相同的正确结果.所以技术问题解决了,但我不知道为什么我原来处理数据框的方式与dist和simil有关.

任何人都有这方面的线索?

Rei*_*son 5

我不确定你的意思不是按照预期的.如果我通过proxy::dist()或通过计算距离/相似度矩阵simil()并转换为相异度,我得到相同的矩阵:

> dist(dfm, method='Pearson')
                                  Gawker Read/WriteWeb WWdN: In Exile ProBlogger Blog Tips Seth's Blog
Read/WriteWeb                  0.2662006                                                              
WWdN: In Exile                 0.2822594     0.2662006                                                
ProBlogger Blog Tips           0.2928932     0.5917517      0.6984887                                 
Seth's Blog                    0.2662006     0.2928932      0.4072510            0.2928932            
The Huffington Post | Raw Feed 0.1835034     0.2312939      0.2662006            0.2928932   0.2312939

> pr_simil2dist(simil(dfm, method = "pearson"))
                                  Gawker Read/WriteWeb WWdN: In Exile ProBlogger Blog Tips Seth's Blog
Read/WriteWeb                  0.2662006                                                              
WWdN: In Exile                 0.2822594     0.2662006                                                
ProBlogger Blog Tips           0.2928932     0.5917517      0.6984887                                 
Seth's Blog                    0.2662006     0.2928932      0.4072510            0.2928932            
The Huffington Post | Raw Feed 0.1835034     0.2312939      0.2662006            0.2928932   0.2312939
Run Code Online (Sandbox Code Playgroud)

d1 <- dist(dfm, method='Pearson')
d2 <- pr_simil2dist(simil(dfm, method = "pearson"))
h1 <- hclust(d1)
h2 <- hclust(d2)
layout(matrix(1:2, ncol = 2))
plot(h1)
plot(h2)
layout(1)
all.equal(h1, h2)
Run Code Online (Sandbox Code Playgroud)

最后一行产生:

> all.equal(h1, h2)
[1] "Component 6: target, current do not match when deparsed"
Run Code Online (Sandbox Code Playgroud)

这告诉我们,h1h2是完全除匹配函数调用(如明显我们使用相同的d1d2在相应的调用).

产生的数字是:

在此输入图像描述

如果正确设置对象,则无需使用标签.查看row.names参数以read.table()查看如何在读入数据时将列用作行标签.

所有这一切都是使用:

dfm <- structure(list(china = c(0L, 2L, 0L, 0L, 0L, 0L), kids = c(1L, 
0L, 2L, 0L, 0L, 6L), music = c(0L, 1L, 4L, 0L, 1L, 0L), yahoo = c(0L, 
3L, 0L, 0L, 0L, 0L), want = c(7L, 1L, 0L, 2L, 3L, 14L), wrong = c(0L, 
1L, 0L, 0L, 1L, 5L)), .Names = c("china", "kids", "music", "yahoo", 
"want", "wrong"), class = "data.frame", row.names = c("Gawker", 
"Read/WriteWeb", "WWdN: In Exile", "ProBlogger Blog Tips", "Seth's Blog", 
"The Huffington Post | Raw Feed"))
Run Code Online (Sandbox Code Playgroud)