I want to compute the integral of a function associated with each value in a vector. Using an example, I precisely describe my aim as follows.
Suppose x and y are independent random variables, and I is an indicator variables such that I=1 if y > 0.25x or zero otherwise. I want to make n draws of y from an exponential distribution, and for each draw I wish to compute the integral of g = xf(x)I over the distribution of x, f(x). I want this distribution to be exponential with parameter value of 1. I tried the following code in r.
f = function(n){
h = list()
g = numeric()
y = rexp(n)
for(i in 1:n){
h[i] = function(x){(x * dexp(x) * (y[i] > 0.5*x))}
g[i] = integrate(h[i], lower=-Inf, upper=Inf)
}
return(summary(g))
}
But then when I run f(3) I get an error message "*Error in h[i] = function(x) { : cannot coerce type 'closure' to vector of type 'list'"*. But when I run the following codes
y = rexp(1)
h = function(x){x * dexp(x) * (y > 0.5*x)}
integrate(h, lower=-Inf, upper=Inf)
f(3)
I get results. Does somebody has suggestion on how to improve this code so that h function is evaluated at each values of y with the output being a function, which then will be passed to the g function?
[[to index your list rather than[, ieh[[i]]