Search code examples
rnumerical-integration

Integral in R shows non-finite result


I have a vector "t" am trying to integrate a function with respect to t taking lower limit as 0 and upper limit as each member of the "t" vector. That is, I am trying to find the integral:

Where:

and:

Now I have the "t" vector as-

t<- seq(0,10,length.out=2001) [-1]

But whenever I try to find the integral using

den<- function(t,m,beta,mu0,mu1,gamma,delta,sigma){
mut<- function(t){mu0+mu1t*exp(-gamma*(14+log(t,base=exp(1)))^delta)}
 g<- function(x){dexp(x,beta,log=FALSE)*pnorm(x,mut(t),sigma)}
return(g(m))
 }

Lambda_den<- integrate(f = function(t) sapply(t, function(t) {
 den(t,m=m,beta=beta,mu0=mu0,mu1=mu1,gamma=gamma,delta=delta,sigma=sigma) # m is constant 
}) , lower = 0, upper = t)$value

Then for values of "t" vector below 0.9 shows the following error:

  Error in integrate(f = function(t) sapply(t, function(t) { : 
  non-finite function value

But for values above 0.9, it gives result. How can I avoid this error?


Solution

  • den<- function(t,m=3,beta=1.8,mu0=2,mu1=0.2,gamma=0.5,delta=1.6,sigma=0.1){
      mut<- function(t){mu0+mu1*t*exp(-gamma*(14+log(t,base=exp(1)))^delta)}
      g<- function(x){dexp(x,beta,log=FALSE)*pnorm(x,mut(t),sigma)}
      return(g(m))
    }
    
    s<- seq(0,10,length.out=2001)[-1]#t is a built in base R function. please refrain from using it as a variable
    
    lapply(s,integrate,f=den,lower=0)