R - Kendall的tau-b和tau-c中的关联度量

bre*_*onk 16 statistics r distribution

是否有任何R软件包用于计算Kendall的tau-b和tau-c及其相关的标准误差?我对Google和Rseek的搜索没有任何结果,但肯定有人在R中实现了这些搜索.

dou*_*oug 41

三个 Kendall tau统计数据(tau-a,tau-btau-c).

它们不可互换,到目前为止发布的答案都没有涉及最后两个,这是OP问题的主题.

我无法在R 标准库(stat等人)或CRAN或其他存储库中可用的任何软件包中找到计算tau-b或tau-c的函数.我使用优秀的R Package sos进行搜索,所以我相信返回的结果是相当彻底的.

这就是OP问题的简短答案:tau-b或tau-c没有内置或包功能.

但是很容易推出自己的.

为Kendall统计编写R函数只需将这些方程式转换为代码:

Kendall_tau_a = (P - Q) / (n * (n - 1) / 2)

Kendall_tau_b = (P - Q) / ( (P + Q + Y0) * (P + Q + X0) ) ^ 0.5 

Kendall_tau_c = (P - Q) * ((2 * m) / n ^ 2 * (m - 1) )
Run Code Online (Sandbox Code Playgroud)

tau-a:等于一致的不一致对,除以一个因子来计算总对数(样本大小).

tau-b:关系的显式计算--ie,数据对的两个成员具有相同的值; 该值等于一致的负不一致对除以一个术语,该术语表示未绑定在x(X0)上的对的数量与未绑定在y(Y0)上的数量之间的几何平均值.

tau-c: 更大的表变体也针对非方形表进行了优化; 等于一致的负不一致对乘以一个调整表大小的因子).

# Number of concordant pairs.
P = function(t) {
  r_ndx = row(t)
  c_ndx = col(t)
  sum(t * mapply(function(r, c){sum(t[(r_ndx > r) & (c_ndx > c)])},
    r = r_ndx, c = c_ndx))
}

# Number of discordant pairs.
Q = function(t) {
  r_ndx = row(t)
  c_ndx = col(t)
  sum(t * mapply( function(r, c){
      sum(t[(r_ndx > r) & (c_ndx < c)])
  },
    r = r_ndx, c = c_ndx) )
}

# Sample size (total number of pairs).
n = n = sum(t)

# The lesser of number of rows or columns.
m = min(dim(t))
Run Code Online (Sandbox Code Playgroud)

所以你需要这四个参数来计算tau-a,tau-btau-c:

  • P

  • Q

  • ñ

(加XO&Y0的tau-B )


例如,tau-c的代码是:

kendall_tau_c = function(t){
    t = as.matrix(t) 
    m = min(dim(t))
    n = sum(t)
    ks_tauc = (m * 2 * (P(t) - Q(t))) / ((n ^ 2) * (m - 1))
}
Run Code Online (Sandbox Code Playgroud)

那么肯德尔的tau统计数据如何分类数据分析中使用的其他统计检验相关

所有三个Kendall tau统计数据,以及Goodman和Kruskal的伽玛都用于序数和二进制数据的相关性.(Kendall tau统计数据是伽马统计量的更复杂的替代方案(只是PQ).)

所以Kendalls的taugamma是简单的卡方Fisher精确测试的对应物,两者都是(据我所知)仅适用于标称数据.

例:

cpa_group = c(4, 2, 4, 3, 2, 2, 3, 2, 1, 5, 5, 1)
revenue_per_customer_group = c(3, 3, 1, 3, 4, 4, 4, 3, 5, 3, 2, 2)
weight = c(1, 3, 3, 2, 2, 4, 0, 4, 3, 0, 1, 1)

dfx = data.frame(CPA=cpa_group, LCV=revenue_per_customer_group, freq=weight)

# Reshape data frame so 1 row for each event 
# (predicate step to create contingency table).
dfx2 = data.frame(lapply(dfx, function(x) { rep(x, dfx$freq)}))

t = xtabs(~ revenue + cpa, dfx)

kc = kendall_tau_c(t)

# Returns -.35.
Run Code Online (Sandbox Code Playgroud)


And*_*ell 10

很长一段时间,但是这三个函数是在DescTools中实现的.

library(DescTools)
# example in: 
# http://support.sas.com/documentation/cdl/en/statugfreq/63124/PDF/default/statugfreq.pdf
# pp. S. 1821
tab <- as.table(rbind(c(26,26,23,18,9),c(6,7,9,14,23)))

# tau-a
KendallTauA(tab, conf.level=0.95)
tau_a    lwr.ci    ups.ci 
0.2068323 0.1771300 0.2365346 

# tau-b
KendallTauB(tab, conf.level=0.95)
    tau_b    lwr.ci    ups.ci 
0.3372567 0.2114009 0.4631126 

# tau-c
> StuartTauC(tab, conf.level=0.95)
     tauc    lwr.ci    ups.ci 
0.4110953 0.2546754 0.5675151 

# alternative for tau-b:
d.frm <- Untable(tab, dimnames = list(1:2, 1:5))
cor(as.numeric(d.frm$Var1), as.numeric(d.frm$Var2),method="kendall")
[1] 0.3372567

# but no confidence intervalls for tau-b! Check:
unclass(cor.test(as.numeric(d.frm$Var1), as.numeric(d.frm$Var2), method="kendall"))
Run Code Online (Sandbox Code Playgroud)


小智 5

今天偶然发现了这个页面,因为我正在寻找 R 中 kendall tau-b 的实现
对于其他正在寻找同样东西的人:
tau-b 实际上是 stats 包的一部分。

有关更多详细信息,请参阅此链接: https://stat.ethz.ch/pipermail/r-help//2012-August/333656.html

我尝试了一下,它有效:库(统计)

x <- c(1,1,2)
y<-c(1,2,3)
cor.test(x, y, method = "kendall", alternative = "greater")
Run Code Online (Sandbox Code Playgroud)

这是输出:

data:  x and y
z = 1.2247, p-value = 0.1103
alternative hypothesis: true tau is greater than 0
sample estimates:
      tau 
0.8164966 

Warning message:
In cor.test.default(x, y, method = "kendall", alternative = "greater") :
  Cannot compute exact p-value with ties
Run Code Online (Sandbox Code Playgroud)

只需忽略警告消息即可。tau 实际上是 tau b !!!

  • 事实上,`cor.test(x, y, method = "kendall")` 和 `cor(x, y, method = "kendall")` 都计算 Kendall 的 Tau-b。 (2认同)

小智 5

道格在这里的回答是不正确的。包 Kendall 可用于计算 Tau b。

Kendall 包函数 Kendall(它看起来也是 cor(x,y,method="kendall"))使用 Tau-b 的公式计算关系。但是,对于有关系的向量,Kendall 包具有更正确的 p 值。请参阅 Kendall 文档的第 4 页,来自https://cran.r-project.org/web/packages/Kendall/Kendall.pdf第 4 页,其中 D 指的是 Kendall 计算的分母:

并且 D = n(n ? 1)/2。S 称为分数,而分母 D 是 S 的最大可能值。当存在平局时,D 的公式更复杂(Kendall,1974,第 3 章),两个排名中平局的一般论坛是在我们的函数中实现。在无关联的零假设下,tau 的 p 值是在没有联系的情况下使用 Best 和 Gipps (1974) 给出的精确算法计算的。当存在关系时,通过将 S 视为具有均值零和方差 var(S) 的正态分布,使用具有连续性校正的正态近似,其中 var(S) 由 Kendall (1976, eqn 4.4, p.55) 给出。除非联系非常广泛和/或数据非常短,否则这种近似值就足够了。如果存在广泛的联系,那么引导程序提供了一个权宜之计(Davis 和 Hinkley,1997)。

我最初对 Doug 对此的回答进行了编辑,但因为“针对作者,更适合作为回答或评论”而被拒绝。我会留下这个作为对答案的评论,但我的声誉还不够高,无法发表评论。