繁体   English   中英

R中因子之间的斯皮尔曼等级相关性

[英]Spearman rank correlation between factors in R

我有如下数据:

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 评论所建议的那样,变量必须具有一个排名,使得一个值大于或小于另一个值。 这是这种情况,因为NorthEast等是根据它们在文档中的存在进行排序的关键字。

要获得 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 值变得困难。 您可以在包coin使用spearman_test来获取具有关系的数据:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM