problem in using optim

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

problem in using optim

Carol Gao
Dear R users,

When I was trying to use the function *optim* to get the MLEs, quite a few
warning messages showed up as below:

Warning in log(psi * lam) : NaNs produced

I am just wondering what does this mean? Was it something wrong with my
likelihood function or was it sth wrong with the data?

Thanks in advance.

Carol Gao

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem in using optim

Arun.stat
just check whether some values in the parameter space forcing the log() function to apply logarithm on negative values !!!
Reply | Threaded
Open this post in threaded view
|

Re: problem in using optim

Ravi Varadhan
In reply to this post by Carol Gao
It probably means that your parameters are taking on values that are
infeasible.  You may have to impose constraints on your parameters to get a
solution (e.g. bounds constraints). If you need better help, you have to
send us a minimal, self-contained, reproducible example.

Ravi.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On
Behalf Of Carol Gao
Sent: Friday, May 07, 2010 5:53 AM
To: [hidden email]
Subject: [R] problem in using optim

Dear R users,

When I was trying to use the function *optim* to get the MLEs, quite a few
warning messages showed up as below:

Warning in log(psi * lam) : NaNs produced

I am just wondering what does this mean? Was it something wrong with my
likelihood function or was it sth wrong with the data?

Thanks in advance.

Carol Gao

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem in using optim

Carol Gao
Hi Ravi,

I have generated some random variable from generalised gamma distribution,
and now I need to use them to test on my optim function. Please see below:

*##Need to install package VGAM first before calling rggamma function
library(VGAM)
x <- rggamma(500,scale=gamma(1.5)/gamma(3.5),d=0.5,k=1.5)
mlogl <- function(param){
  n <- length(x)
  psi <- numeric(n)
  psi[1] <- 1.0
  a0 <- param[1]; a1 <-param[2]; b1 <- param[3]; alpha <- param[4]; kappa <-
param[5];

  for (i in 2:n) {
    psi[i] <- a0 + a1*x[i-1] + b1*psi[i-1]
    lam <- gamma(kappa)/gamma(kappa+(1/alpha))
    ll <-
n*log(alpha/gamma(kappa))+kappa*alpha*sum(log(x))-kappa*alpha*sum(log(psi*lam))-lam^(-alpha)*sum((x/psi)^alpha)
  }
  return(-ll)
}
fittedparam<-optim(c(0.1,0.05,0.5,0.65,1.8),mlogl,x)*

While I do need to put some constrains on my parameters,* the parameters
param[1]~param[5] all need to be positive*. Do you know how to have this
done?

Thank you!

Carol


On Fri, May 7, 2010 at 11:12 PM, Ravi Varadhan <[hidden email]> wrote:

> It probably means that your parameters are taking on values that are
> infeasible.  You may have to impose constraints on your parameters to get a
> solution (e.g. bounds constraints). If you need better help, you have to
> send us a minimal, self-contained, reproducible example.
>
> Ravi.
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On
> Behalf Of Carol Gao
> Sent: Friday, May 07, 2010 5:53 AM
> To: [hidden email]
> Subject: [R] problem in using optim
>
> Dear R users,
>
> When I was trying to use the function *optim* to get the MLEs, quite a few
> warning messages showed up as below:
>
> Warning in log(psi * lam) : NaNs produced
>
> I am just wondering what does this mean? Was it something wrong with my
> likelihood function or was it sth wrong with the data?
>
> Thanks in advance.
>
> Carol Gao
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: problem in using optim

Berend Hasselman
Carol Gao wrote
I have generated some random variable from generalised gamma distribution,
and now I need to use them to test on my optim function. Please see below:

*##Need to install package VGAM first before calling rggamma function
library(VGAM)
x <- rggamma(500,scale=gamma(1.5)/gamma(3.5),d=0.5,k=1.5)
mlogl <- function(param){
  n <- length(x)
  psi <- numeric(n)
  psi[1] <- 1.0
  a0 <- param[1]; a1 <-param[2]; b1 <- param[3]; alpha <- param[4]; kappa <-
param[5];

  for (i in 2:n) {
    psi[i] <- a0 + a1*x[i-1] + b1*psi[i-1]
    lam <- gamma(kappa)/gamma(kappa+(1/alpha))
    ll <-
n*log(alpha/gamma(kappa))+kappa*alpha*sum(log(x))-kappa*alpha*sum(log(psi*lam))-lam^(-alpha)*sum((x/psi)^alpha)
  }
  return(-ll)
}
fittedparam<-optim(c(0.1,0.05,0.5,0.65,1.8),mlogl,x)*
The for loop in your problem is wrongly structured.
You only need to compute lam  once, but you're doing that (n-1) times.
Same goes for the computation of ll.
Since psi was initialised to all zeros by the numeric(n), psi*lam will still contain zero's while i < n.

So move the calculation of lam and ll outside the for loop and you shouldn't get so many messages about NaN

  for (i in 2:n) { psi[i] <- a0 + a1*x[i-1] + b1*psi[i-1] }
 
  lam <- gamma(kappa)/gamma(kappa+(1/alpha))
  ll <- n*log(alpha/gamma(kappa))+kappa*alpha*sum(log(x))-kappa*alpha*sum(log(psi*lam))-lam^(-alpha)*sum((x/psi)^alpha)
 
/Berend
Reply | Threaded
Open this post in threaded view
|

Re: problem in using optim

Carol Gao
That was great! I tried as what you said, not as many warnings as before,
and it seems that it converges.

Thank you Berend. and thank you all!

Carol

On Sat, May 8, 2010 at 5:00 PM, Berend Hasselman <[hidden email]> wrote:

>
>
> Carol Gao wrote:
> >
> > I have generated some random variable from generalised gamma
> distribution,
> > and now I need to use them to test on my optim function. Please see
> below:
> >
> > *##Need to install package VGAM first before calling rggamma function
> > library(VGAM)
> > x <- rggamma(500,scale=gamma(1.5)/gamma(3.5),d=0.5,k=1.5)
> > mlogl <- function(param){
> >   n <- length(x)
> >   psi <- numeric(n)
> >   psi[1] <- 1.0
> >   a0 <- param[1]; a1 <-param[2]; b1 <- param[3]; alpha <- param[4]; kappa
> > <-
> > param[5];
> >
> >   for (i in 2:n) {
> >     psi[i] <- a0 + a1*x[i-1] + b1*psi[i-1]
> >     lam <- gamma(kappa)/gamma(kappa+(1/alpha))
> >     ll <-
> >
> n*log(alpha/gamma(kappa))+kappa*alpha*sum(log(x))-kappa*alpha*sum(log(psi*lam))-lam^(-alpha)*sum((x/psi)^alpha)
> >   }
> >   return(-ll)
> > }
> > fittedparam<-optim(c(0.1,0.05,0.5,0.65,1.8),mlogl,x)*
> >
>
> The for loop in your problem is wrongly structured.
> You only need to compute lam  once, but you're doing that (n-1) times.
> Same goes for the computation of ll.
> Since psi was initialised to all zeros by the numeric(n), psi*lam will
> still
> contain zero's while i < n.
>
> So move the calculation of lam and ll outside the for loop and you
> shouldn't
> get so many messages about NaN
>
>  for (i in 2:n) { psi[i] <- a0 + a1*x[i-1] + b1*psi[i-1] }
>
>  lam <- gamma(kappa)/gamma(kappa+(1/alpha))
>  ll <-
>
> n*log(alpha/gamma(kappa))+kappa*alpha*sum(log(x))-kappa*alpha*sum(log(psi*lam))-lam^(-alpha)*sum((x/psi)^alpha)
>
> /Berend
> --
> View this message in context:
> http://r.789695.n4.nabble.com/problem-in-using-optim-tp2133938p2135929.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.