有 三个 Kendall tau 统计数据(tau-a、tau-b 和 tau-c).
它们不可互换,到目前为止发布的答案都没有涉及最后两个,这是 OP 问题的主题。
我无法在 R Standard Library (stat et al.) 或任何CRAN 或其他存储库上提供的软件包。我使用了优秀的 R 包 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) )
tau-a: 等于一致减去不一致对,再除以一个因子以计算对总数(样本大小)。
tau-b:明确说明关系——即数据对的两个成员具有相同的值;此值等于一致减去不一致对除以 项,该项表示 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))
所以这四个参数就是计算tau-a、tau-b和tau-c所需的全部:
(加上 tau-b 的 XO 和 Y0)
例如,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))
}
那么,Kendall 的 tau 统计与分类数据分析中使用的其他统计测试有什么关系?
所有三个 Kendall tau 统计量以及 Goodman 和 Kruskal 的 gamma 都用于序数和二进制数据的相关性。 (Kendall tau 统计是 gamma 统计(仅 P-Q)的更复杂的替代方案。)
因此,Kendalls 的 tau 和 gamma 对应于简单的 卡方 和 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.