0

I'm trying to integrate the following function: t * ( ( 1 - F(t) )^( ( 1 - .75* b ) / (.75 * a ) ) ) * f(t) in a finite interval between the realization of the random variable X and its upperbound. F(x) is a CDF while f(x) is a pdf. Both F(x) and f(x) are nonparametrically defined (using the np package) as unconditional CDF and pdf respectively. I'm using integrate from the stats library to perform the numerical integration. I need to perform this for each value in the vector x.

# create the values for x
n <- 10
s <- 100
x <- rlnorm(c(n), meanlog = 5, sdlog = 1)
for(i in 2:s){
x <-c(x, rlnorm(c(n), meanlog = 5, sdlog = 1))
}
trim <- 0.01
a <- .2
b <- .3

# Compute pdf and CDF
bw_pdf <- npudensbw(dat = x, bwmethod = 'normal-reference', 
                   trim = trim)
bw_CDF <- npudistbw(dat = x, bwmethod = 'normal-reference', 
                   trim = trim)

f1 <- npudens(bws=bw_pdf)
F1 <- npudist(bws=bw_CDF)


X <- data.frame(x, F1$dist, f1$dens, row.names=NULL)
integ1 <- function(t){
i <- which.min((X$x - t)>=0)
cdf <- X$F1.dist[i]
pdf <- X$f1.dens[i]
vi <- X$x[i]
vector1 <- vi * ( ( 1 - cdf )^( ( 1 - .75* b ) / (.75 * a ) ) ) * pdf
return( vector1 )
}

y <- c()
for(i in 1:length( x ) ){
vv <-x[i]
Z <- integrate( integ1(vv),lower = vv, upper = max(x) )
y <- rbind(y,Z)
}

The error I get is:

Error in match.fun(f) : 'integ1(vv)' is not a function, character or symbol

Finally, I'm not sure whether the which formula is correct. I'm using it to find the value in the vector X which is just larger than the vv to perform the integration only in the interior from vv and the upperbound of X.

Thank you for your help.


Edit: using the quadgk command in the Pracma library as follows the integration works:

integ1 <- function(t){
   i <- which.min((X$x - t)>=0)
   cdf1 <- X$F1.dist[i]
   pdf1 <- X$f1.dens[i]
   vi1 <- X$x[i]
   vector1 <- t * ( ( 1 - cdf )^( ( 1 - .75* b ) / (.75 * a ) ) ) * pdf
   return( vector1 )
}

for(i in 1:length( x ) ){
  vv <-x[i]
  Z <- quadgk( integ1, vv, max(x) )
}

Still I'm not sure whether the which formula makes sense, which is my main issue.

Andrew
  • 678
  • 2
  • 9
  • 19

0 Answers0