[英]How to calculate p-value for Kendall Tau correlation coefficients in R?
您可以簡單地遍歷數據的列(或行,如果您願意的話),以在每個列組合上使用cor.test()
,如下所示:
# Use some data
mat <- iris[,1:4]
# Index combinations of columns
# Not very efficient, but it'll do for now
idx <- expand.grid(colnames(mat), colnames(mat))
# Loop over indices, calculate p-value
pvals <- apply(idx, 1, function(i){
x <- mat[,i[[1]]]
y <- mat[,i[[2]]]
cor.test(x, y, method = "kendall")$p.value
})
# Combine indices with pvalues, do some sort of multiple testing correction
# Note that we are testing column combinations twice
# so we're overcorrecting with the FDR here
pvals <- cbind.data.frame(idx, pvals = p.adjust(pvals, "fdr"))
接下來,您必須用常規相關值補充這些值,並將這些值與 p 值結合起來。
# Calculate basic correlation
cors <- cor(mat, method = "kendall")
cors <- reshape2::melt(cors)
# Indices of correlations and pvalues should be the same, thus can be merged
if (identical(cors[,1:2], pvals[,1:2])) {
df <- cbind.data.frame(pvals, cor = cors[,3])
}
並以下列方式繪制數據:
# Plot a matrix
ggplot(df, aes(Var1, Var2, fill = ifelse(pvals < 0.05, cor, 0))) +
geom_raster() +
scale_fill_gradient2(name = "Significant Correlation", limits = c(-1, 1))
另一種選擇是使用idx <- t(combn(colnames(mat), 2))
,在這種情況下,多次測試更正是合適的,但您必須弄清楚如何操縱這些值以再次與相關性匹配.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.