How can you measure confidence intervals in corrplot ()?
The corrplot()
possible to visualize the confidence intervals are numerically lower ratios?
library(corrplot)
M <- cor(mtcars)
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))
}
res1 <- cor.mtest(mtcars,0.95)
res2 <- cor.mtest(mtcars,0.99)
I would like to add confidence intervals low=res1[[2]]
and upp=res1[[3]]
how numbers below the correlation coefficients to the following graph .
corrplot(M, method="number")
source to share
corrplot
- pretty text table text()
. Therefore, we can try adding additional text to it.
Continuing with my example:
corrplot(cor(mtcars), method="number")
We form labels for the confidence interval:
conf <- paste0("[", format(res1[[2]], digits=1), ":", format(res1[[3]], digits=1), "]")
And add them as text to the existing one corrplot
:
xs <- row(res1[[1]])
ys <- (ncol(res1[[1]])+1) - col(res1[[1]])
text(xs, ys, conf, pos=1, cex=0.5)
NOTE: it looks like y = 1 starts at the top, so we need to invert it (which is why the expression is ys
more complex than xs
.
Here's the result:
source to share