【问题标题】:Correlation matrix and categorical variables相关矩阵和分类变量
【发布时间】:2021-10-17 12:47:34
【问题描述】:

我有数据框df,我展示了其中的前几行

age region    graduate salary
19  "North"   "no"     21000
25  "South"   "yes"    24000
23  "Center"  "yes"    23000
30  "South"   "no"     25000

其中region 可以是“北”、“中”或“南”,graduate 可以是“是”或“否”。我的目标是执行以下分析

library("corrplot")

df <- data.frame(age=c(19,25,23,30), region=c("North","South","Center","South"), graduate=c("no","yes","yes","no"), salary=c(21000,24000,23000,25000))
corrplot(cor(df), method='number')

但我收到以下错误: Error in cor(df) : 'x' must be numeric.

这里有什么解决方案? 是不是一定要把原来的数据库改成下面这样的

age region-North region-Center region-South graduate-yes graduate-no salary
19       1            0             0            0           1       21000
25       0            0             1            1           0       24000
23       0            1             0            1           0       23000
30       0            0             1            0           1       25000

然后重新启动代码?或者我可以直接在corrplot的方法中操作吗?目标是了解哪些变量对salary 的影响最大。

【问题讨论】:

  • 我认为您可以使用方差分析(ANOVA)来了解不同地区和毕业生的平均工资是否存在差异,而不是计算相关性

标签: r correlation categorical-data


【解决方案1】:

关联只能在数值变量之间进行。
但是在字符变量的情况下,可以使用以下来查找相关性

cor(rank(df$region), rank(df$graduate))

【讨论】:

  • 如果我将数据框中的region 替换为rank(region) 并将graduate 替换为rank(graduate),那么在新数据框中使用函数cor 是否有意义?我想找到一种方法来了解哪些变量对salary 的影响最大(请参阅我编辑的问题)。
  • 另一种方法是使用多元回归,例如lm(salary~age+region+graduate, df)
  • @LJG 我认为可以帮助您的一种方法是,使用 Boruta 包。以下是链接。 analyticsvidhya.com/blog/2016/03/…
猜你喜欢
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2021-12-13
  • 2017-01-12
  • 1970-01-01
  • 2018-09-25
相关资源
最近更新 更多