【问题标题】:How to create a correlation matrix with significance levels in R?如何在R中创建具有显着性水平的相关矩阵?
【发布时间】:2015-10-27 10:35:37
【问题描述】:

我有一个大型数据集,函数 cor() 对区分高/低相关性没有多大帮助。

也许有人可以向我展示如何向相关矩阵添加颜色或星号 (* ** ***) 或其他内容的示例,以便我可以轻松地看到重要的值?

【问题讨论】:

标签: r


【解决方案1】:

heatmap 怎么样?

想象mtcars 是您的数据集。

您可以按照说明转换数据here

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

然后您可以计算出漂亮的热图,正如 here 所解释的那样

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

你得到

希望对您有所帮助。

您还可以根据this 添加带有+ geom_text(aes(fill = cormatrix$value, label = round(cormatrix$value, 1))) 的值。

【讨论】:

    【解决方案2】:

    您可以将关联结果返回到数据框,然后您可以排序、子集等。

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 2012-05-27
      • 1970-01-01
      • 2015-10-01
      • 2013-09-07
      • 1970-01-01
      • 2020-01-29
      • 2015-07-11
      相关资源
      最近更新 更多