同一矩阵的 log 和 sqrt 的相关性

correlation of log and sqrt of the same matrix

我有一个包含 9 个变量的数据框 subs 我想获得对数与变量平方根之间的相关性。

所以我首先计算2个矩阵:

library(corrplot)

logs = log(subs)
sqroots = sqrt(subs)

然后我替换无限值(如果有的话):

logs = do.call(data.frame,lapply(logs, function(x) replace(x, 
is.infinite(x),NA)))

sqroots = do.call(data.frame,lapply(sqroots, function(x) replace(x, 
is.infinite(x),NA)))

然后我使用corrplot绘制相关矩阵:

corrplot(cor(logs,sqroots, use = "complete.obs"), order = "AOE")

但是报错:

Error in e1 > 0 : invalid comparison with complex values

我在这里做错了什么?在此先感谢您的帮助!

subs: https://pastebin.com/raw/y35FG2ZV

我认为问题在于您如何尝试摆脱无限值。 以下应该可以解决您的问题:

library(corrplot)

### Defining subs & onlyNum for the code to be reproducible###
subs <- matrix(nrow = 5, ncol = 5)
for(k in 1:5){subs[,k] <- sample(1:100, 5);rm(k)}
onlyNum <- 1:5

### To the answer ###
logs = log(subs[,onlyNum])
sqroots = sqrt(subs[,onlyNum])

### The 2 lines below should solve your issue ###
logs[which(is.infinite(logs), arr.ind = TRUE)] <- NA
sqroots[which(is.infinite(sqroots), arr.ind = TRUE)] <- NA

corrplot(cor(logs,sqroots, use = "complete.obs"), order = "AOE")

这是产生的输出:


更新

既然已经提供了数据,就可以进行编辑了。

(1) 由于 subs 是一个数据框,而 is.infinite 没有数据框的实现方法,因此必须使用 sapply(logs, is.infinite) 而不是 is.infinite(logs) 和分别为sqroots.

(2) 但是,如前所述,问题在于 order = "AOE":"AOE"(Angular 特征向量阶数)仅针对实值特征值定义,因为它需要检查阳性(c.f。?corrMatOrder)。计算相关矩阵的特征值得到:

> eigen(cor(logs,sqroots, use = "complete.obs"))$values
[1] 2.35892882+0.0000000i 1.69884142+0.0000000i 1.16180544+0.0000000i
[4] 0.99435961+0.0176823i 0.99435961-0.0176823i 0.89281529+0.0000000i
[7] 0.32520739+0.0000000i 0.29605683+0.0000000i 0.05592473+0.0000000i

因此必须选择 order 的另一个参数,例如order = "FPC"(第一主成分)。

好的,我的问题解决了。 我需要删除参数 order = "AOE" 原因可能是因为log和sqrt之间的相关性不对称,因此无法命令它们找到一个模式。

谢谢大家!