% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/communities.R
\name{community_consensus}
\alias{community_consensus}
\alias{com_consensus}
\title{Consensus Community Detection}
\usage{
community_consensus(
  x,
  method = c("louvain", "leiden", "infomap", "label_propagation", "spinglass"),
  n_runs = 100,
  threshold = 0.5,
  seed = NULL,
  ...
)

com_consensus(
  x,
  method = c("louvain", "leiden", "infomap", "label_propagation", "spinglass"),
  n_runs = 100,
  threshold = 0.5,
  seed = NULL,
  ...
)
}
\arguments{
\item{x}{Network input: matrix, igraph, network, cograph_network, or tna object}

\item{method}{Community detection algorithm to use. Default "louvain".
Must be a stochastic method (louvain, leiden, infomap, label_propagation,
spinglass).}

\item{n_runs}{Number of times to run the algorithm. Default 100.}

\item{threshold}{Co-occurrence threshold for consensus. Default 0.5.
Nodes that appear together in >= threshold proportion of runs are
placed in the same community.}

\item{seed}{Optional seed for reproducibility. If provided, the RNG state is
initialized once before repeated runs.}

\item{...}{Additional arguments passed to the community detection method.}
}
\value{
A \code{cograph_communities} object with consensus membership.
}
\description{
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.
}
\details{
The algorithm works as follows:
\enumerate{
\item Run the specified algorithm \code{n_runs} times using the current RNG
stream
\item Build a co-occurrence matrix counting how often each pair of nodes
appears in the same community
\item Normalize to proportions (0-1)
\item Threshold to create a consensus graph (edge if co-occurrence >= threshold)
\item Run walktrap on the consensus graph to get final communities
}
}
\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)
}
}
\references{
Lancichinetti, A., & Fortunato, S. (2012).
Consensus clustering in complex networks.
\emph{Scientific Reports}, 2, 336.
}
\seealso{
\code{\link{communities}}, \code{\link{community_louvain}}
}
