Generating and Analyzing Brain Networks with Community Structures

knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)

Overview

This vignette shows the full BrainNetTest workflow on synthetic populations of brain networks with community (block) structure. We simulate two groups of graphs with different intra- and inter-community connection probabilities, run the global L1-distance ANOVA test, and explore the edges and nodes associated with the observed difference.

library(BrainNetTest)

Generating a single community-structured graph

generate_community_graph() samples a symmetric binary adjacency matrix with a block-stochastic structure: edges within a community are drawn with probability intra_prob, edges between communities with inter_prob.

set.seed(42)
G <- generate_community_graph(
  n_nodes       = 40,
  n_communities = 4,
  intra_prob    = 0.8,
  inter_prob    = 0.2)

dim(G)
mean(G[upper.tri(G)])

Visualising the network

A single community-structured adjacency matrix is best inspected directly with igraph. The package's own visualisation helper, plot_critical_edges(), is designed to summarise the result of an analysis (per-population central graphs plus the identified critical edges) and is illustrated at the end of this vignette.

Simulating populations of graphs

generate_category_graphs() produces a list of graphs that share a common community structure: each edge is present independently with the within- or between-community probability, the same for every graph. (The arguments intra_prob_variation and inter_prob_variation can add a random shift of the probabilities of each graph; they are 0 by default.)

control <- generate_category_graphs(
  n_graphs             = 20,
  n_nodes              = 20,
  n_communities        = 2,
  base_intra_prob      = 0.8,
  base_inter_prob      = 0.2,
  seed                 = 1)

patient <- generate_category_graphs(
  n_graphs             = 20,
  n_nodes              = 20,
  n_communities        = 2,
  base_intra_prob      = 0.6,
  base_inter_prob      = 0.4,
  seed                 = 2)

populations <- list(Control = control, Patient = patient)
lengths(populations)

Global test statistic

T_obs <- compute_test_statistic(populations, a = 1)
T_obs

Exploring candidate edges

identify_critical_links() runs the global permutation test (using the combined T and U criterion by default) and, when significant, iteratively removes edges ordered by their marginal p-values until the test on the remaining edges exceeds alpha. The selected set is exploratory rather than an error-rate-controlled set of true edges, and stopping above alpha does not establish that the remaining networks are identical.

result <- identify_critical_links(
  populations,
  alpha          = 0.05,
  method         = "fisher",
  n_permutations = 500,
  seed           = 42)

result

Critical nodes

get_critical_nodes() aggregates the critical edges at the node level, reporting the critical degree (number of critical edges incident on each node):

get_critical_nodes(result)

Visualising the result

plot_critical_edges() produces a multi-panel figure that summarises the analysis: one panel per population showing the (weighted) central graph, plus a final panel that highlights the critical edges on the chosen reference central graph. Communities can be passed in to color the vertices consistently across panels.

Because the result is a critical_links object, plot() dispatches to that function. The populations have to be passed in, since the object stores the modified populations, from which the critical edges have been removed.

plot(
  result,
  populations,
  communities = rep(seq_len(2), each = 10),
  reference   = "Control")

References

Fraiman, D. and Fraiman, R. (2018) An ANOVA approach for statistical comparisons of brain networks. Scientific Reports, 8, 4746. https://doi.org/10.1038/s41598-018-23152-5



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.