我有两个数组。例如,第一个是A,第二个是B:
A <-c(2,5,6,10,11)B <-c(13,2,6,8,12)
我也有常数teta = 1。我做了一个数组:
d = B-θ
和A与D之间的相关系数
koeff <-cor(A,D)
挑战是通过改变常数teta使koeff成为最大值。据我所知,我可以使用nloptr库。我应该如何使用它?
最好的祝福!
附:我使用求解器一般减少梯度在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
>
因此,所有斯皮尔曼相关性应该相同:
> 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
>