我有如下数据:
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"))
Run Code Online (Sandbox Code Playgroud)
如何计算 和 之间的斯皮尔曼等级相关x性y?
编辑
正如 @user20650 和 @dcarlson 评论所建议的,变量必须具有排名,使得一个值大于或小于另一个值。情况确实如此,因为North等East是根据其在文档中的存在情况排序的关键字。
要获得 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
Run Code Online (Sandbox Code Playgroud)
请注意有关关系的警告,这使得计算精确的 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
Run Code Online (Sandbox Code Playgroud)