Error in integrate(integrand, 0, Inf) : non-finite function value

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

Error in integrate(integrand, 0, Inf) : non-finite function value

stats12
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Error in integrate(integrand, 0, Inf) : non-finite function value

David Winsemius

On Oct 20, 2012, at 9:23 AM, stats12 wrote:

> Dear R users,
>
> When I run the code below, I get the error "Error in integrate(integrand, 0,
> Inf) : non-finite function value".  The code works if the function returns
> only "sum(integ)".

But you never showed us the working code.

> However, I want to add "cmh" to it. When I add "cmh" I
> get that error. I can't figure out why this is happening because my
> integrate function has nothing to do with "cmh".  I tried to integrate from
> 0 to 1000, and still same error. Any suggestion is greatly appreciated.
> Thank you in advance!
>
>
>
> d<-matrix(c(1,1,0,0,0,0,0,0,2,1,0,0,1,1,0,1,2,2,1,0),nrow=10,ncol=2)
> h<-matrix(runif(20,0,1),10)
> delta<-matrix(c(2,1,0,1,0,1,0,0,2,1,0,0,1,1,1,1,0,2,1,0),nrow=10,ncol=2)
>
> out<-vector("numeric",length(1:2))
> integ<-vector("numeric",length(1:2))
>
> for (k in 1:2){
> ll<-function(p){
>      cmh<-delta[,k]*(h[,k]*log(0.5))+p*log(gamma(1+1/p))

This inner loop appears to define a function 10 times, but does nothing else:

>      for(s in 1:10){
>      integrand<-function(x)
>      x^d[s,k]*exp(-x*gamma(1+1/p))^p*p*x^(p-1)*exp(-x*h[s,k])
>                     }  # end of s-loop

> ------------------
>      integ<-integrate(integrand,0,Inf)$value
>      cmhn<-as.vector(cmh)
>      lik<-sum(cmhn+integ)
>      -lik
>               }  # end of ll()-function
> initial<-c(1)
> t<-nlm(ll,initial)
> out[k]<-t$estimate
> } # end of k-loop
> est<-as.vector(out)


Your uncommented code seems to have problems with organization, but since you never really described your overall strategy or goal was, it's hard to know.

--


David Winsemius, MD
Alameda, CA, USA

______________________________________________
[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: Error in integrate(integrand, 0, Inf) : non-finite function value

stats12
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Error in integrate(integrand, 0, Inf) : non-finite function value

Jeff Newmiller
That is an intrinsic part of nonlinear optimization. Choose your starting point wisely.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

stats12 <[hidden email]> wrote:

>Hi,
>
>Thank you for your comment. I worked on the code again and was able to
>make
>it work. The only problem I am having right now is that nlm depends on
>the
>initial value.  
>
>When the initial value is 1, I get the following estimates
>0.1230414 19.6271029
>
>when it is 2, I get the following
>29.46874 20.01679
>
>
>
>d<-matrix(c(1,1,0,0,0,0,0,0,2,1,0,0,1,1,0,1,2,2,1,0),nrow=10,ncol=2)
>h<-matrix(runif(20,0,1),10)
>delta<-matrix(c(2,1,0,1,0,1,0,0,2,1,0,0,1,1,1,1,0,2,1,0),nrow=10,ncol=2)
>
>
>out<-vector("numeric",length(1:2))
>integ<-vector("numeric",length(1:10))
>
>for (k in 1:2){
>ll<-function(p){
>cmh<-delta[,k]*(h[,k]*log(0.5))+p*log(gamma(1+1/p))
>for(s in 1:10){
>integrand<-function(x)
>x^d[s,k]*exp(-x*gamma(1+1/p))^p*p*x^(p-1)*exp(-x*h[s,k])
>integ<-integrate(integrand,0,Inf)$value
>return(integ)
>}
>
>lik<-sum(cmh+log(integ))
>-lik
>}
>initial<-c(1)
>t<-nlm(ll,initial)
>out[k]<-t$estimate
>}
>
>est<-as.vector(out)
>
>
>
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Error-in-integrate-integrand-0-Inf-non-finite-function-value-tp4646868p4646896.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.

______________________________________________
[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: Error in integrate(integrand, 0, Inf) : non-finite function value

David Winsemius
In reply to this post by stats12

On Oct 20, 2012, at 8:01 PM, stats12 wrote:

> Hi,
>
> Thank you for your comment. I worked on the code again and was able to make
> it work.

Does that mean you know what value is "correct" for certain cases? Is there an overall strategy that is guiding this effort? I wrote earlier:

> DW> Your uncommented code seems to have problems with organization, but since you never really described
> DW> your overall strategy or [what the] goal was, it's hard to know.

I strongly suspect that is still the case. You are mixing applicative looping strategies with functional methods and it "looks wrong" to my eyes, but I have nothing to compare it to as far as goals.


> The only problem I am having right now is that nlm depends on the
> initial value.  

> When the initial value is 1, I get the following estimates
> 0.1230414 19.6271029
>
> when it is 2, I get the following
> 29.46874 20.01679
>
>
>
> d<-matrix(c(1,1,0,0,0,0,0,0,2,1,0,0,1,1,0,1,2,2,1,0),nrow=10,ncol=2)
> h<-matrix(runif(20,0,1),10)
> delta<-matrix(c(2,1,0,1,0,1,0,0,2,1,0,0,1,1,1,1,0,2,1,0),nrow=10,ncol=2)
>
> out<-vector("numeric",length(1:2))
> integ<-vector("numeric",length(1:10))
>
> for (k in 1:2){
> ll<-function(p){
> cmh<-delta[,k]*(h[,k]*log(0.5))+p*log(gamma(1+1/p))
> for(s in 1:10){
> integrand<-function(x)
> x^d[s,k]*exp(-x*gamma(1+1/p))^p*p*x^(p-1)*exp(-x*h[s,k])
> integ<-integrate(integrand,0,Inf)$value
> return(integ)
> }
>
> lik<-sum(cmh+log(integ))
> -lik
> }
> initial<-c(1)
> t<-nlm(ll,initial)
> out[k]<-t$estimate
> }
>
> est<-as.vector(out)

---

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[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: Error in integrate(integrand, 0, Inf) : non-finite function value

Berend Hasselman
In reply to this post by stats12

On 21-10-2012, at 05:01, stats12 wrote:

> Hi,
>
> Thank you for your comment. I worked on the code again and was able to make
> it work. The only problem I am having right now is that nlm depends on the
> initial value.  
>
> When the initial value is 1, I get the following estimates
> 0.1230414 19.6271029
>
> when it is 2, I get the following
> 29.46874 20.01679
>
>
>
> d<-matrix(c(1,1,0,0,0,0,0,0,2,1,0,0,1,1,0,1,2,2,1,0),nrow=10,ncol=2)
> h<-matrix(runif(20,0,1),10)
> delta<-matrix(c(2,1,0,1,0,1,0,0,2,1,0,0,1,1,1,1,0,2,1,0),nrow=10,ncol=2)
>
> out<-vector("numeric",length(1:2))
> integ<-vector("numeric",length(1:10))
>
> for (k in 1:2){
> ll<-function(p){
> cmh<-delta[,k]*(h[,k]*log(0.5))+p*log(gamma(1+1/p))
> for(s in 1:10){
> integrand<-function(x)
> x^d[s,k]*exp(-x*gamma(1+1/p))^p*p*x^(p-1)*exp(-x*h[s,k])
> integ<-integrate(integrand,0,Inf)$value
> return(integ)
> }
>
> lik<-sum(cmh+log(integ))
> -lik
> }
> initial<-c(1)
> t<-nlm(ll,initial)
> out[k]<-t$estimate
> }
>
> est<-as.vector(out)

When I run this code (in TextMate 1.5; Mac OS X 10.6.8)  I get:

Error in integrate(integrand, 0, Inf) : non-finite function value
RMate stopped at line 0
Calls: nlm -> <Anonymous> -> f -> integrate
In addition: Warning messages:
1: In log(gamma(1 + 1/p)) : NaNs produced
2: In log(gamma(1 + 1/p)) : NaNs produced
Execution halted

In addition: why return(integ) in the inner loop with s? This implies an immediate return in function ll.

You initialize integ as a vector. But in the inner s loop you assign a scalar to integ.
The whole thing looks very muddled.

Your code should be indented for clarity.

Berend

______________________________________________
[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.