# [R-sig-ME] Optimize multiple confounded parameters using optim()

Voeten, C.C. c@c@voeten @ending from hum@leidenuniv@nl
Fri May 18 09:23:00 CEST 2018

```Hi Keren,

This is not a question about mixed-effects regression, so it is probably better suited at a more general R help list, or on StackOverflow. But since I once had a similar problem, I can perhaps briefly answer your question anyway:

You appear to be after dynamic bounds for each of the parameters, depending on the value of the other parameter. Unfortunately, your code:

res = optim(c(a,b), test, lower=c(0,a), upper=c(b,1),method="L-BFGS-B")

will be expanded to:

res = optim(c(0,1), test, lower=c(0,0), upper=c(1,1),method="L-BFGS-B")

before optim() is called, since a=0 and b=1. What you want, however, is for this expansion to happen within the function to be evaluated itself. The way to do this is to set the bounds in your optim() call at the most extreme values for both parameters (i.e. c(0,0) and c(1,1), as you have inadvertently already done), and to have your function return infinity when it is presented with an infeasible solution. In other words:

test <- function (v) if (v > v) Inf else v-v

In the future, this kind of general question not specific to mixed-effects models is probably better posted on StackOverflow, but I hope this helps anyway.

Cesko

> -----Oorspronkelijk bericht-----
> Van: R-sig-mixed-models [mailto:r-sig-mixed-models-bounces at r-
> project.org] Namens Keren Halabi
> Verzonden: donderdag 17 mei 2018 13:07
> Aan: r-sig-mixed-models at r-project.org
> Onderwerp: [R-sig-ME] Optimize multiple confounded parameters using
> optim()
>
> Dear list,
>
> My apologies in advance if this is not the relevant forum for the below
> question.
>
> I wish to define a codon site model, which is mixture model over multiple
> dN/dS ratios.
> Thus, I want to constrain each  dN/dS ratio by its preceding ratio in the
> mixture and its following ratio in the mixture. I was thinking of using the
> bounds parameter of the optim() function to achieve this.
>
> However, I am experiencing an issue while attempting to optimize a function
> with regards to multiple parameters. Specifically, due to setting the bounds
> to be dependent on one another.
>
> Here is a basic example: say that I want to optimize the below function
> named "test', with regards to vector v, with the following constraint:
> 0<=v<=v<=1:
> test <-function(v=c(0,1)) {return(v-v)}
>
> Now, calling optim() with the following settings:
> a=0
> b=1
> res = optim(c(a,b), test, lower=c(0,a), upper=c(b,1),method="L-BFGS-B")
>
> Yields optimized values:
> a=1
> b=0
> test(c(a,b))=-1
>
> It appears that the constraint was not satisfied, but the bounds still had some
> affect on the result. This makes me suspect that I didn't set the lower and
> upper bounds correctly when calling optim().
> Could you please let me know what I did wrong?
>
> Many thanks!
> Keren
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

```