the correct use and interpretation of `modularity()`

[亡魂溺海] 提交于 2020-01-16 03:55:08

问题


In the igraph ?modularity section there is example code given as

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

the output of wtc shows:

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

I am confused by the different parts:

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

wtc itself already has the best split and its associated modularity. why call modularity on wtc? modularity(g, membership(wtc)) I see is finding the modularity of a particular pre chosen split, which makes more sense to me (in this case the best split).

In what cases would you expect these results to differ and why e.g.

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       

回答1:


modularity(graph, split) does not support edge weights in your version of igraph, hence the difference. Basically all edges are assumed to have weight 1 in this case.



来源:https://stackoverflow.com/questions/20313608/the-correct-use-and-interpretation-of-modularity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!