Reputation: 1305
I have the following dataframes and I would like to find the global maximum of the optimised_vector
set.seed(1)
xxx<-data.frame(C1 = c("obs1","obs2","obs3","obs4","obs5","obs6"),
X1 = round(runif(6, 1, 6),digits=1),
X2 = round(runif(6, 1, 6),digits=1),
X3 = round(runif(6, 1, 6),digits=1),
X4 = round(runif(6, 1, 6),digits=1),
X5 = round(runif(6, 1, 6),digits=1)
)
yyy<-data.frame(C1 = c("obs1","obs2","obs3","obs4","obs5","obs6"),
YY = c(1,2,3,4,5,6)
)
#optimised_vector<-c(1,1,1,1,1,1)
optimised_vector<-c("coef1","coef2","coef3","coef4","coef5","coef6")
So I've built these functions to try to find the maximum correlation between my dummy data. But I would like to be able to impose some constraints, like non-negativity and max of 0.75 for each of the elements of the optimised_vector
inner_function <- function(xxx,optimised_vector) {
scoring <- rowSums(xxx[,2:6] * optimised_vector)
return (scoring)
}
maximiser <- function(optimised_vector) {
1-cor(
yyy$YY,
inner_function(xxx,optimised_vector),
method="kendall", use="pairwise")
}
And then I run optim to get the results...
optim(par = c(1,1,1,1,1,1),
fn = maximiser)
Is this correct, does it even find the global convergence points and how can I impose constraints?
Upvotes: 1
Views: 732
Reputation: 764
Using bounderies is possible with the method L-BFGS-B in
optim(par = c(1,1,1,1,1,1),
fn = maximiser,
method = "L-BFGS-B",
lower = rep(0, 4),
upper = rep(0.75, 4))
Upvotes: 1