R/compute_central_graph.R

Defines functions compute_central_graph

Documented in compute_central_graph

# R/compute_central_graph.R

#' Compute the Central (Representative) Graph for a Population
#'
#' This function computes the central graph for a given population by averaging
#' the adjacency matrices of all graphs within that population.
#'
#' @param graph_list A list of adjacency matrices representing brain networks.
#'
#' @return A single adjacency matrix representing the central graph of the population.
#' @export
#'
#' @examples
#' # Generate synthetic Control data
#' Control <- list(
#'   generate_random_graph(n_nodes = 5, edge_prob = 0.1),
#'   generate_random_graph(n_nodes = 5, edge_prob = 0.1)
#' )
#' central_graph <- compute_central_graph(Control)
compute_central_graph <- function(graph_list) {
  if (length(graph_list) == 0) {
    stop("graph_list is empty.")
  }
  
  # Check if all matrices are square and have the same dimensions
  n_nodes <- nrow(graph_list[[1]])
  for (G in graph_list) {
    if (!is.matrix(G)) {
      stop("All elements of graph_list must be matrices.")
    }
    if (nrow(G) != n_nodes || ncol(G) != n_nodes) {
      stop("All adjacency matrices must be square and of the same dimensions.")
    }
  }
  
  # Element-wise mean of the adjacency matrices
  return(Reduce("+", graph_list) / length(graph_list))
}

Try the BrainNetTest package in your browser

Any scripts or data that you put into this service are public.

BrainNetTest documentation built on Dec. 3, 2026, 5:08 p.m.