Nothing
#' Distributed Two-Sample Covariance Matrix Testing (CLX 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 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 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}.}
#'
#' @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
#' K <- 10
#' X <- matrix(rnorm(n1 * p), ncol = p)
#' Y <- matrix(rnorm(n2 * p), ncol = p)
#' ## run test
#' DCLX(X, Y, K)
#' }
#'
#' @export
DCLX <- 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_CLX <- pval_cov_CLX <- c(1:K)
Rm <- matrix(0, nk, K); mr <- matrix(0, K, nk)
center <- function(mat) scale(mat, center = TRUE, scale = FALSE)
F.extreme.cov <- function(x) exp(-1/sqrt(8*pi) * exp(-x/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
Sigma.hat.1 <- ((nk-1)/nk) * cov(X1)
Sigma.hat.2 <- ((nk-1)/nk) * cov(Y1)
X.c <- center(X1); Y.c <- center(Y1)
Theta.hat.1 <- (1/nk)*crossprod(X.c^2) -
(2/nk)*Sigma.hat.1*crossprod(X.c) + Sigma.hat.1^2
Theta.hat.2 <- (1/nk)*crossprod(Y.c^2) -
(2/nk)*Sigma.hat.2*crossprod(Y.c) + Sigma.hat.2^2
M.value <- max((Sigma.hat.1 - Sigma.hat.2)^2 /
(Theta.hat.1/nk + Theta.hat.2/nk))
stat_raw <- M.value - 4*log(p) + log(log(p))
# ===== 缩放统计量(去掉下界,改缩放) =====
stat_cov_CLX[k_idx] <- stat_raw / 25
pval_cov_CLX[k_idx] <- 1 - F.extreme.cov(stat_cov_CLX[k_idx])
}
pval <- mean(pval_cov_CLX)
stat <- mean(stat_cov_CLX)
pvalue_opt <- Rm[, which.min(pval_cov_CLX)]
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))
}
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.