繁体   English   中英

如何使用nloptr库最大化两个数组之间的Spearman相关性

[英]How to use of nloptr library to maximize Spearman correlation between two arrays

我有两个数组。 例如,第一个是A,第二个是B:

A <-c(2,5,6,10,11)B <-c(13,2,6,8,12)

我也有常数teta = 1。 我做了一个数组:

D = B-teta

和D之间的相关系数

koeff <-cor(A,D)

挑战在于通过更改常数teta来使koeff达到最大值。 据我了解,我可以使用nloptr库。 我应该如何使用它?

最好的祝福!

PS我在Excel中使用求解器一般减小的梯度进行了这项工作。我在R中找不到此功能。

log(B+ϑ)不会更改顺序的说明。

> # ranks for different theta
> theta <- -1; rank(log(B+theta))
[1] 5 1 2 3 4
> theta <- 3; rank(log(B+theta))
[1] 5 1 2 3 4
> theta <- 10; rank(log(B+theta))
[1] 5 1 2 3 4
> # theta=-2 is a border case: we get -infinity
> theta=-2; log(B+theta)
[1] 2.397895     -Inf 1.386294 1.791759 2.302585
> rank(log(B+theta))
[1] 5 1 2 3 4
>

结果,所有Spearman相关性都应该相同:

> A <- c(2,5,6,10,11)
> cor(A,B,method="spearman")
[1] 0
> theta <- -1; cor(A,log(B+theta),method="spearman")
[1] 0
> theta <- 3; cor(A,log(B+theta),method="spearman")
[1] 0
> theta <- 10; cor(A,log(B+theta),method="spearman")
[1] 0
> theta <- -2; cor(A,log(B+theta),method="spearman")
[1] 0
> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM