【问题标题】:How to build a Correlation Matrix in R splitted by categories?如何在 R 中建立按类别划分的相关矩阵?
【发布时间】:2017-10-30 19:43:31
【问题描述】:

我有一个包含 4 个变量的数据框 - cur、price、volume 和 carat。

除了 cut 之外,一切都是数值变量: 我能够得到价格、数量和克拉的相关矩阵。 这是下面的代码,

Jewels_Input_Data_2 <- subset(Jewels_Input_Data,select = c(price,Volume,carat))

#Correlation Plot to examine the relationships between price, volume, caret
library(corrplot)
CorMatrix <- cor(Jewels_Input_Data_2)

上图显示了所有有序切割水平的相关矩阵。我想为每个级别的剪辑显示它。

换句话说,我想通过分类变量来打破 CorMatrix - Cut 具有有序的级别。

说每个切割级别 - 我想查看矩阵值,理想情况下我想使用 corrplot() 绘制它

请帮帮我。

【问题讨论】:

  • CorList &lt;- lapply(split(Jewels_Input_Data_2, Jewels_Input_Data$cut), cor) 开始,然后绘制列表中的每个元素CorList

标签: r


【解决方案1】:

不太确定“Jewels_Input_Data”是什么,但作为一个可重现的示例:

library(dplyr)
library(purrr)
library(ggplot2)

diamonds %>% 
  mutate(volume = x*y*z) %>% 
  select(cut, price, volume, carat) %>% 
  split(.$cut) %>% 
  map(~ select(., -cut)) %>% 
  map(~ cor(.)) %>% 
  map(~ corrplot(., method = "number"))

【讨论】:

  • 因为这是 5 个图,也许你可以从 op &lt;- par(mfrow = c(2, 3)) 开始,绘制完成后重置 par(op)。无论如何,点赞
  • 它没有给我情节。但我正在根据需要获取矩阵。有什么问题,我可以做什么?它仍然给我同样的旧情节。
猜你喜欢
  • 1970-01-01
  • 2012-05-27
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 2015-09-23
  • 2020-02-04
相关资源
最近更新 更多