有three 肯德尔 tau 统计数据 (tau-a, tau-b, and tau-c).
他们是not可以互换,到目前为止发布的答案都没有涉及最后两个,这是OP问题的主题。
我无法在 R 中找到计算 tau-b 或 tau-c 的函数标准库 (统计等.) 或 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:明确核算ties--即数据对的两个成员具有相同的值;该值等于一致减去不一致对除以 a表示未绑定的对数之间的几何平均值的术语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, and tau-c:
(plus XO & Y0 for tau-b)
例如,代码为tau-c is:
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)的更复杂的替代品。)
所以肯德尔斯的tau和gamma是简单的对应物卡方 and Fisher 精确检验,两者(据我所知)仅适用于名义数据.
example:
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.