【问题标题】:Spearman rank correlation between factors in RR中因子之间的Spearman等级相关性
【发布时间】:2020-05-18 03:18:51
【问题描述】:

我有如下数据:

directions <- c("North", "East", "South", "South")
x<-factor(directions, levels= c("North", "East", "South", "West"))

cities <- c("New York","Rome","Paris","London")
y<-factor(cities, levels= c("New York","Rome","Paris","London"))

如何计算 xy 之间的 Spearman 等级相关性?

编辑

正如@user20650 和@dcarlson cmets 所建议的,变量必须有一个排名,使得一个值大于或小于另一个值。之所以如此,是因为NorthEast 等是根据它们在文档中的存在情况排序的关键字。

【问题讨论】:

  • ...虽然根据分类数据的假设使用卡方检验、互信息等更为常见
  • Spearman 是一个等级相关系数,它假设变量的等级使得一个值大于或小于另一个值。您的数据不具有该属性,因为 North 不大于 West 或小于 East。纽约不比罗马大,也不比伦敦小。正如@user20650 建议的那样,您需要使用卡方。
  • 我编辑了这篇文章。谢谢你。我错过了一个重要信息。

标签: r correlation


【解决方案1】:

要获得 Spearman 与因子的相关性,您必须将它们转换为它们的基础数字代码:

cor(as.numeric(x), as.numeric(y), method="spearman")
# [1] 0.9486833
cor.test(as.numeric(x), as.numeric(y), method="spearman")
# 
#   Spearman's rank correlation rho
# 
# data:  as.numeric(x) and as.numeric(y)
# S = 0.51317, p-value = 0.05132
# alternative hypothesis: true rho is not equal to 0
# sample estimates:
#       rho 
# 0.9486833 
# 
# Warning message:
# In cor.test.default(as.numeric(x), as.numeric(y), method = "spearman") :
#   Cannot compute exact p-value with ties

请注意有关导致难以计算精确 p 值的关系的警告。您可以使用 spearman_test 包中的 coin 来获取有关联的数据:

library(coin)
spearman_test(as.numeric(x)~as.numeric(y))
# 
#   Asymptotic Spearman Correlation Test
# 
# data:  as.numeric(x) by as.numeric(y)
# Z = 1.6432, p-value = 0.1003
# alternative hypothesis: true rho is not equal to 0

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    • 2018-03-15
    • 2017-05-11
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多