Hi all, I am trying to rebuild the results of a study using a different data set. I'm using about 450 observations. The code I've written seems to work well, but I have some troubles minimizing the negative of the LogLikelyhood function using 5 free parameters. As starting values I am using the result of the paper I am rebuiling. The system.time of the calculation of the function is about 0.65 sec. Since the free parameters should be within some boundaries I am using the following command: optim(fn=calculateLogLikelyhood, c(0.4, 2, 0.4, 80000, 0.8), lower=c(0,0,0,0,0), upper=c(1, 5, Inf, Inf, 1), control=list(trace=1, maxit=1000)) Unfortunately the result doesn't seem to be reasonable. 3 of the optimized parameters are on the boundaries. Unfortunately I don't have much experience using optimizatzion methods. That's why I am asking you. Do you have any hints for me what should be taken into account when doing such an optimization. Is there a good way to implement the boundaries into the code (instead of doing it while optimizing)? I've read about parscale in the help-section. Unfortunately I don't really know how to use it. And anyways, could this help? What other points/controls should be taken into account? I know that this might be a bit little information about my current code. But I don't know what you need to give me some advise. Just let me know what you need to know. Thankds
On Mon, 5 Jul 2010, Fabian Gehring wrote:> Hi all, > > I am trying to rebuild the results of a study using a different data set. I'm > using about 450 observations. The code I've written seems to work well, but I > have some troubles minimizing the negative of the LogLikelyhood function > using 5 free parameters. > > As starting values I am using the result of the paper I am rebuiling. The > system.time of the calculation of the function is about 0.65 sec. Since the > free parameters should be within some boundaries I am using the following > command: > > optim(fn=calculateLogLikelyhood, c(0.4, 2, 0.4, 80000, 0.8), > lower=c(0,0,0,0,0), upper=c(1, 5, Inf, Inf, 1), control=list(trace=1, > maxit=1000)) > > Unfortunately the result doesn't seem to be reasonable. 3 of the optimized > parameters are on the boundaries.You haven't said why this is unreasonable. A suggestion: Profile the loglikelihood around the starting value and around the putative maximum. (This might help with the parscale issue.) Also, you might try something like apply(rbind(0,eps*as.matrix(expand.grid(rep(list(c(-1,0,1)),5)))),1, function(x) calculateLogLikelyhood( x + y ) ) where y is the starting value (or the value achieved by optim()) and 'eps' is small enough to make small changes in the function value might help you see what gives. (It might be necessary to scale each column in as.matrix(...) separately, 'though.) In addition to inspecting the results by eyeball, you can fit the results to a quadratic form in rbind(...) using lm() and then figure out roughly where to go to find the maximum of your function. If this isn't enough to get you started, at least it might enable you to say more clearly what is not reasonable about your results. HTH, Chuck> > Unfortunately I don't have much experience using optimizatzion methods. > That's why I am asking you. Do you have any hints for me what should be > taken into account when doing such an optimization. > > Is there a good way to implement the boundaries into the code (instead of > doing it while optimizing)? I've read about parscale in the help-section. > Unfortunately I don't really know how to use it. And anyways, could this > help? What other points/controls should be taken into account? > > I know that this might be a bit little information about my current code. But > I don't know what you need to give me some advise. Just let me know what you > need to know. > > Thankds > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
> -----Mensaje original----- > De: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] En nombre de Fabian Gehring > Enviado el: lunes, 05 de julio de 2010 21:53 > Para: r-help at r-project.org > Asunto: [R] selection of optim parameters > > Hi all, > > I am trying to rebuild the results of a study using a > different data set. I'm using about 450 observations. The > code I've written seems to work well, but I have some > troubles minimizing the negative of the LogLikelyhood > function using 5 free parameters. > > As starting values I am using the result of the paper I am rebuiling. > The system.time of the calculation of the function is about 0.65 sec. > Since the free parameters should be within some boundaries I > am using the following command: > > optim(fn=calculateLogLikelyhood, c(0.4, 2, 0.4, 80000, 0.8), > lower=c(0,0,0,0,0), upper=c(1, 5, Inf, Inf, 1), control=list(trace=1, > maxit=1000)) > > Unfortunately the result doesn't seem to be reasonable. 3 of > the optimized parameters are on the boundaries.1) Your parameters seem to vary over several orders of magnitude. The control argument has a parscale parameter that can be used to re-scale all parameters to the same order of magnitude. Alternatively, your could estimate the log of your parameters, say par=c(log(0.4), log(2), log(0.4), log(80000), log(0.8) (and equivalent changes in lower and upper), and in your function, instead of the parameter value, use exp(parameter value9. That way the _numerical optimization_ occurs in the log space whereas the _function evaluation_ occurs in the original space. This transformation approach would make your parameter estimates and their hessian matrix (in case you are interested in the hessian) be output in the transformed space, so estimates and their covariance matrix will have to be back-transformed. For estimates just use exp(), whereas for the covariance matrix you might have to use something like Taylor series. 2) Did you use "L-BFGS-B" in the method argument of optim()? This method admits box-constrained optimization whereas the default (which you seem to be using, Nelder-Mead) in unconstrained, as far as I know.> Unfortunately I don't have much experience using > optimizatzion methods. > That's why I am asking you. > Do you have any hints for me what should be taken into > account when doing such an optimization. > > Is there a good way to implement the boundaries into the code > (instead of doing it while optimizing)? I've read about > parscale in the help-section. Unfortunately I don't really > know how to use it. And anyways, could this help? What other > points/controls should be taken into account?About using parscale, see Ravi Varadhan's post "Re: optim() not finding optimal values" the 27th of June. HTH Rub?n ____________________________________________________________________________________ Dr. Rub?n Roa-Ureta AZTI - Tecnalia / Marine Research Unit Txatxarramendi Ugartea z/g 48395 Sukarrieta (Bizkaia) SPAIN
Without the data and objective function, it is fairly difficult to tell what is going on. However, we can note: - no method is specified, but it would have to be L-BFGS-B as this is the only one that can handle box constraints - the fourth parameter is at a different scale, and you are right to think about this, especially as you don't seem to be providing analytic gradients. Options: - I would definitely rescale your function i.e., in the code, so your rawpar[4] is 10000*par[4]. That gets all the scales approx. the same, assuming your start reflects the eventual answer - try bobyqa from minqa package once scaled -- it really doesn't like scale differences. If you have data and a script nicely packaged that can be emailed, I'll take a look at it. If analytic gradients can be provided, there are more possibilities too, e.g., Rvmmin. spg from BB package also does constraints. It may, of course, turn out that the solution really is on the boundary. JN> Message: 47 > Date: Mon, 05 Jul 2010 21:53:18 +0200 > From: Fabian Gehring <fabian.gehring at bluewin.ch> > To: r-help at r-project.org > Subject: [R] selection of optim parameters > Message-ID: <4C32382E.3050409 at bluewin.ch> > Content-Type: text/plain; charset=ISO-8859-15; format=flowed > > Hi all, > > I am trying to rebuild the results of a study using a different data > set. I'm using about 450 observations. The code I've written seems to > work well, but I have some troubles minimizing the negative of the > LogLikelyhood function using 5 free parameters. > > As starting values I am using the result of the paper I am rebuiling. > The system.time of the calculation of the function is about 0.65 sec. > Since the free parameters should be within some boundaries I am using > the following command: > > optim(fn=calculateLogLikelyhood, c(0.4, 2, 0.4, 80000, 0.8), > lower=c(0,0,0,0,0), upper=c(1, 5, Inf, Inf, 1), control=list(trace=1, > maxit=1000)) > > Unfortunately the result doesn't seem to be reasonable. 3 of the > optimized parameters are on the boundaries. > > Unfortunately I don't have much experience using optimizatzion methods. > That's why I am asking you. > Do you have any hints for me what should be taken into account when > doing such an optimization. > > Is there a good way to implement the boundaries into the code (instead > of doing it while optimizing)? I've read about parscale in the > help-section. Unfortunately I don't really know how to use it. And > anyways, could this help? What other points/controls should be taken > into account? > > I know that this might be a bit little information about my current > code. But I don't know what you need to give me some advise. Just let me > know what you need to know. > > Thankds > >