Skip to contents

General function to apply community detection algorithms available in igraph. Follows the EGAnet approach of setting singleton and disconnected nodes to missing (NA)

Usage

community.detection(
  network,
  algorithm = c("edge_betweenness", "fast_greedy", "fluid", "infomap", "label_prop",
    "leading_eigen", "leiden", "louvain", "optimal", "spinglass", "walktrap"),
  allow.singleton = FALSE,
  membership.only = TRUE,
  ...
)

Arguments

network

Matrix or igraph network object

algorithm

Character or igraph cluster_* function (length = 1). Available options:

allow.singleton

Boolean (length = 1). Whether singleton or single node communities should be allowed. Defaults to FALSE. When FALSE, singleton communities will be set to missing (NA); otherwise, when TRUE, singleton communities will be allowed

membership.only

Boolean (length = 1). Whether the memberships only should be output. Defaults to TRUE. Set to FALSE to obtain all output for the community detection algorithm

...

Additional arguments to be passed on to igraph's community detection functions (see algorithm for link to arguments of each algorithm)

Value

Returns memberships from a community detection algorithm

References

Csardi, G., & Nepusz, T. (2006). The igraph software package for complex network research. InterJournal, Complex Systems, 1695.

Author

Hudson Golino <hfg9s at virginia.edu> and Alexander P. Christensen <alexpaulchristensen@gmail.com>

Examples

# Load data
wmt <- wmt2[,7:24]

# Estimate network
network <- EBICglasso.qgraph(data = wmt)

# Compute Edge Betweenness
community.detection(network, algorithm = "edge_betweenness")
#> Warning: At vendor/cigraph/src/community/edge_betweenness.c:503 : Membership vector will be selected based on the highest modularity score.
#> Algorithm:  Edge Betweenness
#> 
#> Number of communities:  4
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1    NA     2     2     2     2     3     1     4     3     2     1 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     4     2     1     2     3 

# Compute Fast Greedy
community.detection(network, algorithm = "fast_greedy")
#> Algorithm:  Fast-greedy
#> 
#> Number of communities:  3
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     2     3     2     2 

# Compute Fluid
community.detection(
  network, algorithm = "fluid",
  no.of.communities = 2 # needs to be set
)
#> Algorithm:  Fluid
#> 
#> Number of communities:  2
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     1     2     1     1     2     2 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     2     2     1     1     1 

# Compute Infomap
community.detection(network, algorithm = "infomap")
#> Algorithm:  Infomap
#> 
#> Number of communities:  1
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     1     1     1     1     1     1     1     1 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     1     1     1     1     1 

# Compute Label Propagation
community.detection(network, algorithm = "label_prop")
#> Algorithm:  Label Propagation
#> 
#> Number of communities:  1
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     1     1     1     1     1     1     1     1 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     1     1     1     1     1 

# Compute Leading Eigenvector
community.detection(network, algorithm = "leading_eigen")
#> Algorithm:  Leading Eigenvector
#> 
#> Number of communities:  3
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     1     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     3     3     2     2 

# Compute Leiden (with modularity)
community.detection(
  network, algorithm = "leiden",
  objective_function = "modularity"
)
#> Algorithm:  Leiden with Modularity
#> 
#> Number of communities:  3
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     2     3     2     2 

# Compute Leiden (with CPM)
community.detection(
  network, algorithm = "leiden",
  objective_function = "CPM",
  resolution_parameter = 0.05 # "edge density"
)
#> Algorithm:  Leiden with Constant Potts Model
#> 
#> Number of communities:  4
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     2     3     4     4 

# Compute Louvain
community.detection(network, algorithm = "louvain")
#> Algorithm:  Louvain
#> 
#> Number of communities:  3
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     2     3     2     2 

# Compute Optimal (identifies maximum modularity solution)
community.detection(network, algorithm = "optimal")
#> Algorithm:  Optimal
#> 
#> Number of communities:  3
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     2     2     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     2     3     2     2 

# Compute Spinglass
community.detection(network, algorithm = "spinglass")
#> Algorithm:  Spinglass
#> 
#> Number of communities:  5
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     3     3     4     4     3 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     3     4     3     5     5 

# Compute Walktrap
community.detection(network, algorithm = "walktrap")
#> Algorithm:  Walktrap
#> 
#> Number of communities:  2
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     2     2     2     2     2 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     2     2     2     2     2 

# Example with {igraph} network
community.detection(
  convert2igraph(network), algorithm = "walktrap"
)
#> Algorithm:  Walktrap
#> 
#> Number of communities:  2
#> 
#>  wmt1  wmt2  wmt3  wmt4  wmt5  wmt6  wmt7  wmt8  wmt9 wmt10 wmt11 wmt12 wmt13 
#>     1     1     1     1     1     2     2     2     2     2     2     2     2 
#> wmt14 wmt15 wmt16 wmt17 wmt18 
#>     2     2     2     2     2