简体   繁体   中英

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

I have two arrays. For example the first one is A and the second B:

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

Also I have constant teta=1. I made an array :

D=B-teta

and correlation coefficient between A and D

koeff<-cor(A,D)

The challenge is make koeff the maximum value by changing constant teta. As I understood I can use nloptr library. How should I use use it?

Best regards!

PS I made this work in excel with solver general reduced gradient.I have not find this function in R.

Illustration that log(B+ϑ) does not change the ordering.

> # 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
>

As a result all Spearman correlations should be the same:

> 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
> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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