Ravi Varadhan
2013-Feb-06 16:58 UTC
[R] Non linear programming: choose R that minimizes corr(y,x^R)
Hi John, Do you want to minimize correlation or |correlation|? Here is a simple code, with example, that shows how to optimize the magnitude (that is corr^2) of correlation between y and x^p, where p is a positive real number. You can modify it to suit your needs. corxy.opt <- function(x, y, interval=c(0,4), maximum=TRUE){ obj <- function(p, x, y) cor(y, x^p)^2 ans <- optimize(f=obj, interval=interval, maximum=maximum, x=x, y=y) ans } set.seed(321) x <- runif(100) y <- rnorm(100, mean=1-2*x, sd=1) corxy.opt(x, y) # Why minimizing does not make sense obj2 <- function(p, x, y) sapply(p, function(p) cor(y, x^p)^2) plot(seq(0,5,length=1000), obj2(seq(0,5,length=1000),x,y), type="l") Best, Ravi Ravi Varadhan, Ph.D. Assistant Professor The Center on Aging and Health Division of Geriatric Medicine & Gerontology Johns Hopkins University rvaradhan@jhmi.edu<mailto:rvaradhan@jhmi.edu> 410-502-2619 [[alternative HTML version deleted]]