[R] optim and "the function should not" advice

Gildas Mazo
Thu, 20 Sep 2012 08:24:28 -0000

Dear R users,

I'm using optim to optimize a pretty complicated function. This function
takes the parameter vector "theta" and within its body I use
instructions like

sigma<-theta[a:b]; computations with sigma...
out<-c()
for (i in 1:d){
a<-theta[(3*d+i):c]
out[i]<-evaluation of an expression involving 'a' (I use symbolic
differentiation)
}

Unfortunately for certain problems 'optim' returns a parameter vector
which didn't move at all from the initial parameters, and the output says
that although the function has been evaluated a high number of times, the
gradient (which I fed the function with) has been evaluated only one time. I
used the BFGS method.

By chance I looked at the help and I read "The parameter vector passed to
fn has special semantics and may be shared between calls: the function should
not change or copy it" . Could the instructions above be the cause of the
failure? If so, how to deal with symbolic differentation?

Thanks in advance,
Gildas



	[[alternative HTML version deleted]]