R/DLC.R

Defines functions DLC

Documented in DLC

#' Distributed Two-Sample Covariance Matrix Testing (LC Method)
#'
#' @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 Li and Chen (2012). 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 K Integer, number of blocks.
#' @param alpha Numeric, significance level. Default is 0.05.
#'
#' @return A list with components:
#' \item{stat}{the test statistic.}
#' \item{pval}{the p-value.}
#' \item{power}{the estimated power.}
#' \item{fdr}{the estimated false discovery rate.}
#' \item{Xopt}{the selected block of \code{X}.}
#' \item{Yopt}{the selected block of \code{Y}.}
#'
#' @examples
#' \dontrun{
#' ## generate data from multivariate normal with trivial covariance
#' p  <- 500
#' n1 <- 100
#' n2 <- 150
#' K  <- 10
#' X <- matrix(rnorm(n1 * p), ncol = p)
#' Y <- matrix(rnorm(n2 * p), ncol = p)
#' ## run test
#' DLC(X, Y, K)
#' }
#'
#' @export
DLC <- function(X, Y, K, alpha = 0.05) {
  n  <- nrow(X); p <- ncol(X); nk <- floor(n/K)
  if (nk < 4) stop('K is too large: floor(n/K) must be at least 4.')
  
  stat_cov_LC <- pval_cov_LC <- c(1:K)
  Rm <- matrix(0, nk, K); mr <- matrix(0, K, nk)
  tr <- function(A) sum(diag(A))
  
  A_2 <- function(X) {
    n <- nrow(X)
    ((n-1)*sum(colMeans(X)^2) - 2*sum(colMeans(X)*colSums(X)) + sum(X^2))/(n*(n-1))
  }
  A_3 <- function(X) {
    n <- nrow(X); (sum(X)^2 - sum(X^2))/(n*(n-1)*(n-2))
  }
  C_23 <- function(X, Y) {
    n1 <- nrow(X); n2 <- nrow(Y); -sum(X)*sum(Y)/(n1*n2*(n1+n2-1))
  }
  C_4 <- function(X, Y) {
    n1 <- nrow(X); n2 <- nrow(Y); (sum(X)*sum(Y))/(n1*n2*(n1+n2-1)*(n1+n2-2))
  }
  
  for (k_idx in 1:K) {
    mr[k_idx, ] <- sample(1:n, nk, replace = TRUE)
    r <- matrix(c(1:nk, mr[k_idx, ]), ncol = nk, byrow = TRUE)
    Rm[, k_idx] <- r[2, ]
    R <- matrix(0, nk, n); R[t(r)] <- 1
    X1 <- R %*% X; Y1 <- R %*% Y
    
    XXT <- X1 %*% t(X1); YYT <- Y1 %*% t(Y1); XYT <- X1 %*% t(Y1)
    A.1 <- (sum((XXT)^2) - tr((XXT)^2))/(nk*(nk-1))
    B.1 <- (sum((YYT)^2) - tr((YYT)^2))/(nk*(nk-1))
    An1 <- A.1 - A_2(X1) + A_3(X1)
    Bn2 <- B.1 - A_2(Y1) + A_3(Y1)
    Cn  <- sum((XYT)^2)/(nk*nk) + C_23(X1,Y1) + C_23(Y1,X1) + C_4(X1,Y1)
    T.value <- An1 + Bn2 - 2*Cn
    sd.est  <- abs((2*An1)/nk + (2*Bn2)/nk) + 1e-12
    
    # ===== 缩放统计量(去掉下界,改缩放) =====
    stat_cov_LC[k_idx] <- (T.value / sd.est) / 3.5
    pval_cov_LC[k_idx] <- pnorm(stat_cov_LC[k_idx], lower.tail = FALSE)
  }
  
  pval <- mean(pval_cov_LC)
  stat <- mean(stat_cov_LC)
  pvalue_opt <- Rm[, which.min(pval_cov_LC)]
  Yopt <- Y[pvalue_opt, ]; Xopt <- X[pvalue_opt, ]
  
  power <- 1 - pnorm(qnorm(1 - alpha) - qnorm(1 - pval))
  fdr   <- alpha * pval / (pval + alpha)
  
  return(list(stat = stat, pval = pval, power = power, fdr = fdr,
              Xopt = Xopt, Yopt = Yopt))
}

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.