| Title: | Hypothesis Testing for Populations of Brain Networks |
|---|---|
| Description: | Non-parametric hypothesis testing for populations of brain networks represented as graphs, following the L1-distance ANOVA framework of Fraiman and Fraiman (2018) <doi:10.1038/s41598-018-23152-5>. The package builds on this nonparametric graph-comparison framework, extending it with procedures for edge-level inference and identification of the specific connections driving group differences. In particular, it provides utilities to compute central (mean) graphs, pairwise Manhattan distances between adjacency matrices, the group test statistic T and its permutation p-value, and a fast permutation procedure to identify the critical edges that drive between-group differences. Helper functions to generate synthetic community-structured graphs and to visualise brain networks with communities are also included. |
| Authors: | Maximiliano Martino [aut, cre] (ORCID: <https://orcid.org/0000-0002-2437-4387>), Daniel Fraiman [aut] (ORCID: <https://orcid.org/0000-0002-0482-9137>) |
| Maintainer: | Maximiliano Martino <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-10-15 11:20:33 UTC |
| Source: | https://github.com/mmaximiliano/brainnettest |
This function computes the central graph for a given population by averaging the adjacency matrices of all graphs within that population.
compute_central_graph(graph_list)compute_central_graph(graph_list)
graph_list |
A list of adjacency matrices representing brain networks. |
A single adjacency matrix representing the central graph of the population.
# 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)# 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)
This function calculates the Manhattan norm (L1 norm) distance between two adjacency matrices representing brain networks.
compute_distance(G, M)compute_distance(G, M)
G |
A square adjacency matrix representing a brain network. |
M |
A square adjacency matrix representing the central brain network. |
A numeric value representing the Manhattan distance between G and M.
# Generate synthetic data G1 <- generate_random_graph(n_nodes = 5, edge_prob = 0.1) G2 <- generate_random_graph(n_nodes = 5, edge_prob = 0.1) central_graph <- compute_central_graph(list(G1, G2)) distance <- compute_distance(G1, central_graph)# Generate synthetic data G1 <- generate_random_graph(n_nodes = 5, edge_prob = 0.1) G2 <- generate_random_graph(n_nodes = 5, edge_prob = 0.1) central_graph <- compute_central_graph(list(G1, G2)) distance <- compute_distance(G1, central_graph)
This function computes the frequency of each edge (connection between nodes) across all graphs within each population.
compute_edge_frequencies(populations)compute_edge_frequencies(populations)
populations |
A list where each element is a population containing a list of graphs (adjacency matrices). |
A list containing edge counts and edge proportions for each population.
# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) # View edge counts for the first population edge_counts_control <- frequencies$edge_counts[,,1] print(edge_counts_control)# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) # View edge counts for the first population edge_counts_control <- frequencies$edge_counts[,,1] print(edge_counts_control)
This function computes p-values for the differences in edge presence between populations using statistical tests (e.g., Fisher's exact test, chi-squared test).
compute_edge_pvalues(edge_counts, N, method = "fisher", adjust_method = "none")compute_edge_pvalues(edge_counts, N, method = "fisher", adjust_method = "none")
edge_counts |
An array of edge counts from |
N |
A vector of sample sizes for each population. |
method |
The statistical test method to use: "fisher", "chi.squared", or "prop". Default is "fisher". |
adjust_method |
The method for p-value adjustment for multiple testing. Default is "none". |
A matrix of p-values for each edge.
# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) edge_counts <- frequencies$edge_counts N <- lengths(populations) # Compute p-values for edge differences edge_pvalues <- compute_edge_pvalues(edge_counts, N) # View p-values for the first few edges print(edge_pvalues[1:5, 1:5])# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) edge_counts <- frequencies$edge_counts N <- lengths(populations) # Compute p-values for edge differences edge_pvalues <- compute_edge_pvalues(edge_counts, N) # View p-values for the first few edges print(edge_pvalues[1:5, 1:5])
This function computes the test statistic T to assess whether different populations of brain networks originate from the same distribution.
compute_test_statistic(populations, a = 1)compute_test_statistic(populations, a = 1)
populations |
A named list where each element is a list of adjacency
matrices for a population. Example:
|
a |
A normalization constant. Default is 1. |
A numeric value representing the test statistic T.
# Generate synthetic populations data Control <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.1), generate_random_graph(n_nodes = 5, edge_prob = 0.1) ) PatientA <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.15), generate_random_graph(n_nodes = 5, edge_prob = 0.15) ) PatientB <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.2), generate_random_graph(n_nodes = 5, edge_prob = 0.2) ) populations <- list(Control = Control, PatientA = PatientA, PatientB = PatientB) # Compute the test statistic T T_value <- compute_test_statistic(populations, a = 1) print(T_value)# Generate synthetic populations data Control <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.1), generate_random_graph(n_nodes = 5, edge_prob = 0.1) ) PatientA <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.15), generate_random_graph(n_nodes = 5, edge_prob = 0.15) ) PatientB <- list( generate_random_graph(n_nodes = 5, edge_prob = 0.2), generate_random_graph(n_nodes = 5, edge_prob = 0.2) ) populations <- list(Control = Control, PatientA = PatientA, PatientB = PatientB) # Compute the test statistic T T_value <- compute_test_statistic(populations, a = 1) print(T_value)
This function generates a list of adjacency matrices representing brain networks belonging to the same category (e.g., Control group). The generated graphs have similar community structures but vary slightly in their intra-community and inter-community connection probabilities to reflect natural variability.
generate_category_graphs( n_graphs = 10, n_nodes = 100, n_communities = 4, community_sizes = NULL, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = NULL )generate_category_graphs( n_graphs = 10, n_nodes = 100, n_communities = 4, community_sizes = NULL, base_intra_prob = 0.8, base_inter_prob = 0.2, intra_prob_variation = 0.05, inter_prob_variation = 0.05, seed = NULL )
n_graphs |
An integer specifying the number of graphs to generate. Default is 10. |
n_nodes |
An integer specifying the total number of nodes (brain regions). Default is 100. |
n_communities |
An integer specifying the number of communities. Default is 4. |
community_sizes |
An integer vector specifying the sizes of each community. If NULL, communities are of equal size. Default is NULL. |
base_intra_prob |
A numeric value between 0 and 1, or a numeric vector
of length |
base_inter_prob |
A numeric value between 0 and 1 specifying the base probability of an edge existing between nodes from different communities. Default is 0.2. |
intra_prob_variation |
A numeric value specifying the maximum variation to apply to the intra-community probability for each graph. Default is 0.05. |
inter_prob_variation |
A numeric value specifying the maximum variation to apply to the inter-community probability for each graph. Default is 0.05. |
seed |
An optional integer for setting the random seed to ensure reproducibility. Default is NULL. |
A list of symmetric binary adjacency matrices with no self-loops, representing brain networks with similar community structures.
# Generate a set of 5 graphs for the Control category control_graphs <- generate_category_graphs(n_graphs = 5, n_nodes = 100, n_communities = 4, base_intra_prob = 0.8, base_inter_prob = 0.2)# Generate a set of 5 graphs for the Control category control_graphs <- generate_category_graphs(n_graphs = 5, n_nodes = 100, n_communities = 4, base_intra_prob = 0.8, base_inter_prob = 0.2)
This function generates a random symmetric adjacency matrix representing a brain network with community structure. Nodes within the same community have a higher probability of being connected compared to nodes from different communities.
generate_community_graph( n_nodes = 100, n_communities = 4, community_sizes = NULL, intra_prob = 0.8, inter_prob = 0.2, seed = NULL )generate_community_graph( n_nodes = 100, n_communities = 4, community_sizes = NULL, intra_prob = 0.8, inter_prob = 0.2, seed = NULL )
n_nodes |
An integer specifying the total number of nodes (brain regions). Default is 100. |
n_communities |
An integer specifying the number of communities. Default is 4. |
community_sizes |
An integer vector specifying the sizes of each community. If NULL, communities are of equal size. Default is NULL. |
intra_prob |
A numeric value between 0 and 1, or a numeric vector of
length |
inter_prob |
A numeric value between 0 and 1 specifying the probability of an edge existing between nodes from different communities. Default is 0.2. |
seed |
An optional integer for setting the random seed to ensure reproducibility. Default is NULL. |
A symmetric binary adjacency matrix with no self-loops, representing a brain network with community structure.
# Generate a brain network with community structure G <- generate_community_graph(n_nodes = 100, n_communities = 4, intra_prob = 0.8, inter_prob = 0.2)# Generate a brain network with community structure G <- generate_community_graph(n_nodes = 100, n_communities = 4, intra_prob = 0.8, inter_prob = 0.2)
This function generates a random symmetric adjacency matrix representing a brain network. The adjacency matrix is binary, with edges present based on a specified probability.
generate_random_graph(n_nodes, edge_prob = 0.1)generate_random_graph(n_nodes, edge_prob = 0.1)
n_nodes |
An integer specifying the number of nodes (brain regions). |
edge_prob |
A numeric value between 0 and 1 specifying the probability of an edge existing between any two nodes. |
A symmetric binary adjacency matrix with no self-loops.
G <- generate_random_graph(n_nodes = 10, edge_prob = 0.1)G <- generate_random_graph(n_nodes = 10, edge_prob = 0.1)
Given the output of identify_critical_links, this function
identifies the unique nodes involved in the critical edges and summarizes
their participation. Each node is reported with the number of critical edges
it participates in (its critical degree). If node labels are supplied,
they are included in the output.
get_critical_nodes(result, node_labels = NULL)get_critical_nodes(result, node_labels = NULL)
result |
The list returned by |
node_labels |
An optional character vector of length |
A data frame with the following columns, ordered by decreasing
critical_degree:
Integer node index.
Character label for the node (only present when
node_labels is not NULL).
Number of critical edges incident on this node.
If no critical edges were found (result$critical_edges is
NULL or has zero rows), an empty data frame with the same columns
is returned.
# Generate two synthetic populations with different community structure control <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) populations <- list(Control = control, Patient = patient) # Identify critical edges result <- identify_critical_links(populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 42) # Extract critical nodes (no labels) get_critical_nodes(result) # With labels labels <- paste0("Region_", 1:10) get_critical_nodes(result, node_labels = labels)# Generate two synthetic populations with different community structure control <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) populations <- list(Control = control, Patient = patient) # Identify critical edges result <- identify_critical_links(populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 42) # Extract critical nodes (no labels) get_critical_nodes(result) # With labels labels <- paste0("Region_", 1:10) get_critical_nodes(result, node_labels = labels)
Tests whether two or more populations of brain networks share the same
central graph, using the L1-distance ANOVA statistic T of Fraiman and
Fraiman (2018) and a permutation null distribution. This is the global test
that identify_critical_links runs before it starts removing
edges; here it is available on its own, so that the population-level
question can be answered, and reported, before any edge is singled out.
global_test(populations, n_permutations = 1000, seed = NULL)global_test(populations, n_permutations = 1000, seed = NULL)
populations |
A named |
n_permutations |
Positive integer giving the number of permutation
replicates used to approximate the null distribution of T. Default
|
seed |
Optional integer seed for reproducibility of the permutation
replicates. Default |
The statistic T contrasts the mean distance of each graph to its
own population's central graph with its mean distance to every
population's central graph (see compute_test_statistic).
Under the null hypothesis of a common central graph it is approximately
standard normal; under the alternative it shifts towards negative values,
because graphs are closer to their own centre than to the others. The
value is therefore the left tail: replicates are generated by
permuting the group labels across the pooled sample while preserving the
group sizes, T is recomputed for each, and the proportion of replicates
with a statistic below the observed one is reported. With
replicates the estimate cannot resolve below , which is how the
print method reports an estimate of zero.
The normalisation constant a of
compute_test_statistic is fixed at 1. The value
does not depend on it: a rescales the observed statistic and every
replicate by the same positive factor, which leaves their comparison
unchanged.
The computation uses the edge-wise decomposition of T described in
identify_critical_links: the statistic of every replicate
is obtained from permuted edge counts through a single matrix product,
rather than by rebuilding graphs and recomputing distances. That
decomposition holds for binary graphs, which is why the input must be
binary. Calling global_test() and
identify_critical_links with the same populations,
n_permutations and seed evaluates the same permutations, so
the value returned here is the one that
identify_critical_links() records in its p_value component.
An object of class "global_test": a list with the
components
the observed value of T, computed with normalisation
constant a = 1.
the permutation value: the proportion of
replicates whose statistic is smaller than the observed one.
NA when the graphs have a single node and hence no edges.
numeric vector of length
n_permutations holding the statistic of every replicate.
the number of replicates.
named integer vector with the number of graphs in each population.
the number of nodes.
the matched call.
A print method is provided.
Fraiman, D. and Fraiman, R. (2018) An ANOVA approach for statistical comparisons of brain networks. Scientific Reports, 8, 4746. doi:10.1038/s41598-018-23152-5.
compute_test_statistic for the statistic alone,
identify_critical_links for the edges behind a rejection,
print.global_test.
control <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2) populations <- list(Control = control, Patient = patient) gt <- global_test(populations, n_permutations = 500, seed = 42) gt gt$p_value # The observed statistic is the one compute_test_statistic() returns all.equal(gt$statistic, compute_test_statistic(populations, a = 1)) # The null distribution is kept, so it can be inspected summary(gt$null_distribution)control <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2) populations <- list(Control = control, Patient = patient) gt <- global_test(populations, n_permutations = 500, seed = 42) gt gt$p_value # The observed statistic is the one compute_test_statistic() returns all.equal(gt$statistic, compute_test_statistic(populations, a = 1)) # The null distribution is kept, so it can be inspected summary(gt$null_distribution)
Iteratively identifies the edges that drive the observed difference between two or more populations of brain networks, using the permutation-based L1-distance ANOVA test of Fraiman and Fraiman (2018). Edges are ranked by their marginal p-value, then removed in batches until the global test is no longer significant. The permutation null is re-evaluated incrementally via a prefix-sum decomposition of T, which avoids recomputing the statistic from scratch at every step.
identify_critical_links( populations, alpha = 0.05, method = "fisher", adjust_method = "none", batch_size = 1, n_permutations = 1000, a = 1, seed = NULL )identify_critical_links( populations, alpha = 0.05, method = "fisher", adjust_method = "none", batch_size = 1, n_permutations = 1000, a = 1, seed = NULL )
populations |
A named |
alpha |
Numeric significance level used both for the global test and
for the iterative edge-removal stopping rule. Default |
method |
Character string selecting the marginal edge test used to
rank candidate edges. One of |
adjust_method |
Character string passed to |
batch_size |
Positive integer giving the number of edges to drop per
iteration. Larger values speed up the procedure at the cost of coarser
resolution. Default |
n_permutations |
Positive integer giving the number of permutation
replicates used to approximate the null distribution of T. Default
|
a |
Positive numeric normalisation constant for the test statistic T
(see |
seed |
Optional integer seed for reproducibility of the permutation
replicates. Default |
The implementation exploits the fact that T decomposes as a sum of
per-edge contributions . Removing an edge e is equivalent
to subtracting from T, so prefix sums of the sorted
values give the test statistic after any number of
removals in O(1). The permutation null is built once via a single BLAS
matrix multiplication and reused for every iteration, reducing the
overall complexity from O(K * B * |E| * m) (naive) to O(B * |E| * m).
Permutation replicates are generated by uniformly permuting the group labels across the pooled sample of all graphs, while preserving the original group sizes (sampling without replacement).
An object of class "critical_links": a list with the
components
a data.frame with columns node1,
node2, p_value, listing the edges removed until the test
became non-significant, ordered from most to least significant;
NULL if the global test was not significant.
a list of length-2 integer vectors giving the
(i, j) indices of the removed edges, in removal order.
the input populations with the
critical edges zeroed out in every graph.
the permutation value of the global test on the
unmodified data, NA when there were no candidate edges.
the number of candidate edges considered.
the arguments the analysis ran with.
the matched call.
Methods are provided for print,
summary and
plot. The first three components are
unchanged from earlier versions, so code that extracts them keeps working.
Fraiman, D. and Fraiman, R. (2018) An ANOVA approach for statistical comparisons of brain networks. Scientific Reports, 8, 4746. doi:10.1038/s41598-018-23152-5.
print.critical_links,
summary.critical_links, plot.critical_links,
compute_test_statistic,
compute_edge_pvalues, get_critical_nodes.
set.seed(1) control <- generate_category_graphs( n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs( n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) populations <- list(Control = control, Patient = patient) result <- identify_critical_links( populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 42) result summary(result) head(result$critical_edges)set.seed(1) control <- generate_category_graphs( n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs( n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) populations <- list(Control = control, Patient = patient) result <- identify_critical_links( populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 42) result summary(result) head(result$critical_edges)
Produces a multi-panel figure summarising the output of
identify_critical_links: one panel per population showing the
weighted central graph, plus a final panel that highlights the critical
edges on a chosen reference central graph. This is the canonical
visualization of the BrainNetTest workflow and replaces the manual
igraph plumbing required to build the same figure.
plot_critical_edges( populations, critical_links, communities = NULL, layout = NULL, central_graphs = NULL, reference = 1L, threshold = 0.5, edge_scale = 6, critical_color = "red", critical_width = 3, background_color = "grey80", background_width = 1, vertex_size = 12, vertex_label = TRUE, panel_titles = NULL, mfrow = NULL, ... )plot_critical_edges( populations, critical_links, communities = NULL, layout = NULL, central_graphs = NULL, reference = 1L, threshold = 0.5, edge_scale = 6, critical_color = "red", critical_width = 3, background_color = "grey80", background_width = 1, vertex_size = 12, vertex_label = TRUE, panel_titles = NULL, mfrow = NULL, ... )
populations |
A named |
critical_links |
The object returned by
|
communities |
Optional integer (or factor) vector of length
|
layout |
Optional layout for the graphs. Either an
|
central_graphs |
Optional list of pre-computed central graphs (one per
population). If |
reference |
Either an integer index or a name selecting which
population's central graph is used as the background of the
critical-edges panel. Default |
threshold |
Numeric in |
edge_scale |
Numeric multiplier applied to the central-graph edge
weights to set |
critical_color, critical_width
|
Color and line width used to draw
critical edges. Defaults |
background_color, background_width
|
Color and line width used to draw
non-critical edges in the critical panel. Defaults |
vertex_size |
Vertex size passed to |
vertex_label |
Logical. If |
panel_titles |
Optional character vector of length
|
mfrow |
Optional integer vector of length 2 giving the
|
... |
Additional arguments forwarded to
|
The first length(populations) panels show the weighted
central graph of each population, with edge widths proportional to the
central-graph weights. The final panel binarises the chosen reference
central graph at threshold and overlays the critical edges
returned by identify_critical_links() in
critical_color. Critical edges that are absent from the binarised
reference graph are added so that they remain visible.
The graphics state (par(mfrow, mar, oma)) is restored on exit.
Invisibly returns NULL; called for its side effect of
producing the multi-panel plot.
identify_critical_links,
compute_central_graph,
get_critical_nodes.
set.seed(123) community_sizes <- c(4, 2, 3, 3, 5) control <- generate_category_graphs( n_graphs = 50, n_nodes = 17, n_communities = 5, community_sizes = community_sizes, base_intra_prob = rep(0.70, 5), base_inter_prob = 0.05, intra_prob_variation = 0.02, inter_prob_variation = 0.01) patient <- generate_category_graphs( n_graphs = 50, n_nodes = 17, n_communities = 5, community_sizes = community_sizes, base_intra_prob = c(0.40, 0.70, 0.70, 0.70, 0.70), base_inter_prob = 0.05, intra_prob_variation = 0.02, inter_prob_variation = 0.01) populations <- list(Control = control, Patient = patient) result <- identify_critical_links(populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 1) communities <- rep(seq_along(community_sizes), times = community_sizes) plot_critical_edges(populations, result, communities = communities)set.seed(123) community_sizes <- c(4, 2, 3, 3, 5) control <- generate_category_graphs( n_graphs = 50, n_nodes = 17, n_communities = 5, community_sizes = community_sizes, base_intra_prob = rep(0.70, 5), base_inter_prob = 0.05, intra_prob_variation = 0.02, inter_prob_variation = 0.01) patient <- generate_category_graphs( n_graphs = 50, n_nodes = 17, n_communities = 5, community_sizes = community_sizes, base_intra_prob = c(0.40, 0.70, 0.70, 0.70, 0.70), base_inter_prob = 0.05, intra_prob_variation = 0.02, inter_prob_variation = 0.01) populations <- list(Control = control, Patient = patient) result <- identify_critical_links(populations, alpha = 0.05, method = "fisher", n_permutations = 200, seed = 1) communities <- rep(seq_along(community_sizes), times = community_sizes) plot_critical_edges(populations, result, communities = communities)
Method interface to plot_critical_edges: draws the weighted
central graph of each population alongside a panel highlighting the
critical edges.
## S3 method for class 'critical_links' plot(x, populations, ...)## S3 method for class 'critical_links' plot(x, populations, ...)
x |
An object of class |
populations |
The named |
... |
Further arguments passed to |
The populations must be supplied, because the object stores the modified populations, in which the critical edges have been removed, and plotting them would hide the very structure the figure is about.
Invisibly NULL; called for its side effect.
plot_critical_edges, which this method wraps and
which can also be called directly.
set.seed(1) community_sizes <- c(4, 3, 3) control <- generate_category_graphs(n_graphs = 30, n_nodes = 10, n_communities = 3, community_sizes = community_sizes, base_intra_prob = rep(0.8, 3), base_inter_prob = 0.1, seed = 1) patient <- generate_category_graphs(n_graphs = 30, n_nodes = 10, n_communities = 3, community_sizes = community_sizes, base_intra_prob = c(0.3, 0.8, 0.8), base_inter_prob = 0.1, seed = 2) populations <- list(Control = control, Patient = patient) result <- identify_critical_links(populations, n_permutations = 200, seed = 42) plot(result, populations, communities = rep(seq_along(community_sizes), times = community_sizes))set.seed(1) community_sizes <- c(4, 3, 3) control <- generate_category_graphs(n_graphs = 30, n_nodes = 10, n_communities = 3, community_sizes = community_sizes, base_intra_prob = rep(0.8, 3), base_inter_prob = 0.1, seed = 1) patient <- generate_category_graphs(n_graphs = 30, n_nodes = 10, n_communities = 3, community_sizes = community_sizes, base_intra_prob = c(0.3, 0.8, 0.8), base_inter_prob = 0.1, seed = 2) populations <- list(Control = control, Patient = patient) result <- identify_critical_links(populations, n_permutations = 200, seed = 42) plot(result, populations, communities = rep(seq_along(community_sizes), times = community_sizes))
Compact report of an identify_critical_links analysis: the
populations compared, the outcome of the global test, and the size of the
critical set together with its most significant edges. Use
summary.critical_links for the call, the analysis settings
and the node-level view as well.
## S3 method for class 'critical_links' print(x, n = 6, digits = getOption("digits"), ...)## S3 method for class 'critical_links' print(x, n = 6, digits = getOption("digits"), ...)
x |
An object of class |
n |
Number of critical edges to display. Default |
digits |
Number of significant digits used for the $p$ values.
Default |
... |
Further arguments, currently ignored. |
x, invisibly.
summary.critical_links,
plot.critical_links.
control <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) result <- identify_critical_links(list(Control = control, Patient = patient), n_permutations = 200, seed = 42) resultcontrol <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) result <- identify_critical_links(list(Control = control, Patient = patient), n_permutations = 200, seed = 42) result
Reports the populations compared, the observed statistic and the
permutation value of a global_test analysis. An
estimate of zero is reported as being below , the resolution of
permutation replicates.
## S3 method for class 'global_test' print(x, digits = getOption("digits"), ...)## S3 method for class 'global_test' print(x, digits = getOption("digits"), ...)
x |
An object of class |
digits |
Number of significant digits used for the statistic.
Default |
... |
Further arguments, currently ignored. |
x, invisibly.
control <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2) global_test(list(Control = control, Patient = patient), n_permutations = 500, seed = 42)control <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 20, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2) global_test(list(Control = control, Patient = patient), n_permutations = 500, seed = 42)
Print a Critical-Edge Analysis Summary
## S3 method for class 'summary.critical_links' print(x, n = 10, digits = getOption("digits"), ...)## S3 method for class 'summary.critical_links' print(x, n = 10, digits = getOption("digits"), ...)
x |
An object of class |
n |
Number of critical edges and nodes to display. Default |
digits |
Number of significant digits used for the $p$ values.
Default |
... |
Further arguments, currently ignored. |
x, invisibly.
This function ranks edges based on their p-values obtained from statistical tests, ordering them from lowest to highest p-value (most to least significant).
rank_edges(edge_pvalues)rank_edges(edge_pvalues)
edge_pvalues |
A square matrix of p-values for each edge, typically obtained from |
A data frame with edges and their corresponding p-values, ordered from most significant.
# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) edge_counts <- frequencies$edge_counts N <- lengths(populations) # Compute p-values for edge differences edge_pvalues <- compute_edge_pvalues(edge_counts, N) # Rank edges based on p-values edge_df <- rank_edges(edge_pvalues) # View the top ranked edges head(edge_df)# Generate synthetic populations control_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1 ) patient_graphs <- generate_category_graphs( n_graphs = 5, n_nodes = 10, n_communities = 2, base_intra_prob = 0.6, base_inter_prob = 0.4, seed = 2 ) populations <- list(Control = control_graphs, Patient = patient_graphs) # Compute edge frequencies frequencies <- compute_edge_frequencies(populations) edge_counts <- frequencies$edge_counts N <- lengths(populations) # Compute p-values for edge differences edge_pvalues <- compute_edge_pvalues(edge_counts, N) # Rank edges based on p-values edge_df <- rank_edges(edge_pvalues) # View the top ranked edges head(edge_df)
Extends print.critical_links with the settings the analysis
used and a node-level view of the critical set: the nodes that participate
in the most critical edges, as returned by get_critical_nodes.
## S3 method for class 'critical_links' summary(object, node_labels = NULL, ...)## S3 method for class 'critical_links' summary(object, node_labels = NULL, ...)
object |
An object of class |
node_labels |
Optional character vector of node labels, passed to
|
... |
Further arguments, currently ignored. |
An object of class "summary.critical_links", a list with the
components n_critical, n_edges, p_value,
settings, group_sizes, critical_edges and
critical_nodes.
identify_critical_links,
get_critical_nodes.
control <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) result <- identify_critical_links(list(Control = control, Patient = patient), n_permutations = 200, seed = 42) summary(result)control <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.8, base_inter_prob = 0.2, seed = 1) patient <- generate_category_graphs(n_graphs = 15, n_nodes = 10, n_communities = 2, base_intra_prob = 0.5, base_inter_prob = 0.5, seed = 2) result <- identify_critical_links(list(Control = control, Patient = patient), n_permutations = 200, seed = 42) summary(result)