r - `modularity()`的正确使用和解释

标签 r igraph modularity

igraph ?modularity 部分给出了示例代码

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

wtc 的输出显示:

wtc
#Graph community structure calculated with the walktrap algorithm
#Number of communities (best split): 3 
#Modularity (best split): 0.5757575 
#Membership vector:
# [1] 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2

我对不同的部分感到困惑:

modularity(wtc)
# and
modularity(g, membership(wtc))

wtc 本身已经有了最好的拆分及其相关的模块化。为什么要在 wtc 上调用 modularitymodularity(g, membership(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       

最佳答案

modularity(graph, split) 在您的 igraph 版本中不支持边权重,因此存在差异。在这种情况下,基本上假定所有边的权重都为 1。

关于r - `modularity()`的正确使用和解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20313608/

相关文章:

r - 如何根据 R 中的位置向量替换字符串向量中的字符?

r - 如何在 brms::marginal_effects() 生成的图中指定 y 限制?

R igraph 部分填充的顶点

r - 计算每对列的相同行值以创建网络图

java - Clauset-Newman-Moore 社区检测实现

r - 使用 dplyr 获取相对频率百分比的更智能方法?

r - 如何将 pajek .paj 文件导入到 R 的 igraph 中?

c - 模块化出了问题

c++ - 用于模块化 C++ 框架的 CMAKE

r - R中的偏回归图