# [R] Error in integrate(integrand, 0, Inf) : non-finite function value

David Winsemius dwinsemius at comcast.net
Sat Oct 20 22:21:52 CEST 2012

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