I have $m$ variables $x_1,\dots,x_m$, measured in $N$ independent tests $\{x_{i1},\dots,x_{im}\}_{i=1}^N$, leading to the design matrix $X$. I noted that the demo function corrplot_intro.r from the R package corrplot includes a nice function cor.mtest (reported below), which computes the pairwise correlations for $x_1,\dots,x_m$ in the sample $X$ and reports the corresponding $p$-values:

cor.mtest <- function(mat, conf.level = 0.95){
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
    diag(p.mat) <- 0
    diag(lowCI.mat) <- diag(uppCI.mat) <- 1
    for(i in 1:(n-1)){
        for(j in (i+1):n){
            tmp <- cor.test(mat[,i], mat[,j], conf.level = conf.level)
            p.mat[i,j] <- p.mat[j,i] <- tmp$p.value
            lowCI.mat[i,j] <- lowCI.mat[j,i] <- tmp$conf.int[1]
            uppCI.mat[i,j] <- uppCI.mat[j,i] <- tmp$conf.int[2]
    return(list(p.mat, lowCI.mat, uppCI.mat))

However, it seems to me that no correction for multiple comparisons is performed, while it would be needed because I'm (blindly) testing for all correlations (see this nice answer). So, the $p$-values reported are not actually reliable. Am I right? Could you help me correct the code so that the effect of doing $m^2$ comparisons is correctly taken into account?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.