# [R] How to obtain final gradient estimation from optim

Ott Toomet otoomet at econ.dk
Thu Mar 27 16:28:51 CET 2003

Hi,

| From: Stéphane Luchini <luchini at ehess.cnrs-mrs.fr>
|
| > No, and optim does not even necessarily calculate a gradient.
| > But if it does, it is supposed to be zero at a maximum....
|
| It is zero at the theoretical level, it is not zero at a numerical level and
| it can be used to compute the Outer Product of the Gradient as an estimator
| of the information matrix instead of the inverse hessian. For long
| computation, it enables one to get an estimation of standard deviations
| without computing the hessian matrix which can take a long time.

you can calculate information matrix _only_ if your gradient is in a
matrix form (as I understand this is what you call for the matrix of
component for each observation (or each individual).  I am not sure if
optim() can handle such (documentation says the function should return
a scalar result...).  In that case you may use BHHH method as

hess <- function(theta, ...) {
-t(g) %*% g
}

You may take a look at my package econ at
www.obs.ee/~siim/econ_0.0-4.tar.gz which includes BHHH method and can
return the gradient too (however, not in matrix form).  I should warn
you that it is not well documented nor tested.  If you are interested
in Newton-Raphson method, you may look at nlm() too.

| The matrix of the contribution to the gradient with a typical element $G_{ti}$
| defined as follows:
|
| $$G_{ti}(y,\theta) = \partial \ell_t(u,\theta) / \partial \theta_i$$

you mean

$$G_{ti}(y,\theta) = \partial \ell_i(u,\theta) / \partial \theta_t$$

where i means observations (individuals) and t parameters?  Or am I
misunderstanding something?

Perhaps it helps

Ott