簡體   English   中英

如何計算 R 中 Kendall Tau 相關系數的 p 值?

[英]How to calculate p-value for Kendall Tau correlation coefficients in R?

我已經使用以下方法計算了 Kendal 相關系數:

corr_test <- cor.test(values, use = "pairwise", method="kendall")
corr_test

但我需要 p 值。 我找不到任何為 Kendall 相關性提供 p 值的包。

如何計算 Kendall tau 相關系數的 p 值?

此任務的目標是生成相關圖,其中彩色單元格表示顯着的相關系數。 我使用 Kendall tau 是因為我的數據中有很多聯系,一個變量是一個因素。

在此處輸入圖片說明

您可以簡單地遍歷數據的列(或行,如果您願意的話),以在每個列組合上使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM