R/SNET.R

Defines functions SNET

Documented in SNET

#' Two-Sample Covariance Test
#'
#' @description
#' Given two sets of data matrices \code{X} and \code{Y}, where \code{X} is an
#' \eqn{n_1 \times p} matrix and \code{Y} is an \eqn{n_2 \times p} matrix, we
#' conduct hypothesis testing of the covariance matrix between two samples.
#' The null hypothesis is
#' \deqn{H_0 : \Sigma_1 = \Sigma_2,}
#' where \eqn{\Sigma_1} and \eqn{\Sigma_2} are the sample covariance matrices of
#' \code{X} and \code{Y} respectively. The test is based on the method proposed
#' by Cai, Liu and Xia (2013). When the p-value is less than the significance
#' level (generally 0.05), the null hypothesis is rejected.
#'
#' @param X A numeric matrix of dimension \eqn{n_1 \times p}.
#' @param Y A numeric matrix of dimension \eqn{n_2 \times p}.
#' @param B Integer, number of bootstrap replicates. Default is 200.
#' @param mc.cores Integer, number of cores for parallel computing.
#'   Default is 1 (serial execution).
#' @param alpha Numeric, significance level. Default is 0.05.
#' @param k Numeric, tuning parameter. Default is 3.
#'
#' @return A list with components:
#' \item{stat}{the test statistic.}
#' \item{pval}{the p-value.}
#' \item{z}{the standardized test statistic.}
#'
#' @references
#' Cai, T. T., Liu, W., and Xia, Y. (2013).
#' Two-sample covariance matrix testing and support recovery in
#' high-dimensional settings. \emph{Journal of the American Statistical
#' Association}, 108(501), 265--277.
#'
#' @examples
#' \dontrun{
#' ## generate data from multivariate normal with trivial covariance
#' p  <- 500
#' n1 <- 100
#' n2 <- 150
#' B  <- 300
#' X <- matrix(rnorm(n1 * p), ncol = p)
#' Y <- matrix(rnorm(n2 * p), ncol = p)
#' ## run test
#' SNET(X, Y, B, alpha = 0.05)
#' }
#'
#' @export
SNET <- function(X, Y, B = 200, mc.cores = 1L, alpha = 0.05, k = 3) {
  n1 <- nrow(X); n2 <- nrow(Y)
  p  <- ncol(X)
  if (p != ncol(Y)) stop("Dimensions of X and Y do not match!")
  if (p <= 30) warning("SNET is designed for high-dimensional data (p>>n).")
  
  S1 <- cov(X) * (n1 - 1) / n1
  S2 <- cov(Y) * (n2 - 1) / n2
  dS <- S1 - S2
  spec_obs <- RSpectra::svds(dS, 1, 0, 0)$d
  
  # ===== 关键修改:mc.cores = 1 时走串行,不建集群 =====
  boot_spec <- function(b) {
    idx1 <- sample(n1, replace = TRUE)
    idx2 <- sample(n2, replace = TRUE)
    S1b <- cov(X[idx1, ]) * (n1 - 1) / n1
    S2b <- cov(Y[idx2, ]) * (n2 - 1) / n2
    RSpectra::svds(S1b - S2b, 1, 0, 0)$d
  }
  if (mc.cores > 1L && parallel::detectCores() > 1L) {
    cl <- parallel::makeCluster(min(mc.cores, parallel::detectCores()))
    on.exit(parallel::stopCluster(cl), add = TRUE)
    parallel::clusterExport(cl, c("X", "Y", "n1", "n2"), envir = environment())
    parallel::clusterEvalQ(cl, library(RSpectra))
    spec_boot <- unlist(parallel::parLapply(cl, seq_len(B), boot_spec))
  } else {
    spec_boot <- unlist(lapply(seq_len(B), boot_spec))
  }
  # =====================================================
  
  var_spec <- var(spec_boot)
  
  F_obs  <- norm(dS, "F")^2
  logF_p <- log(F_obs / p)
  T_snet <- spec_obs / sqrt(var_spec) + logF_p
  var_logF <- (1/n1^2 + 1/n2^2) * 2
  var_snet <- 1 + var_logF
  
  z    <- T_snet / sqrt(var_snet) / k
  pval <- pnorm(z, lower.tail = FALSE)
  
  list(stat = T_snet, pval = pval, z = z)
}

Try the Docovt package in your browser

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

Docovt documentation built on Oct. 19, 2026, 5:06 p.m.