Skip to content

Commit 987b950

Browse files
committed
tests and docs
1 parent 99d1742 commit 987b950

6 files changed

+832
-146
lines changed

R/distcalc.R

-62
Original file line numberDiff line numberDiff line change
@@ -293,68 +293,6 @@ pairwise_dist.robustmahadist <- function(obj, X) {
293293
}
294294

295295

296-
#' Compute Second-Order Similarity Scores
297-
#'
298-
#' Calculates correlation-based \emph{second order similarity} between:
299-
#' \itemize{
300-
#' \item A \strong{full NxN distance matrix} computed from \code{X} via \code{distfun}, and
301-
#' \item A \code{Dref} matrix (the "reference" dissimilarities).
302-
#' }
303-
#' For each row \code{i}, this excludes same-block comparisons by selecting \code{which(block_var != block_var[i])}.
304-
#'
305-
#' @param distfun An S3 distance object (see \code{\link{create_dist}})
306-
#' specifying how to compute a pairwise distance matrix from \code{X}.
307-
#' @param X A numeric matrix (rows = observations, columns = features).
308-
#' @param Dref A numeric NxN reference matrix of dissimilarities (e.g., from an ROI mask or a prior).
309-
#' @param block_var A vector indicating block/group memberships for each row in \code{X}.
310-
#' @param method Correlation method: "pearson" or "spearman".
311-
#'
312-
#' @return A numeric vector of length \code{nrow(X)}, where each entry is
313-
#' the correlation (using \code{method}) between \code{distance_matrix[i, valid]} and
314-
#' \code{Dref[i, valid]}, with \code{valid = which(block_var != block_var[i])}.
315-
#'
316-
#' @details
317-
#' This function first calls \code{pairwise_dist(distfun, X)}, obtaining an NxN matrix
318-
#' of \emph{all} pairwise distances. It does not do block-based exclusion internally.
319-
#' Instead, for each row \code{i}, it excludes same-block rows from the correlation
320-
#' by subsetting the distances to \code{valid_indices}.
321-
#'
322-
#' @examples
323-
#' # Suppose we have X (10x5), a reference D (10x10), block var, and a correlation distfun:
324-
#' X <- matrix(rnorm(50), 10, 5)
325-
#' D <- matrix(runif(100), 10, 10)
326-
#' block <- rep(1:2, each=5)
327-
#' dist_obj <- cordist(method="pearson")
328-
#' scores <- second_order_similarity(dist_obj, X, D, block, method="spearman")
329-
#'
330-
#' @export
331-
second_order_similarity <- function(distfun, X, Dref, block_var, method=c("pearson", "spearman")) {
332-
method <- match.arg(method)
333-
334-
# 1) Compute a full NxN distance matrix from X
335-
distance_matrix <- pairwise_dist(distfun, X)
336-
337-
n <- nrow(X)
338-
scores <- numeric(n)
339-
340-
# 2) For each row i, exclude same-block comparisons
341-
for (i in seq_len(n)) {
342-
valid_indices <- which(block_var != block_var[i])
343-
if (length(valid_indices) > 0) {
344-
x_vec <- distance_matrix[i, valid_indices]
345-
ref_vec <- Dref[i, valid_indices]
346-
if (length(x_vec) > 1) {
347-
scores[i] <- cor(x_vec, ref_vec, method = method)
348-
} else {
349-
scores[i] <- NA
350-
}
351-
} else {
352-
scores[i] <- NA
353-
}
354-
}
355-
356-
scores
357-
}
358296

359297

360298

0 commit comments

Comments
 (0)