R/DPEF.R

Defines functions DPEF

Documented in DPEF

#' Distributed Two-Sample Covariance Matrix Testing (Fisher Combination)
#'
#' @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 Yu, Li and Xue (2022). 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
#' DPEF(X, Y, K)
#' }
#'
#' @export
DPEF <- 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_Fisher <- pval_cov_Fisher <- c(1:K)
  Rm <- matrix(0, nk, K); mr <- matrix(0, K, nk)
  
  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
    
    pval_cov_LC  <- LC(X1, Y1)$pval
    pval_cov_CLX <- CLX(X1, Y1)$pval
    # 输入保护:避免 -log 爆炸(统计性保护)
    pval_cov_LC  <- max(pval_cov_LC,  0.01)
    pval_cov_CLX <- max(pval_cov_CLX, 0.01)
    
    stat_raw <- -2*log(pval_cov_LC) - 2*log(pval_cov_CLX)
    
    # ===== 缩放统计量(去掉下界,改缩放) =====
    stat_cov_Fisher[k_idx] <- stat_raw / 2
    pval_cov_Fisher[k_idx] <- pchisq(stat_cov_Fisher[k_idx], df = 4, lower.tail = FALSE)
  }
  
  pval <- mean(pval_cov_Fisher)
  stat <- mean(stat_cov_Fisher)
  pvalue_opt <- Rm[, which.min(pval_cov_Fisher)]
  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.