Skip to content

Parameters setting in functions optimization

6 messages · Diane Bailleul, Florent D.

#
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
#
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:
#
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. <flodel at gmail.com> wrote:
#
Le 11/30/2011 2:09 AM, Florent D. a ?crit :

Thanks for your answer !
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.
My own function, otherwise it would be simplier to discuss about my 
problems.
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)

?

  
    
#
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
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
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.bailleul at u-psud.fr> wrote:
#
Dear Florent,
I know that I'm asking to optim to minimize my values, and that the 
results with a lower fvalue are best supported than those with a higher 
fvalue.
My comment was just from a data point of view. I'd like the lower ms 
(second parameter) as possible, as well as the fvalue. So a ms of 0.97 
(i.e. that 97% of my individuals are coming from outside the experiment 
plot) is very disapointing.

Dear John,
I can send you my data by email. It's very kind of you to offer to use 
my data as test for your new and coming optimx ! thank you.

Le 11/30/2011 12:08 PM, Florent D. a ?crit :