Skip to content
Prev 16587 / 20628 Next

Optimize multiple confounded parameters using optim()

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[1] > v[2]) Inf else v[2]-v[1]

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