Error in cor.mtest when there are not enough finite observations
cor.mtest is basically a loop calling cor.test(x = mat[, i], y = mat[, j], ...)
When combinations of x and y do not have enough data, cor.test fails and thus cor.mtest.
Please catch this error, maybe with something along these lines:
if(sum(!is.na(mat[,i]*mat[,j])) > 2) {
tmp = cor.test(x = mat[, i], y = mat[, j], ...)
} else {
tmp <- list(p.value = NA, conf.int = NULL)
}
I am submitting a pull request to fix this issue.
The PR code returns the same list as suggested by @Gael-Hammer, but executes on any error message using tryCatch (rather than counting observations in an if statement). This should handle any other potential error encountered by cor.test. The error message is re-presented as a warning message for the user. The correlations that do not have enough observations are displayed in corrplot as "?" which matches the current behavior when cor.mtest encounters a variable with zero variance (a warning).
Here is a minimal reproducible example that fails with the current code but works with the PR:
library(corrplot)
dat <- matrix(rnorm(25), ncol = 5)
dat[2:4,1] <- NA # creates an error for cor.test
corr_mat <- cor(dat)
cor_test <- cor.mtest(dat)
corrplot(
corr_mat,
p.mat = cor_test$p,
sig.level = 0.05,
insig = "blank"
)