Nothing
# R/identify_critical_links_naive.R
# Naive (brute-force) reference implementation of identify_critical_links.
# ---------------------------------------------------------------------------
# This function recomputes the test statistic T from scratch after every edge
# removal and regenerates the full permutation null at each step. Its
# distance-based complexity is O(K * B * |E| * m * n), where n is the total
# number of graphs. It is kept internally for benchmarking and as a correctness
# reference for the optimised `identify_critical_links()`; it is NOT exported.
#
#' @noRd
#' @keywords internal
identify_critical_links_naive <- function(populations,
alpha = 0.05,
method = "fisher",
adjust_method = "none",
batch_size = 1,
n_permutations = 1000,
a = 1,
seed = NULL) {
cl <- match.call()
if (!is.null(seed)) set.seed(seed)
.check_inference_populations(populations)
batch_size <- .check_count(batch_size, "batch_size")
n_permutations <- .check_count(n_permutations, "n_permutations")
alpha <- .check_proportion(alpha, "alpha")
a <- .check_positive(a, "a")
method <- .check_edge_method(method, length(populations))
adjust_method <- .check_adjust_method(adjust_method)
settings <- list(alpha = alpha, method = method,
adjust_method = adjust_method, batch_size = batch_size,
n_permutations = n_permutations, a = a, seed = seed,
criterion = "original")
## 0. Metadata ------------------------------------------------------------
Npop <- lengths(populations)
m <- length(populations)
n <- sum(Npop)
## 1. Rank edges by marginal p-value (same as optimised) -----------------
freq <- compute_edge_frequencies(populations)
raw_pvals <- compute_edge_pvalues(freq$edge_counts, Npop, method = method)
edge_pvals <- .adjust_edge_pvalues(raw_pvals, adjust_method)
edge_df <- rank_edges(edge_pvals, tie_values =
if (identical(adjust_method, "none")) NULL else raw_pvals)
n_edges <- nrow(edge_df)
if (n_edges == 0L)
return(.critical_links(NULL, list(), populations, NA_real_, 0L,
settings, cl))
## Helper: permutation p-value for the current populations ----------------
.permutation_pvalue <- function(pops, Npop_vec, n_total, n_perm) {
## Normalisation is immaterial to ranks; keep a = 1 to avoid artificial
## underflow/overflow, as in the optimised implementation.
T_obs <- compute_test_statistic(pops, a = 1)
all_graphs <- unlist(pops, recursive = FALSE)
T_perm <- numeric(n_perm)
for (b in seq_len(n_perm)) {
perm_idx <- sample.int(n_total)
cumN <- c(0L, cumsum(Npop_vec))
perm_pops <- lapply(seq_along(Npop_vec), function(k)
all_graphs[perm_idx[(cumN[k] + 1L):cumN[k + 1L]]])
names(perm_pops) <- names(pops)
T_perm[b] <- compute_test_statistic(perm_pops, a = 1)
}
.plus_one_pvalue(c(T_obs, T_perm))
}
## 2. Global test ---------------------------------------------------------
initial_p <- .permutation_pvalue(populations, Npop, n, n_permutations)
if (initial_p > alpha) {
warning("Initial test is not significant (p = ",
round(initial_p, 4),
"). Populations may be identical or differences too small to detect.")
return(.critical_links(NULL, list(), populations, initial_p, n_edges,
settings, cl))
}
## 3. Iterative edge removal (naive: full recomputation each step) ------
edges_removed <- list()
k_removed <- 0L
continue <- TRUE
while (continue && k_removed < n_edges) {
batch_end <- min(k_removed + batch_size, n_edges)
idx_batch <- (k_removed + 1L):batch_end
## Physically remove edges from all graphs -----------------------------
for (idx in idx_batch) {
i <- edge_df$node1[idx]; j <- edge_df$node2[idx]
for (pop in seq_along(populations))
for (g in seq_len(Npop[pop])) {
populations[[pop]][[g]][i, j] <- 0
populations[[pop]][[g]][j, i] <- 0
}
edges_removed[[length(edges_removed) + 1L]] <- c(i, j)
}
k_removed <- batch_end
## Recompute T and permutation null FROM SCRATCH -----------------------
p_k <- .permutation_pvalue(populations, Npop, n, n_permutations)
continue <- (p_k <= alpha)
}
critical_edges <- if (k_removed > 0)
edge_df[seq_len(k_removed), ]
else
NULL
.critical_links(critical_edges, edges_removed, populations, initial_p,
n_edges, settings, cl)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.