invalid function value in 'nlm' optimizer

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

invalid function value in 'nlm' optimizer

Hanze Zhang
Hi, R users,


I am using nlm function to get the MLE of parameter alpha and lambda from a
 parametric survival model (Weibull distribution). However, this message
always came out: ' invalid function value in 'nlm' optimizer'. Could anyone
help me? Code is

project<-read.table(file="C://data.txt", header=T, as.is=T)
names(project)
attach(project)

x<-time
delta<-ind


# -log likelihood
#alpha<-theta[1]
#lambda<-theta[2]
ln<-function(theta)
  {

 -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
}

#MLE
nlm(ln,theta<-c(1,1),hessian=TRUE)


Thanks!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

Bert Gunter
Why are you doing this instead of using the survival package?

Bert

On Tuesday, April 28, 2015, Hanze Zhang <[hidden email]> wrote:

> Hi, R users,
>
>
> I am using nlm function to get the MLE of parameter alpha and lambda from a
>  parametric survival model (Weibull distribution). However, this message
> always came out: ' invalid function value in 'nlm' optimizer'. Could anyone
> help me? Code is
>
> project<-read.table(file="C://data.txt", header=T, as.is=T)
> names(project)
> attach(project)
>
> x<-time
> delta<-ind
>
>
> # -log likelihood
> #alpha<-theta[1]
> #lambda<-theta[2]
> ln<-function(theta)
>   {
>
>
>  -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
> }
>
> #MLE
> nlm(ln,theta<-c(1,1),hessian=TRUE)
>
>
> Thanks!
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] <javascript:;> mailing list -- To UNSUBSCRIBE and
> more, see
> 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.
>


--

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
Clifford Stoll

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

Duncan Murdoch-2
In reply to this post by Hanze Zhang
On 28/04/2015 2:43 AM, Hanze Zhang wrote:

> Hi, R users,
>
>
> I am using nlm function to get the MLE of parameter alpha and lambda from a
>  parametric survival model (Weibull distribution). However, this message
> always came out: ' invalid function value in 'nlm' optimizer'. Could anyone
> help me? Code is
>
> project<-read.table(file="C://data.txt", header=T, as.is=T)
> names(project)
> attach(project)
>
> x<-time
> delta<-ind
>
>
> # -log likelihood
> #alpha<-theta[1]
> #lambda<-theta[2]
> ln<-function(theta)
>   {
>
>  -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
> }
>
> #MLE
> nlm(ln,theta<-c(1,1),hessian=TRUE)

You are taking logs of parameters.  Probably the optimizer is setting
the parameters to negative values, and so the log returns NaN.

You can avoid this by testing your parameters on input, and always
returning a valid number.  There are lots of ways to do this: One
strategy is to return +Inf for invalid values; another is to move the
parameter to the nearest boundary, and apply a penalty according to how
far you moved it.  Or just take the absolute value of the parameter.  Or
reparametrize so that illegal values aren't possible.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

Hanze Zhang
I still cannot solve the problem:  'invalid function value in 'nlm'
optimizer'

I want to get the MLE for theta[1] and theta[2], my code is below:


x <- c(2,5,3,7,3,2,4)
delta <- c(1, 0, 1, 1, 1, 0, 1)

# -log likelihood
#alpha<-theta[1]
#lamda<-theta[2]
ln<-function(theta,x1,x2  )
  {

 -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
}


#MLE
nlm(ln,theta<-c(1,1),x1=x, x2=delta, hessian=TRUE)


On Tue, Apr 28, 2015 at 6:40 AM, Duncan Murdoch <[hidden email]>
wrote:

> On 28/04/2015 2:43 AM, Hanze Zhang wrote:
> > Hi, R users,
> >
> >
> > I am using nlm function to get the MLE of parameter alpha and lambda
> from a
> >  parametric survival model (Weibull distribution). However, this message
> > always came out: ' invalid function value in 'nlm' optimizer'. Could
> anyone
> > help me? Code is
> >
> > project<-read.table(file="C://data.txt", header=T, as.is=T)
> > names(project)
> > attach(project)
> >
> > x<-time
> > delta<-ind
> >
> >
> > # -log likelihood
> > #alpha<-theta[1]
> > #lambda<-theta[2]
> > ln<-function(theta)
> >   {
> >
> >
> -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
> > }
> >
> > #MLE
> > nlm(ln,theta<-c(1,1),hessian=TRUE)
>
> You are taking logs of parameters.  Probably the optimizer is setting
> the parameters to negative values, and so the log returns NaN.
>
> You can avoid this by testing your parameters on input, and always
> returning a valid number.  There are lots of ways to do this: One
> strategy is to return +Inf for invalid values; another is to move the
> parameter to the nearest boundary, and apply a penalty according to how
> far you moved it.  Or just take the absolute value of the parameter.  Or
> reparametrize so that illegal values aren't possible.
>
> Duncan Murdoch
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

William Dunlap
Your function ln() does not return a scalar.
   > ln(theta=c(1,2))
   [1] 48.5342640972 48.5342640972 48.5342640972 48.5342640972 48.5342640972


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Apr 28, 2015 at 6:40 PM, Hanze Zhang <[hidden email]> wrote:

> I still cannot solve the problem:  'invalid function value in 'nlm'
> optimizer'
>
> I want to get the MLE for theta[1] and theta[2], my code is below:
>
>
> x <- c(2,5,3,7,3,2,4)
> delta <- c(1, 0, 1, 1, 1, 0, 1)
>
> # -log likelihood
> #alpha<-theta[1]
> #lamda<-theta[2]
> ln<-function(theta,x1,x2  )
>   {
>
>
>  -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
> }
>
>
> #MLE
> nlm(ln,theta<-c(1,1),x1=x, x2=delta, hessian=TRUE)
>
>
> On Tue, Apr 28, 2015 at 6:40 AM, Duncan Murdoch <[hidden email]>
> wrote:
>
> > On 28/04/2015 2:43 AM, Hanze Zhang wrote:
> > > Hi, R users,
> > >
> > >
> > > I am using nlm function to get the MLE of parameter alpha and lambda
> > from a
> > >  parametric survival model (Weibull distribution). However, this
> message
> > > always came out: ' invalid function value in 'nlm' optimizer'. Could
> > anyone
> > > help me? Code is
> > >
> > > project<-read.table(file="C://data.txt", header=T, as.is=T)
> > > names(project)
> > > attach(project)
> > >
> > > x<-time
> > > delta<-ind
> > >
> > >
> > > # -log likelihood
> > > #alpha<-theta[1]
> > > #lambda<-theta[2]
> > > ln<-function(theta)
> > >   {
> > >
> > >
> >
> -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
> > > }
> > >
> > > #MLE
> > > nlm(ln,theta<-c(1,1),hessian=TRUE)
> >
> > You are taking logs of parameters.  Probably the optimizer is setting
> > the parameters to negative values, and so the log returns NaN.
> >
> > You can avoid this by testing your parameters on input, and always
> > returning a valid number.  There are lots of ways to do this: One
> > strategy is to return +Inf for invalid values; another is to move the
> > parameter to the nearest boundary, and apply a penalty according to how
> > far you moved it.  Or just take the absolute value of the parameter.  Or
> > reparametrize so that illegal values aren't possible.
> >
> > Duncan Murdoch
> >
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

Hanze Zhang
How should I do? The issue happened on log likelihood function?

On Tue, Apr 28, 2015 at 11:06 PM, William Dunlap <[hidden email]> wrote:

> Your function ln() does not return a scalar.
>    > ln(theta=c(1,2))
>    [1] 48.5342640972 48.5342640972 48.5342640972 48.5342640972 48.
> 5342640972
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Tue, Apr 28, 2015 at 6:40 PM, Hanze Zhang <[hidden email]>
> wrote:
>
>> I still cannot solve the problem:  'invalid function value in 'nlm'
>> optimizer'
>>
>> I want to get the MLE for theta[1] and theta[2], my code is below:
>>
>>
>> x <- c(2,5,3,7,3,2,4)
>> delta <- c(1, 0, 1, 1, 1, 0, 1)
>>
>> # -log likelihood
>> #alpha<-theta[1]
>> #lamda<-theta[2]
>> ln<-function(theta,x1,x2  )
>>   {
>>
>>
>>  -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
>> }
>>
>>
>> #MLE
>> nlm(ln,theta<-c(1,1),x1=x, x2=delta, hessian=TRUE)
>>
>>
>> On Tue, Apr 28, 2015 at 6:40 AM, Duncan Murdoch <[hidden email]
>> >
>> wrote:
>>
>> > On 28/04/2015 2:43 AM, Hanze Zhang wrote:
>> > > Hi, R users,
>> > >
>> > >
>> > > I am using nlm function to get the MLE of parameter alpha and lambda
>> > from a
>> > >  parametric survival model (Weibull distribution). However, this
>> message
>> > > always came out: ' invalid function value in 'nlm' optimizer'. Could
>> > anyone
>> > > help me? Code is
>> > >
>> > > project<-read.table(file="C://data.txt", header=T, as.is=T)
>> > > names(project)
>> > > attach(project)
>> > >
>> > > x<-time
>> > > delta<-ind
>> > >
>> > >
>> > > # -log likelihood
>> > > #alpha<-theta[1]
>> > > #lambda<-theta[2]
>> > > ln<-function(theta)
>> > >   {
>> > >
>> > >
>> >
>> -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
>> > > }
>> > >
>> > > #MLE
>> > > nlm(ln,theta<-c(1,1),hessian=TRUE)
>> >
>> > You are taking logs of parameters.  Probably the optimizer is setting
>> > the parameters to negative values, and so the log returns NaN.
>> >
>> > You can avoid this by testing your parameters on input, and always
>> > returning a valid number.  There are lots of ways to do this: One
>> > strategy is to return +Inf for invalid values; another is to move the
>> > parameter to the nearest boundary, and apply a penalty according to how
>> > far you moved it.  Or just take the absolute value of the parameter.  Or
>> > reparametrize so that illegal values aren't possible.
>> >
>> > Duncan Murdoch
>> >
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
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: invalid function value in 'nlm' optimizer

Duncan Murdoch-2
On 29/04/2015 9:49 AM, Hanze Zhang wrote:
> How should I do? The issue happened on log likelihood function?

I imagine it's the log(x[delta==1]) term that is turning the result into
a vector.

Duncan Murdoch

>
> On Tue, Apr 28, 2015 at 11:06 PM, William Dunlap <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Your function ln() does not return a scalar.
>        > ln(theta=c(1,2))
>        [1] 48.5342640972 48.5342640972 48.5342640972 48.5342640972
>     48.5342640972 <tel:5342640972>
>
>
>     Bill Dunlap
>     TIBCO Software
>     wdunlap tibco.com <http://tibco.com>
>
>     On Tue, Apr 28, 2015 at 6:40 PM, Hanze Zhang
>     <[hidden email] <mailto:[hidden email]>> wrote:
>
>         I still cannot solve the problem: 'invalid function value in 'nlm'
>         optimizer'
>
>         I want to get the MLE for theta[1] and theta[2], my code is below:
>
>
>         x <- c(2,5,3,7,3,2,4)
>         delta <- c(1, 0, 1, 1, 1, 0, 1)
>
>         # -log likelihood
>         #alpha<-theta[1]
>         #lamda<-theta[2]
>         ln<-function(theta,x1,x2  )
>           {
>
>          -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
>         }
>
>
>         #MLE
>         nlm(ln,theta<-c(1,1),x1=x, x2=delta, hessian=TRUE)
>
>
>         On Tue, Apr 28, 2015 at 6:40 AM, Duncan Murdoch
>         <[hidden email] <mailto:[hidden email]>>
>         wrote:
>
>         > On 28/04/2015 2:43 AM, Hanze Zhang wrote:
>         > > Hi, R users,
>         > >
>         > >
>         > > I am using nlm function to get the MLE of parameter alpha
>         and lambda
>         > from a
>         > >  parametric survival model (Weibull distribution).
>         However, this message
>         > > always came out: ' invalid function value in 'nlm'
>         optimizer'. Could
>         > anyone
>         > > help me? Code is
>         > >
>         > > project<-read.table(file="C://data.txt", header=T, as.is
>         <http://as.is>=T)
>         > > names(project)
>         > > attach(project)
>         > >
>         > > x<-time
>         > > delta<-ind
>         > >
>         > >
>         > > # -log likelihood
>         > > #alpha<-theta[1]
>         > > #lambda<-theta[2]
>         > > ln<-function(theta)
>         > >   {
>         > >
>         > >
>         >
>         -sum(delta)*log(theta[1]*theta[2])-sum(delta)*(theta[1]-1)*log(x[delta==1])+theta[2]*sum(x^theta[1])
>         > > }
>         > >
>         > > #MLE
>         > > nlm(ln,theta<-c(1,1),hessian=TRUE)
>         >
>         > You are taking logs of parameters.  Probably the optimizer
>         is setting
>         > the parameters to negative values, and so the log returns NaN.
>         >
>         > You can avoid this by testing your parameters on input, and
>         always
>         > returning a valid number.  There are lots of ways to do
>         this: One
>         > strategy is to return +Inf for invalid values; another is to
>         move the
>         > parameter to the nearest boundary, and apply a penalty
>         according to how
>         > far you moved it.  Or just take the absolute value of the
>         parameter.  Or
>         > reparametrize so that illegal values aren't possible.
>         >
>         > Duncan Murdoch
>         >
>         >
>
>                 [[alternative HTML version deleted]]
>
>         ______________________________________________
>         [hidden email] <mailto:[hidden email]> mailing
>         list -- To UNSUBSCRIBE and more, see
>         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 -- To UNSUBSCRIBE and more, see
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.