I have an optimization model in R that utilizes a single variable in my objective function. See below:

    startx <- 1.25

    anstestoptimx<-optimx(startx,fn=testfunc,gr=NULL, hess=NULL, lower=1, upper=1.5, method="L-BFGS-B", itnmax = 50, hessian=FALSE,
         control=list(save.failures=TRUE, maximize=TRUE, ndeps= 0.1, factr=0.01, kkt=FALSE, trace=1))

I'm not including the code for the objective function 'testfunc' as it is rather long. But it uses one input variable, contains several filtering routines, calculates period returns, and returns a single output (a Sharpe Ratio for a portfolio). As you can see, it utilizs the optimx package and the "L-BFGS-B" method. This code works and optimizes to a reasonable solution.

I would like to expand this objective function to include more than one variable, but do not know what packages exist for multivariable objective functions that are similar to optimx.

Can anyone recommend a package for this need? I believe that "MCO" may be a feasible option, but the documentation for MCO isn't as comprehensive as optimx so I'm not sure it will function in a similar manner.

  • $\begingroup$ Hi: You should clarify what you mean by multivariate because most of the routines ( I think all of them actually ) allow for multiple unknown parameters. I think by you're use of the term "multivariate" you may be meaning something else ? Or maybe someone else knows what you mean and can clarify. $\endgroup$
    – mark leeds
    Sep 15, 2021 at 19:11
  • $\begingroup$ Good question and sorry for any confusion. I don't believe I mean parameters as yes I agree that most packages can accomodate multiple parameters. I mean unknown variables in the objective function. Perhaps I don't understand myself what I'm asking hopefully this helps. Currently my objective function is structured like: f(x). I would like this: f(x, y), or even f(x, y, ........., z). $\endgroup$
    – Jcarl
    Sep 15, 2021 at 19:25
  • 1
    $\begingroup$ Hi: I could be misunderstanding you or not understanding but I don't see how this stops you from using the optimx machinery. Are x and y known and does the objective function also involve parameters ? Maybe look at the optimx vignette for examples. John Nash has also written a book about optimization in R that might be helpful. I'm not sure of the best way to find examples but those are two ways. Also, maybe just google say: "optimization examples in R". There's also the cran optimization task view that describes what is available. There's a lot. $\endgroup$
    – mark leeds
    Sep 15, 2021 at 22:46
  • $\begingroup$ Thanks for your note. After some research/reading, I believe I didn't understand the capabilities of optimx. My applogies. $\endgroup$
    – Jcarl
    Sep 16, 2021 at 0:57
  • $\begingroup$ No problem. I'm glad to hear that you figured it out. $\endgroup$
    – mark leeds
    Sep 16, 2021 at 12:55

1 Answer 1


Just because others may experience the same problem, here is a short answer to this problem: To optimize a multi-variate problem with optimx (i.e. more than one parameter is optimized) you can create a vector that is passed to the function. So in your case, it would be something like:

startVals <- c(1.25, 1.0, ...)

anstestoptimx<-optimx(startVals ,fn=testfunc,gr=NULL, hess=NULL, 
                      lower=c(1,1,...), upper=c(1.5,1.5,...), 
                      method="L-BFGS-B", itnmax = 50, hessian=FALSE,
                      control=list(save.failures=TRUE, maximize=TRUE, 
                                   ndeps= 0.1, factr=0.01, kkt=FALSE, trace=1))

where the testfunc is a function that uses a vector instead of unique variables, so for example in the function x and y should be referred to like this: X[1] (= "x") X[2] (= "y").

You have to specify lower and upper in the same way as vector, e.g. lower=c(0,0,0,...) and upper = c(2,2,2,...) this enables you also to define parameter-specific bounds.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.