With optimx(c(30,50),ms=c(0.4,0.5), fn=LogLiketot) where
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)) } I think it will do something like this for the first call to LogLiketot: LogLiketot(c(30,50), ms=c(0.4,0.5)) which is obviously not the usage you had in mind. Also, I see you say the results for the bad usage above: 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 look very good but you do not comment about the results for the correct usage of optimx: 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 Do you realize optimx is trying to _minimize_ your function? See that the fvalues from the correct usage are much better (smaller) than you first (bad) usage. On Wed, Nov 30, 2011 at 4:16 AM, Diane Bailleul <diane.baill...@u-psud.fr> wrote: > Le 11/30/2011 2:09 AM, Florent D. a écrit : > > Thanks for your answer ! > >> 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(). > > I'm not dedicated to optim() fonction. I just want to optimise my two > parameters and the loglikelihood result, and if there's a better fonction > for that, I wish I could use it. > > >> 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? > > My own function, otherwise it would be simplier to discuss about my > problems. > > >> 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. > > I'm not familiar, but I'll search about browser(). > > If the function with par is correct, any idea of what I've made with this : > > optimx(c(30,50),ms=c(0.4,0.5), fn=LogLiketot) > > > ? > > > >> >> >> On Tue, Nov 29, 2011 at 1:15 PM, Diane Bailleul >> <diane.baill...@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@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. >>> > > > -- > 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@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.