Good afternoon everybody, I'm quite new in functions optimization on R and, whereas I've read lot's of function descriptions, I'm not sure of the correct settings for function like "optimx" and "nlminb". I'd like to minimize my parameters and the loglikelihood result of the function. My parameters are a mean distance of dispersion and a proportion of individuals not assigned, coming from very far away. The function LikeGi reads external tables and it's working as I want (I've got a similar model on Mathematica). My "final" function is LogLiketot : LogLiketot<- function(dist,ms) { res <- NULL for(i in 1:nrow(pop5)){ for(l in 1:nrow(freqvar)){ res <- c(res, pop5[i,l]*log(LikeGi(l,i,dist,ms))) } } return(-sum(res)) } dist is the mean dispersal distance (0, lots of meters) and ms the proportion of individuals (0-1). Of course, I want them to be as low as possible. I'd tried to enter the initials parameters as indicated in the tutorials : optim(c(40,0.5), fn=LogLiketot) >Error in 1 - ms : 'ms' is missing But ms is 0.5 ... So I've tried this form : optimx(c(30,50),ms=c(0.4,0.5), fn=LogLiketot) with different values for the two parameters : par fvalues method fns grs itns conv KKT1 KKT2 xtimes >2 19.27583, 25.37964 2249.698 BFGS 12 8 NULL 0 TRUE TRUE 57.5 >1 29.6787861, 0.1580298 2248.972 Nelder-Mead 51 NA NULL 0 TRUE TRUE 66.3 The first line is not possible but as I've not constrained the optimization ... but the second line would be a very good result ! Then, searching for another similar cases, I've tried to change my function form: LogLiketot<- function(par) { res <- NULL for(i in 1:nrow(pop5)){ for(l in 1:nrow(freqvar)){ res <- c(res, pop5[i,l]*log(LikeGi(l,i,par[1],par[2]))) } } return(-sum(res)) } where dist=par[1] and ms=par[2] And I've got : optimx(c(40,0.5), fn=LogLiketot) par fvalues method fns grs itns conv KKT1 KKT2 xtimes >2 39.9969607, 0.9777634 1064.083 BFGS 29 10 NULL 0 TRUE NA 92.03 >1 39.7372199, 0.9778101 1064.083 Nelder-Mead 53 NA NULL 0 TRUE NA 70.83 And I've got now a warning message : >In log(LikeGi(l, i, par[1], par[2])) : NaNs produced (which are very bad results in that case) Anyone with previous experiences in optimization of several parameters could indicate me the right way to enter the initial parameters in this kind of functions ? Thanks a lot for helping me ! Diane -- Diane Bailleul Doctorante Universit? Paris-Sud 11 - Facult? des Sciences d'Orsay Unit? Ecologie, Syst?matique et Evolution D?partement Biodiversit?, Syst?matique et Evolution UMR 8079 - UPS CNRS AgroParisTech Porte 320, premier ?tage, B?timent 360 91405 ORSAY CEDEX FRANCE (0033) 01.69.15.56.64
I also think your last write-up for LogLiketot (using a single argument "par") is the correct approach if you want to feed it to optim(). So now you have a problem with log(LikeGi(l, i, par[1], par[2])) for some values of par[1] and par[2]. Where is LikeGi coming from? a package or is it your own function? You could add some print statements (if you are familiar with "browser()" it is even better) so you may see what values of "par" are causing trouble. On Tue, Nov 29, 2011 at 1:15 PM, Diane Bailleul <diane.bailleul at u-psud.fr> wrote:> Good afternoon everybody, > I'm quite new in functions optimization on R and, whereas I've read lot's of > function descriptions, I'm not sure of the correct settings for function > like "optimx" and "nlminb". > I'd like to minimize my parameters and the loglikelihood result of the > function. > My parameters are a mean distance of dispersion and a proportion of > individuals not assigned, coming from very far away. > The function LikeGi reads external tables and it's working as I want (I've > got a similar model on Mathematica). > > My "final" function is LogLiketot : > LogLiketot<- function(dist,ms) > { > res <- NULL > for(i in 1:nrow(pop5)){ > ? ?for(l in 1:nrow(freqvar)){ > res <- c(res, pop5[i,l]*log(LikeGi(l,i,dist,ms))) > ? ?} > ? ? ? ?} > return(-sum(res)) > ? ? ? ? ? ?} > > dist is the mean dispersal distance (0, lots of meters) and ms the > proportion of individuals (0-1). > Of course, I want them to be as low as possible. > > I'd tried to enter the initials parameters as indicated in the tutorials : > optim(c(40,0.5), fn=LogLiketot) >>Error in 1 - ms : 'ms' is missing > But ms is 0.5 ... > > So I've tried this form : > optimx(c(30,50),ms=c(0.4,0.5), fn=LogLiketot) > with different values for the two parameters : > ? ? ? ? ? ? ? ? ? ?par ?fvalues ? ? ?method fns grs itns conv KKT1 KKT2 > xtimes >>2 ? ?19.27583, 25.37964 2249.698 ? ? ? ?BFGS ?12 ? 8 NULL ? ?0 TRUE TRUE >> 57.5 >>1 29.6787861, 0.1580298 2248.972 Nelder-Mead ?51 ?NA NULL ? ?0 TRUE TRUE >> 66.3 > > The first line is not possible but as I've not constrained the optimization > ... but the second line would be a very good result ! > > Then, searching for another similar cases, I've tried to change my function > form: > > LogLiketot<- function(par) > { > res <- NULL > for(i in 1:nrow(pop5)){ > ? ?for(l in 1:nrow(freqvar)){ > res <- c(res, pop5[i,l]*log(LikeGi(l,i,par[1],par[2]))) > ? ?} > ? ? ? ?} > return(-sum(res)) > ? ? ? ? ? ?} > > where dist=par[1] and ms=par[2] > > And I've got : > optimx(c(40,0.5), fn=LogLiketot) > ? ? ? ? ? ? ? ? ? ?par ?fvalues ? ? ?method fns grs itns conv KKT1 KKT2 > xtimes >>2 39.9969607, 0.9777634 1064.083 ? ? ? ?BFGS ?29 ?10 NULL ? ?0 TRUE ? NA >> ?92.03 >>1 39.7372199, 0.9778101 1064.083 Nelder-Mead ?53 ?NA NULL ? ?0 TRUE ? NA >> ?70.83 > And I've got now a warning message : >>In log(LikeGi(l, i, par[1], par[2])) : NaNs produced > (which are very bad results in that case) > > > Anyone with previous experiences in optimization of several parameters could > indicate me the right way to enter the initial parameters in this kind of > functions ? > > Thanks a lot for helping me ! > > Diane > > -- > Diane Bailleul > Doctorante > Universit? Paris-Sud 11 - Facult? des Sciences d'Orsay > Unit? Ecologie, Syst?matique et Evolution > D?partement Biodiversit?, Syst?matique et Evolution > UMR 8079 - UPS CNRS AgroParisTech > Porte 320, premier ?tage, B?timent 360 > 91405 ORSAY CEDEX FRANCE > (0033) 01.69.15.56.64 > > ______________________________________________ > 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. >
optimx does allow you to use bounds. The default is using only methods from optim(), but even though I had a large hand in those methods, and they work quite well, there are other tools available within optimx that should be more appropriate for your problem. For example, the current version of optimx should work quite well with lower and upper bounds specified, and you can see which methods work well by putting control=list(all.methods=TRUE). all.methods would be overkill for general use of course. I am hoping to have a new version of optimx up on R-forge within a week -- there are so many options to check -- that traps the NaNs etc. if it can, and also allows parameter and function scaling as well as several other new features. This is all experimental at the moment, but initial results are promising. This is less about "better methods" than about trapping errors and bad scaling etc. However, if you are able to share your script and data, I'll be happy to use it as a test and report back to you if you can communicate it to me off-list. Best, JN On 11/30/2011 06:00 AM, r-help-request at r-project.org wrote:> Message: 68 > Date: Tue, 29 Nov 2011 19:15:43 +0100 > From: Diane Bailleul <diane.bailleul at u-psud.fr> > To: r-help at r-project.org > Subject: [R] Parameters setting in functions optimization > Message-ID: <4ED5214F.7030504 at u-psud.fr> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Good afternoon everybody, > I'm quite new in functions optimization on R and, whereas I've read > lot's of function descriptions, I'm not sure of the correct settings for > function like "optimx" and "nlminb". > I'd like to minimize my parameters and the loglikelihood result of the > function. > My parameters are a mean distance of dispersion and a proportion of > individuals not assigned, coming from very far away. > The function LikeGi reads external tables and it's working as I want > (I've got a similar model on Mathematica). > > My "final" function is LogLiketot : > LogLiketot<- function(dist,ms) > { > res <- NULL > for(i in 1:nrow(pop5)){ > for(l in 1:nrow(freqvar)){ > res <- c(res, pop5[i,l]*log(LikeGi(l,i,dist,ms))) > } > } > return(-sum(res)) > } > > dist is the mean dispersal distance (0, lots of meters) and ms the > proportion of individuals (0-1). > Of course, I want them to be as low as possible. > > I'd tried to enter the initials parameters as indicated in the tutorials : > optim(c(40,0.5), fn=LogLiketot) > >Error in 1 - ms : 'ms' is missing > But ms is 0.5 ... > > So I've tried this form : > optimx(c(30,50),ms=c(0.4,0.5), fn=LogLiketot) > with different values for the two parameters : > par fvalues method fns grs itns conv KKT1 > KKT2 xtimes > >2 19.27583, 25.37964 2249.698 BFGS 12 8 NULL 0 TRUE > TRUE 57.5 > >1 29.6787861, 0.1580298 2248.972 Nelder-Mead 51 NA NULL 0 TRUE > TRUE 66.3 > > The first line is not possible but as I've not constrained the > optimization ... but the second line would be a very good result ! > > Then, searching for another similar cases, I've tried to change my > function form: > > LogLiketot<- function(par) > { > res <- NULL > for(i in 1:nrow(pop5)){ > for(l in 1:nrow(freqvar)){ > res <- c(res, pop5[i,l]*log(LikeGi(l,i,par[1],par[2]))) > } > } > return(-sum(res)) > } > > where dist=par[1] and ms=par[2] > > And I've got : > optimx(c(40,0.5), fn=LogLiketot) > par fvalues method fns grs itns conv KKT1 > KKT2 xtimes > >2 39.9969607, 0.9777634 1064.083 BFGS 29 10 NULL 0 TRUE > NA 92.03 > >1 39.7372199, 0.9778101 1064.083 Nelder-Mead 53 NA NULL 0 TRUE > NA 70.83 > And I've got now a warning message : > >In log(LikeGi(l, i, par[1], par[2])) : NaNs produced > (which are very bad results in that case) > > > Anyone with previous experiences in optimization of several parameters > could indicate me the right way to enter the initial parameters in this > kind of functions ? > > Thanks a lot for helping me ! > > Diane > > -- Diane Bailleul Doctorante Universit? Paris-Sud 11 - Facult? des Sciences d'Orsay Unit? > Ecologie, Syst?matique et Evolution D?partement Biodiversit?, Syst?matique et Evolution > UMR 8079 - UPS CNRS AgroParisTech Porte 320, premier ?tage, B?timent 360 91405 ORSAY CEDEX > FRANCE (0033) 01.69.15.56.64