簡體   English   中英

越來越多的變量的平均成對相關性 (R)

[英]Average pairwise correlations of an increasing number of variables (R)

我有一個名為“變量”的矩陣,其中包含 9 個變量(9 列)。 我已經用這個代碼獲得了成對相關矩陣:

matrix.cor <- cor(variables, method="kendall", use="pairwise")

現在我想獲得平均成對相關性作為所考慮變量數量的函數。 我的意思是,2 個變量、3 個變量、4 個變量......最多 9 個變量的所有可能相關性的平均值,以查看添加變量的效果。 我有這個 R 代碼(從一篇包含更多因素和列的文章中提取)但它運行不佳,我只獲得了考慮 9 個變量的平均值。

pairwisecor.df = ddply(data,c("Exp"),function(x) {
Smax = unique(x$Rich)
x = x[,variables]
cormat = cor(t(x),use="complete.obs",method=c("kendall"))
data.frame(
Smax = Smax,
no.fn = nrow(x),
avg.cor = mean(cormat[lower.tri(cormat)]) ) } )

我認為創建一個函數來分析累積數量的變量並不是很困難……但我只有一篇文章的參考資料,其中數據要復雜得多。
任何的想法?

這是一個虛構的示例,用於計算從左上角開始的下三角矩陣的大小逐漸增加的平均值:

> (cormat <- matrix((1:25)/25, 5, 5))
     [,1] [,2] [,3] [,4] [,5]
[1,] 0.04 0.24 0.44 0.64 0.84
[2,] 0.08 0.28 0.48 0.68 0.88
[3,] 0.12 0.32 0.52 0.72 0.92
[4,] 0.16 0.36 0.56 0.76 0.96
[5,] 0.20 0.40 0.60 0.80 1.00
> avg.cor = c()
> for (i in 2:dim(cormat)[1]) {
+   avg.cor=cbind(avg.cor,mean(cormat[1:i,1:i][lower.tri(cormat[1:i,1:i])]))
+ }
> avg.cor
     [,1]      [,2]      [,3] [,4]
[1,] 0.08 0.1733333 0.2666667 0.36

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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