受け入れられているソリューション(psychパッケージのcorr.test関数)は機能しますが、大きな行列の場合は非常に遅くなります。私は、薬剤感受性マトリックス(〜1,000 x〜500)に相関する遺伝子発現マトリックス(〜20,000 x〜1,000)を使用して作業していましたが、それが永遠にかかるため、停止する必要がありました。
psychパッケージからいくつかのコードを取得し、代わりにcor()関数を直接使用して、はるかに優れた結果を得ました。
# find (pairwise complete) correlation matrix between two matrices x and y
# compare to corr.test(x, y, adjust = "none")
n <- t(!is.na(x)) %*% (!is.na(y)) # same as count.pairwise(x,y) from psych package
r <- cor(x, y, use = "pairwise.complete.obs") # MUCH MUCH faster than corr.test()
cor2pvalue = function(r, n) {
t <- (r*sqrt(n-2))/sqrt(1-r^2)
p <- 2*(1 - pt(abs(t),(n-2)))
se <- sqrt((1-r*r)/(n-2))
out <- list(r, n, t, p, se)
names(out) <- c("r", "n", "t", "p", "se")
return(out)
}
# get a list with matrices of correlation, pvalues, standard error, etc.
result = cor2pvalue(r,n)
2つの100x200マトリックスを使用しても、その違いは驚異的でした。45秒に対して1秒または2秒。
> system.time(test_func(x,y))
user system elapsed
0.308 2.452 0.130
> system.time(corr.test(x,y, adjust = "none"))
user system elapsed
45.004 3.276 45.814