[R] a question about gradient in optim

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 23 10:50:03 CEST 2006

On Tue, 23 May 2006, Mehmet Balcilar wrote:

> Hi,
> I am using optim to estimate the parameters of a smooth transition
> autoregressive model.  I  wrote a function to return the  gradient  for
> each observation. So, for k parameters and n observations my function
> returns a (n x k) matrix. The gr argument of optim expects a (k x 1)
> vector.  Now,  what is the correct way of passing my gradient matrix to
> optim?

gr is documented to be a function, not a vector!

Since you are optimizing a numeric function such as the log-likelihood, 
the derivative is a vector, not a matrix, and your gr function should 
return a vector.  We have far too few details, but for example did you 
forget to sum over observations?

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list