mix
mix

Reputation: 1

How to create a correlation matrix with significance levels in R?

I have a large data set and the function cor() doesn't help much to distinguish between high/low correlations.

Maybe someone can show me an example how to add colours or stars (* ** ***) or something to the correlation matrix, so I can easily see significant values?

Upvotes: 0

Views: 1691

Answers (2)

giac
giac

Reputation: 4309

What about a heatmap ?

Imagine mtcars is your dataset.

You can transform the data as explain here

ccor = cor(mtcars[,3:10]) # whatever variables 
cormatrix = arrange( melt(ccor), -abs(value) )

Then you can compute a nice heatmap, as explain here

ggplot(cormatrix, aes(Var1, Var2) ) + geom_tile(aes(fill = value), colour = "white") + scale_fill_gradient(low = "white", high = "steelblue")

You get

enter image description here

Hope this help.

Also you can add the values with + geom_text(aes(fill = cormatrix$value, label = round(cormatrix$value, 1))) according to this.

Upvotes: 1

Benjamin
Benjamin

Reputation: 17289

You can return the results of your correlations to a data frame, and then you can sort, subset, etc.

library(broom)
library(dplyr)

cor.list <- list(NULL)
length(cor.list) <- length(mtcars)^2

for(i in seq_along(mtcars)){
  for(j in seq_along(mtcars)){
    cor.list[[(i-1)*11 + j]] <- 
      tidy(cor.test(mtcars[, i], mtcars[, j])) %>%
      mutate(x = names(mtcars)[i],
             y = names(mtcars)[j])
  }
}

bind_rows(cor.list)

Upvotes: 0

Related Questions