[英]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.