Runs a stochastic community detection algorithm multiple times and finds consensus communities via co-occurrence matrix thresholding. This approach produces more robust and stable community assignments than single runs.
Arguments
- x
Network input: matrix, igraph, network, cograph_network, or tna object
- method
Community detection algorithm to use. Default "louvain". Must be a stochastic method (louvain, leiden, infomap, label_propagation, spinglass).
- n_runs
Number of times to run the algorithm. Default 100.
- threshold
Co-occurrence threshold for consensus. Default 0.5. Nodes that appear together in >= threshold proportion of runs are placed in the same community.
- seed
Optional seed for reproducibility. If provided, the RNG state is initialized once before repeated runs and restored on exit.
- ...
Currently ignored. Each run calls the underlying
igraph::cluster_*()function with its own defaults; no extra arguments are forwarded.
Value
A cograph_communities data frame (columns node and
community) holding the consensus membership. Its
"algorithm" attribute is "consensus_<method>" and its
"modularity" attribute is that of the final walktrap partition of
the consensus graph, not of the original network.
Details
The algorithm works as follows:
Run the specified algorithm
n_runstimes using the current RNG streamBuild a co-occurrence matrix counting how often each pair of nodes appears in the same community
Normalize to proportions (0-1)
Threshold to create a consensus graph (edge if co-occurrence >= threshold)
Run walktrap on the consensus graph to get final communities
References
Lancichinetti, A., & Fortunato, S. (2012). Consensus clustering in complex networks. Scientific Reports, 2, 336.
Examples
if (requireNamespace("igraph", quietly = TRUE)) {
g <- igraph::make_graph("Zachary")
# Consensus from 50 Louvain runs
cc <- community_consensus(g, method = "louvain", n_runs = 50)
print(cc)
# Stricter threshold for more robust communities
cc2 <- community_consensus(g, threshold = 0.7, n_runs = 100)
}
#> Community structure (consensus_louvain)
#> Nodes: 34 | Communities: 4 | Modularity: 0.6409
#> Sizes: 12, 11, 5, 6
#>
#> node community
#> 1 1
#> 2 1
#> 3 1
#> 4 1
#> 5 3
#> 6 3
#> 7 3
#> 8 1
#> 9 2
#> 10 1
#> 11 3
#> 12 1
#> 13 1
#> 14 1
#> 15 2
#> 16 2
#> 17 3
#> 18 1
#> 19 2
#> 20 1
#> 21 2
#> 22 1
#> 23 2
#> 24 4
#> 25 4
#> 26 4
#> 27 2
#> 28 4
#> 29 4
#> 30 2
#> 31 2
#> 32 4
#> 33 2
#> 34 2
