我需要计算数据框中的相关性并仅突出显示重要的相关性。我在stackoverflow上找到了这个函数。
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))
}
我操作我的数据库并计算相关性
my_df <- cor(my_df)
head(mydf)
> head(my_df)
## V1 V2 V3 V4 V5 V6 V7 V8
## V1 1.00000000 -0.09483036 0.08496371 0.3022391 0.5626154 -0.2483336 0.1096767 0.2441640
## V2 -0.09483036 1.00000000 0.45397343 0.8459913 -0.5454400 0.3609005 -0.2278882 -0.3699623
## V3 0.08496371 0.45397343 1.00000000 0.7579513 -0.1285077 -0.1381399 -0.5044181 -0.2756256
## V4 0.30223914 0.84599133 0.75795133 1.0000000 -0.2444336 0.1124788 -0.3283376 -0.2769132
## V5 0.56261538 -0.54543998 -0.12850765 -0.2444336 1.0000000 -0.4929598 0.1378570 0.2633717
## V6 -0.24833365 0.36090049 -0.13813990 0.1124788 -0.4929598 1.0000000 -0.2517067 0.3912901
我应用函数来获得重要性,然后创建一个corrplot
cor1 <- cor.mtest(my_df, 0.95)
plotted_corrmat <-corrplot(cor(my_df), p.mat = cor1[[1]] , insig = "blank", method = "color",
addCoef.col="black" ,
order = "original", tl.cex = 1/par("cex"),
cl.cex = 1/par("cex"), addCoefasPercent = TRUE)
但系数会发生变化(如果我在原始数据框中添加另一个变量-coloumn,或者如果我考虑数据框中两个旧变量与新变量之间的相关性,则系数也会发生变化)
head(plotted_corrmat)
## V1 V2 V3 V4 V5 V6 V7 V8
## V1 1.00000000 -0.4808666 -0.07645303 -0.1426384 0.7927122 -0.6805792 0.1983160 0.1665963
## V2 -0.48086660 1.0000000 0.73402255 0.9096073 -0.8505173 0.4304575 -0.6465926 -0.7499984
## V3 -0.07645303 0.7340225 1.00000000 0.9204683 -0.4071524 -0.0452260 -0.8033922 -0.7459460
## V4 -0.14263840 0.9096073 0.92046835 1.0000000 -0.5955864 0.1353852 -0.7492527 -0.8040587
## V5 0.79271216 -0.8505173 -0.40715236 -0.5955864 1.0000000 -0.7342590 0.4391327 0.4262626
## V6 -0.68057919 0.4304575 -0.04522600 0.1353852 -0.7342590 1.0000000 -0.3788302 0.2120725