R中的巨型组件

时间:2013-06-09 16:06:06

标签: networking igraph

我需要计算我在R中创建的网络的一个巨大组件,但是在此站点上给出的代码(http://lists.gnu.org/archive/html/igraph-help/2009-08/msg00064.html)会导致奇怪的输出。我需要巨大的组件来计算亲密度(根据大学老师的说法)

giant.component <- function(graph) {
cl <- clusters(graph)
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)-1)-1)}
G <- giant.component(as.igraph(Q))

结果:

IGRAPH U-W- 0 0 -- 
+ attr: frame.color (v/c), label.color (v/c), label (v/c), shape
(v/c), color (v/c), size (v/n), size2 (v/n), weight (e/n),
arrow.mode (e/n), curved (e/n), color (e/c), label (e/c), lty
(e/n), width (e/n)

Csardi建议的代码调整会产生以下代码和结果:

model5 <- matrix(c(0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), 5, 5, byrow = TRUE) 
C <- qgraph(model5, labels = c("X1", "X2", "X3", "X4", "X5"), vsize = 20, esize = 10, asize = 10, layout = "circle") 

giant.component <- function(graph) { 
cl <- clusters(graph) 
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)))} 
G <- giant.component(as.igraph(C)) 
G <- qgraph(C)

IGRAPH D-W- 5 7 -- 
+ attr: frame.color (v/c), label.color (v/c), label (v/c), shape
(v/c), color (v/c), size (v/n), size2 (v/n), weight (e/n),
arrow.mode (e/n), curved (e/n), color (e/c), lty (e/n), width (e/n) 

1 个答案:

答案 0 :(得分:5)

较旧(前0.6)版本的igraph索引顶点(以及其他所有内容)从零开始,而较新版本使用基于1的索引。因此,您需要将代码更新为:

...
induced.subgraph(graph, which(cl$membership == which.max(cl$csize)))}
...

(未经测试,因为您没有提供示例数据集。)