Oh, and your message: In log(LikeGi(l, i, par[1], par[2])) : NaNs produced
means your LikeGi is returning something negative. Can't take the log of it... On Tue, Nov 29, 2011 at 8:09 PM, Florent D. <flo...@gmail.com> wrote: > 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.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. >> > ______________________________________________ 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.