【问题标题】:Cutting a graph in R在 R 中切割图形
【发布时间】:2013-08-08 20:31:50
【问题描述】:

我有以下简单的问题。我有一个用于多个节点的距离矩阵,并且我想获取该节点的子集列表,以便在每个子集中,每两个节点的最小距离为 dmin。也就是说,最初每两个节点都由具有关联值的边连接。我想删除值小于 dmin 的每条边,并列出所有生成的断开连接的图。

本质上,我想获得彼此非常接近的数据点集群,而不是使用聚类算法,而是使用距离的阈值。

我的问题自然是如何在 R 中完成它。考虑以下矩阵 m:

    a   b   c   d
a 1.0 0.9 0.2 0.3
b 0.9 1.0 0.4 0.1
c 0.2 0.4 1.0 0.7
d 0.3 0.1 0.7 1.0

有四个节点(a、b、c、d)。我搜索给定该矩阵(实际上是 1 - 距离矩阵)和阈值 dmin 的函数或包,例如 dmin <- 0.5,将产生两组:{a,b}{c,d}。一种非常低效的实现方式如下:

clusters <- list()
nodes <- colnames( m )
dmin <- 0.5

# loop over nodes
for( n in nodes ) {

  found <- FALSE
  # check whether a node can be associated to one of the existing
  # clusters
  for( c in names( clusters ) ) {
    if( any( m[ n, clusters[[c]] ] > 0.5 ) ) {
      clusters[[c]] <- c( clusters[[c]], n )
      found <- TRUE
      next
    }
  }

  # no luck? create a new cluster for that node
  if( ! found )
    clusters[[n]] <- c( n )
} 

结果是

> clusters
$a
[1] "a" "b"

$c
[1] "c" "d"

【问题讨论】:

  • 你的问题是?也许制作reproducible example 是个好主意。
  • 您的问题不清楚:在第一段中,您要求彼此相距较远的点的子集(“在最小距离dmin” - 它是graph colouring problem,对于其边的长度最多为dmin),但在第二个中,您要求“彼此靠近的点簇”。
  • 我很抱歉造成这种混乱。 @VincentZoonekynd:是的,我认为是图形着色问题。第二种表述并不准确。混淆是因为矩阵 m 的元素是 1 - 距离(实际上,1 - cor( y )^2 其中 y 包含每个节点的一行测量值)。我想通过切割图表来找到高度相关的节点组。是的,还有其他聚类方法,我正在使用它们,但我也想尝试一下。
  • @Thomas 我按照你的建议做了。

标签: r graph cluster-analysis


【解决方案1】:

从您的相似度矩阵m, 您可以将邻接矩阵构建为m &gt; .5, 构造对应的图 使用igraph 包 并提取其连通分量。

m <- matrix(c(10,9,2,3, 9,10,4,1, 2,4,10,7, 3,1,7,10), 4, 4)/10
colnames(m) <- rownames(m) <- letters[1:4]
library(igraph)
g <- graph.adjacency( m > .5 )
plot(g)
clusters(g)$membership
# [1] 1 1 2 2
tapply(colnames(m), clusters(g)$membership, c)
# $`1`
# [1] "a" "b"
# $`2`
# [1] "c" "d"

【讨论】:

  • 是的!而已。谢谢你的耐心。作为一名生物学家,我经常发现自己缺乏正确的术语(否则我可以用谷歌搜索...)
猜你喜欢
  • 1970-01-01
  • 2015-10-02
  • 2017-04-27
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多