.packageName <- "CORREP"
`cor.LRtest1` <-
function(x, m1, m2)
{
    ## m: number of replicates
    p <- dim(x)[1]
    n <- dim(x)[2]
    stopifnot(p == m1+m2)

    ## get grant means muhat_x, muhat_y
    mu.x <- mean(x[1:m1,])
    mu.y <- mean(x[(m1+1):(m1+m2),])
    mux <-  c(rep(mu.x, m1))
    muy <-  c(rep(mu.y, m2))
    mu <- c(rep(mu.x, m1), rep(mu.y, m2))

    ## get Sigmahat_x, Sigmahat_y, Sigma_null, Sigma_alternative
    Sigma.x <- matrix(0, m1, m1)
    Sigma.y <- matrix(0, m2, m2)
    Sigma.null <- matrix(0, p, p)
    Sigma.alter <- matrix(0, p, p)
    xnew1 <- x[1:m1,]-mux
    xnew2 <- x[(m1+1):(m1+m2),]-muy
    xnew <- x-mu

    for (j in 1:n)
    {
        Sigma.x <- Sigma.x  +  xnew1[,j]%*%t(xnew1 [,j])
        Sigma.y <- Sigma.y  +  xnew2[,j]%*%t(xnew2 [,j])
        Sigma.alter <- Sigma.alter + xnew[,j]%*%t(xnew[,j])
    }
    Sigma.x <- Sigma.x/n
    Sigma.y <- Sigma.y/n
    Sigma.alter <- Sigma.alter/n
    diag(Sigma.x) <- 1
    diag(Sigma.y) <- 1
    diag(Sigma.alter) <- 1
    Sigma.null[1:m1,1:m1] <- Sigma.x
    Sigma.null[(m1+1):(m1+m2), (m1+1):(m1+m2)] <- Sigma.y

    M <- solve(Sigma.null) %*% Sigma.alter
    G <- n* (sum(diag(M))-log(det(M))-(m1+m2))

    p.value <- 1- pchisq(G, df=2*m1*m2)
    p.value
}
`cor.LRtest2` <-
function(x, m1, m2)
{
    ## m: number of replicates
    p <- dim(x)[1]
    n <- dim(x)[2]
    stopifnot(p == m1+m2)

    ## get grant means muhat_x, muhat_y
    mu.x <- mean(x[1:m1,])
    mu.y <- mean(x[(m1+1):(m1+m2),])
    mux <-  c(rep(mu.x, m1))
    muy <-  c(rep(mu.y, m2))
    mu <- c(rep(mu.x, m1), rep(mu.y, m2))

    Sigma.x <- matrix(0, m1, m1)
    Sigma.y <- matrix(0, m2, m2)
    Sigma.null <- matrix(0, p, p)
    Sigma.alter <- matrix(0, p, p)
    xnew1 <- NULL
    xnew2 <- NULL
    xnew <- NULL
    ## get Sigmahat_x, Sigmahat_y, Sigma_null, Sigma_alternative
    xnew1 <- x[1:m1,]-mux
    xnew2 <- x[(m1+1):(m1+m2),]-muy
    xnew <- x-mu

    for (j in 1:n)
    {
        Sigma.x <- Sigma.x  +  xnew1[,j]%*%t(xnew1 [,j])
        Sigma.y <- Sigma.y  +  xnew2[,j]%*%t(xnew2 [,j])
        Sigma.alter <- Sigma.alter + xnew[,j]%*%t(xnew[,j])
    }
    Sigma.x <- Sigma.x/n
    Sigma.y <- Sigma.y/n
    Sigma.alter <- Sigma.alter/n
    Sigmax <- matrix(0, m1, m1)
    Sigmay <- matrix(0, m2, m2)
    Sigmaalter <- matrix(0, p, p)

    for (i in 1:m1)
    for (j in 1:m1)
        Sigmax[i,j] <- Sigma.x[i,j]/sqrt(Sigma.x[i,i]*Sigma.x[j,j])

    for (i in 1:m2)
    for (j in 1:m2)
        Sigmay[i,j] <- Sigma.y[i,j]/sqrt(Sigma.y[i,i]*Sigma.y[j,j])

    for (i in 1:p)
    for (j in 1:p)
        Sigmaalter[i,j] <- Sigma.alter[i,j]/sqrt(Sigma.alter[i,i]*Sigma.alter[j,j])

    Sigma.null[1:m1,1:m1] <- Sigma.x
    Sigma.null[(m1+1):(m1+m2), (m1+1):(m1+m2)] <- Sigma.y

    M <- solve(Sigma.null) %*% Sigma.alter
    G <- n* (sum(diag(M))-log(det(M))-(m1+m2))

    p.value <- 1- pchisq(G, df=2*m1*m2)
    p.value
}
`cor.balance` <-
function(x, m, G)
{
    p <- dim(x)[1]
    n <- dim(x)[2]
    D <- p/m
    if(D != G)
    stop("unqueal number of replicates or incorrect number of replicates or incorrect number of genes (random variables)")
    M <- matrix(NA, D, D)

    for(i in 1:(D-1))
    {
        for(j in (i+1):D)
        {
        subdat <- rbind(x[((i-1)*m + 1):((i-1)*m + m),], x[((j-1)*m + 1):((j-1)*m + m),])
        muhat <- c(rep(mean(subdat[1:m,]), m), rep(mean(subdat[(m+1):(2*m),]), m))
        MB <- matrix(0, nrow(subdat), nrow(subdat))
        for (k in 1:n)
            MB <- MB + (subdat[,k] - muhat)%o%(subdat[,k] - muhat)
        Sigmahat <- (MB/n)
        M[i,j] <- mean(c(Sigmahat[1:m,(m+1):(2*m)],Sigmahat[(m+1):(2*m),1:m]))
        M[j,i] <- M[i,j]
        }
    }
diag(M) <- 1
M
}

`cor.bootci` <-
function (x, y = NULL, m, G, alpha)
{
    if (is.data.frame(y))
        y <- as.matrix(y)
    else stopifnot(is.atomic(y))
    if (is.data.frame(x))
        x <- as.matrix(x)
    else {
        stopifnot(is.atomic(x))
        if (!is.matrix(x)) {
            if (is.null(y))
                stop("supply both x and y or a matrix-like x")
            x <- as.vector(x)
        }
    }
    p <- dim(x)[1]
    n <- dim(x)[2]
    D <- p/m
    if (D != G)
        stop("unqueal number of replicates or incorrect number of replicates or incorrect number of genes (random variables)")
    Bootdist <- c(NA, 1000)
    upperCI <- matrix(NA, D, D)
    lowerCI <- matrix(NA, D, D)
    for (i in 1:(D - 1)) {
        for (j in (i + 1):D) {
            subdat <- rbind(x[((i - 1) * m + 1):((i - 1) * m +
                m), ], x[((j - 1) * m + 1):((j - 1) * m + m),
                ])
            for (B in 1:1000) {
                subdatB <- subdat[, sample(seq(1:n), n, replace = TRUE)]
                muhatB <- c(rep(mean(subdatB[1:m, ]), m), rep(mean(subdatB[(m +
                  1):(2 * m), ]), m))
                ss0B <- (subdatB[, 1] - muhatB) %o% (subdatB[,
                  1] - muhatB)
                for (k in 2:n) ssB <- ss0B + (subdatB[, k] -
                  muhatB) %o% (subdatB[, k] - muhatB)
                SigmahatB <- ssB/n
                Bootdist[B] <- mean(c(SigmahatB[1:m, (m + 1):(2 *
                  m)], SigmahatB[(m + 1):(2 * m), 1:m]))
            }
            upperCI[i, j] <- quantile(Bootdist, probs = c(alpha/2,
                1 - alpha/2))[1]
            lowerCI[i, j] <- quantile(Bootdist, probs = c(alpha/2,
                1 - alpha/2))[2]
        }
    }
    list(upperCI, lowerCI)
}

`cor.unbalance` <-
function(x, m1, m2)
{
    n <- dim(x)[2]
    if(m1+m2 != nrow(x))
    stop("incorrect number of replicates, check m1 and m2")
    muhat <- c(rep(mean(x[1:m1,]), m1), rep(mean(x[(m1+1):(m1+m2),]), m2))
    M <- matrix(0, nrow(x), nrow(x))
    for (k in 1:n)
        M <- M + (x[,k] - muhat)%o%(x[,k] - muhat)
    Sigmahat <- M/n
    cor.unbal <- mean(c(Sigmahat[1:m1,(m1+1):(m1+m2)], Sigmahat[(m1+1):(m1+m2),1:m1]))
    cor.unbal
}

`permutest` <-
function (x, y=NULL, m, G)
{
    if (is.data.frame(y))
        y <- as.matrix(y)
    else stopifnot(is.atomic(y))
    if (is.data.frame(x))
        x <- as.matrix(x)
    else {
        stopifnot(is.atomic(x))
        if (!is.matrix(x)) {
            if (is.null(y))
                stop("supply both x and y or a matrix-like x")
            x <- as.vector(x)
        }
    }
    p <- dim(x)[1]
    n <- dim(x)[2]
    D <- p/m
    if (D != G)
        stop("unqueal number of replicates or incorrect number of replicates or incorrect number of genes (random variables)")
    PV <- matrix(NA, D, D)
    nulldist <- rep(NA, factorial(n))
    for (i in 1:(D - 1)) {
        for (j in (i + 1):D) {
            subdat <- rbind(x[((i - 1) * m + 1):((i - 1) * m +
                m), ], x[((j - 1) * m + 1):((j - 1) * m + m),
                ])
            muhat <- c(rep(mean(subdat[1:m, ]), m), rep(mean(subdat[(m +
                1):(2 * m), ]), m))
            ss0 <- (subdat[, 1] - muhat) %o% (subdat[, 1] - muhat)
            for (k in 2:n) ss <- ss0 + (subdat[, k] - muhat) %o%
                (subdat[, k] - muhat)
            Sigmahat <- ss/n
            cor0 <- mean(c(Sigmahat[1:m, (m + 1):(2 * m)], Sigmahat[(m +
                1):(2 * m), 1:m]))
            for (P in 1:(factorial(n))) {
                subdatP <- rbind(subdat[1:m, permutations(n)[P,
                  ]], subdat[(m + 1):(2 * m), ])
                muhatP <- c(rep(mean(subdatP[1:m, ]), m), rep(mean(subdatP[(m +
                  1):(2 * m), ]), m))
                ss0P <- (subdatP[, 1] - muhatP) %o% (subdatP[,
                  1] - muhatP)
                for (k in 2:n) ssP <- ss0P + (subdatP[, k] -
                  muhatP) %o% (subdatP[, k] - muhatP)
                SigmahatP <- ssP/n
                nulldist[P] <- mean(c(SigmahatP[1:m, (m + 1):(2 *
                  m)], SigmahatP[(m + 1):(2 * m), 1:m]))
            }
            PV[i, j] <- sum(abs(nulldist) > cor0)/factorial(n)
            PV[j, i] <- PV[i, j]
        }
    }
    diag(PV) <- 1
    PV
}

