I think that the optimizing function in Stan is fast and better than optim function in R, but the hessian matrix results (and, of course, the standard deviation (sd) of the MLEs) are different.
If X~N(mu, sigma), we can obtain the exact value of MLEs and their sds. By running the attached file, we can see that optim and sampling (in Stan) functions have good sd, but the optimizing function results is unacceptable (of course, only for the sd of the sigma parameter).
Thank you for checking the file and specifying my mistake
.
Best,
-Bistoon

I’m not a Stan developer. I don’t see very much difference in parameter estimate in mean and sd of fit1 and fit2, the hessian estimate differs in sigma/sigma. Is it that optimization is calculated at the unconstrained parameter space?

See: " hessian: ‘TRUE’ or ‘FALSE’ (the default): flag indicating whether to
calculate the Hessian (via numeric differentiation of the
gradient function in the unconstrained parameter space)."

Here, I think fit1 and fit2 have similar parameter space and optimizing method. The optim function seems to be a good result (in sd) and I want to get the same results with the optimizing function, but I have not been able to do this yet.

hessian: ‘TRUE’ or ‘FALSE’ (the default): flag indicating whether to
calculate the Hessian (via numeric differentiation of the
gradient function in the unconstrained parameter space).

I’ve taken my answer in this topic (Optimizing function: incorrect estimation of the standard deviation of MLE). This code is useful (thanks Dr. Goodrich):