taiyun / corrplot

A visual exploratory tool on correlation matrix
https://github.com/taiyun/corrplot
Other
316 stars 86 forks source link

Define correlated variable in correlation matrix #90

Closed Navien2 closed 7 years ago

Navien2 commented 7 years ago

I have used the code bellow to detect correlation between the variable in my matrix which is 1302x8 and i transpose the matrix and the r result is 1302x1302, then I pick the upper triangle of the corr matrix then I'am trying to flatten it to see the correlated variable, but the code has issue:

A <- as.matrix(Adata2)
res <- cor(t(A))
round(res, 2)
library(Hmisc)
res2 <- rcorr(t(A))
cormat <- res2$r
pmat <- res2$p

# ++++++++++++++++++++++++++++
# flattenCorrMatrix
# ++++++++++++++++++++++++++++
# cormat : matrix of the correlation coefficients
# pmat : matrix of the correlation p-values
flattenCorrMatrix <- function(cormat, pmat) {
  ut <- upper.tri(cormat)
  data.frame(
    row = rownames(cormat)[row(cormat)[ut]],
    column = rownames(cormat)[col(cormat)[ut]],
    cor  = (cormat)[ut],
    p = pmat[ut]
  )
}
flattenCorrMatrix(res2$r, res2$P)

The error is:

flattenCorrMatrix(res2$r, res2$P)
Error in data.frame(row = rownames(cormat)[row(cormat)[ut]], column = rownames(cormat)[col(cormat)[ut]],  : 
  arguments imply differing number of rows: 0, 14196
Called from: data.frame(row = rownames(cormat)[row(cormat)[ut]], column = rownames(cormat)[col(cormat)[ut]], 
    cor = (cormat)[ut], p = pmat[ut])
vsimko commented 7 years ago

I'm not sure how this relates to corrplot ? Moreover, such a large matrix will render very slowly in corrplot.

vsimko commented 7 years ago

Would this help ?

# generating some random data 1302x8
some_random_data <- rnorm(1302)
Adata2 <- data.frame(
  a = rnorm(1302),
  b = some_random_data,
  c = rnorm(1302),
  d = some_random_data,
  e = rnorm(1302),
  f = rnorm(1302),
  g = -some_random_data,
  h = rnorm(1302))

A <- as.matrix(Adata2)
res <- cor(t(A))

# reorder variables
library(corrplot)
ord <- corrMatOrder(res, order = "FPC")
res <- res[ord, ord]

# plot the matrix including legend
library(raster)
plot(raster(res))

image