This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to compute exact P-values for a small data set for Kendall Correlation Matrix and implement it in the corr plot?

I am trying to create a corr plot for a Kendall correlation matrix of sample size 30. But I want to compute the exact P-values ( with no ties). Also, implement it in the corr plot for visualization purposes. I would like to find statistically significant correlation values.

corr biostatistics statistics r

What have you tried? Please show us your code at least a small but representative sample of your data

# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
# calculates P but says "cannot calculate exact P value with ties

cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}

p.mat <- cor.mtest(my_data , method="kendall")
head(p.mat[, 1:5])


#The corr plot and adding the P values to it 
M<-cor(my_data , method="kendall")
head(round(M,2))
png(height=3000, width=3000, pointsize=15, file="kendallcor_real_P_6.png")
corrplot(M, addCoef.col="black" , number.cex = 1.5, tl.cex = 2.5 , order = "AOE",p.mat=p.mat,sig.level = 0.05 )

I will specify the code I used to get P-values for the Kendall Correlation test in R, but the code cannot compute exact P-values with ties which I would like to improve

cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
    colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
    p.mat
}

# matrix of the p-value of the correlation
p.mat <- cor.mtest(my_data , method="kendall")

head(p.mat[, 1:5])

mat : is a matrix of data
... : further arguments to pass to the native R cor.test function

Is there any other methods where I can find the ties corrected P-values for Kendall's Correlation test and implement it in Corr Plot?

1 answer

This cor.mtest function calls cor.test which has a parameter called exact which computes exact p values. You can change your cor.mtest call like so:

p.mat <- cor.mtest(my_data, method = 'kendall', exact = TRUE)

Once you have that, you should be able to pass the matrix of correlation values and p.mat as the p-value matrix to corrplot.

Log in to answer this question.