Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“模块化”(modularity)的正确使用和解释`_R_Igraph_Modularity - Fatal编程技术网

“模块化”(modularity)的正确使用和解释`

“模块化”(modularity)的正确使用和解释`,r,igraph,modularity,R,Igraph,Modularity,在igraph模块化部分,给出了如下示例代码: g <- graph.full(5) %du% graph.full(5) %du% graph.full(5) g <- add.edges(g, c(1,6, 1,11, 6, 11)) wtc <- walktrap.community(g) modularity(wtc) #[1] 0.5757575 modularity(g, membership(wtc)) #[1] 0.5757576 我被不同的部分弄糊涂了: m

在igraph
模块化
部分,给出了如下示例代码:

g <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
g <- add.edges(g, c(1,6, 1,11, 6, 11))
wtc <- walktrap.community(g)
modularity(wtc)
#[1] 0.5757575
modularity(g, membership(wtc))
#[1] 0.5757576
我被不同的部分弄糊涂了:

modularity(wtc)
# and
modularity(g, membership(wtc))
wtc
本身已经有了最好的分割和相关的模块化。为什么在
wtc
上调用
modularity
<代码>模块化(g,成员资格(wtc))我看到的是找到一个特定的预先选择的拆分的模块化,这对我来说更有意义(在这种情况下是最佳拆分)

在什么情况下,您希望这些结果会有所不同,以及为什么

g2 <- structure(list(from = structure(c(2L, 3L, 4L, 1L, 3L, 4L, 1L, 
  2L, 4L, 1L, 2L, 3L), .Label = c("A", "B", "C", "D"), class = "factor"), 
      to = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
      4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
      weight = c(2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L
      )), .Names = c("from", "to", "weight"), row.names = c(2L, 
  3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L), class = "data.frame")

g2 <- graph.data.frame(g2, vertices = unique(g2[1]))

set.seed(444)
wtc2 <- walktrap.community(g2)
modularity(wtc2)
# [1] 0.4444444
wtc2
# Graph community structure calculated with the walktrap algorithm
# Number of communities (best split): 2 
# Modularity (best split): 0.4444444 
# Membership vector:
# B C D A 
# 2 1 1 2 
modularity(g2, membership(wtc2))
# [1] -0.1666667

sessionInfo()
# R version 3.0.2 (2013-09-25)
# Platform: x86_64-apple-darwin10.8.0 (64-bit)
# 
# locale:
# [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] Matrix_1.0-14   lattice_0.20-23 igraph_0.6.6    reshape2_1.2.2  ggplot2_0.9.3.1
# 
# loaded via a namespace (and not attached):
#  [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.3       grid_3.0.2         gtable_0.1.2       labeling_0.2      
#  [7] MASS_7.3-29        munsell_0.4.2      plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 scales_0.2.3      
# [13] stringr_0.6.2      tools_3.0.2       

g2
模块化(图形,分割)
在您的igraph版本中不支持边权重,因此存在差异。在这种情况下,基本上假设所有边的权重都为1。

它们不应该不同,对我来说它们没有区别。请通过包含一个随机种子(Walktrap是一个随机算法)以及版本和平台信息,使您的示例具有可复制性。@GaborCsardi hey感谢您检查这一点,因此它们应该没有区别,如果是这样的话,那么我看到
模块化(wtc2)
max(wtc2$modularity)
是一样的吗?如果是这样的话,我的问题得到了回答,但结果之间的差异显然让我感到困惑。尽管如此,我想我得到的结果中还是有一些有趣的地方。注意,在igraph
1.2.5
上,这些方法对于图
g2是不同的
g2 <- structure(list(from = structure(c(2L, 3L, 4L, 1L, 3L, 4L, 1L, 
  2L, 4L, 1L, 2L, 3L), .Label = c("A", "B", "C", "D"), class = "factor"), 
      to = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
      4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
      weight = c(2L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L
      )), .Names = c("from", "to", "weight"), row.names = c(2L, 
  3L, 4L, 5L, 7L, 8L, 9L, 10L, 12L, 13L, 14L, 15L), class = "data.frame")

g2 <- graph.data.frame(g2, vertices = unique(g2[1]))

set.seed(444)
wtc2 <- walktrap.community(g2)
modularity(wtc2)
# [1] 0.4444444
wtc2
# Graph community structure calculated with the walktrap algorithm
# Number of communities (best split): 2 
# Modularity (best split): 0.4444444 
# Membership vector:
# B C D A 
# 2 1 1 2 
modularity(g2, membership(wtc2))
# [1] -0.1666667

sessionInfo()
# R version 3.0.2 (2013-09-25)
# Platform: x86_64-apple-darwin10.8.0 (64-bit)
# 
# locale:
# [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] Matrix_1.0-14   lattice_0.20-23 igraph_0.6.6    reshape2_1.2.2  ggplot2_0.9.3.1
# 
# loaded via a namespace (and not attached):
#  [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.3       grid_3.0.2         gtable_0.1.2       labeling_0.2      
#  [7] MASS_7.3-29        munsell_0.4.2      plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 scales_0.2.3      
# [13] stringr_0.6.2      tools_3.0.2