Awhile back, in this thread, Perry helped me get started on using the nimIntegrate function. Recently I needed to do a double integral, and got that working too, with all constant parameters.
Now I would like to integrate over an exponential density with a time-varying rate parameter, but I keep getting stuck on several points. I want the density to be defined by an exponentially decreasing log hazard rate.
For example:
Let the log hazard rate be defined by an intercept and a coefficient controlling exponential decay, something like
b0 <- -5.5
b1 = 4
loghaz <- b0 + b1*exp(-(0:100)/10)
plot(0:100, loghaz)
lam <- exp(loghaz)
Then I want to calculate the density. I understand that the density function for time varying hazard is
f(x) = lam(x)exp(-LAM(x)),
where LAM(x) is the integral of lam(s) over s = 0 to x.
Lastly, I want to integrate this density function from x = 0 to a. I tried the following, but I don't think it is right, and it doesn't totally run either.
# inner function
InnerFunc <- nimbleFunction(
run = function(
x = double(1),
theta = double(1)){
f <- exp(theta[1] + theta[2]* exp(-x/10))
return(dexp(x,f))
returnType(double(1))
})
# outer integral
OuterFunc <- nimbleFunction(
run = function(
y = double(1),
theta = double(1)){
return(nimIntegrate(InnerFunc, 0, theta[3], param = theta)[1])
returnType(double(1))
})
a = 100
theta = c(-5.5, 4, a)
#this works
nimIntegrate(InnerFunc, 0, theta[3], param = theta)
# this doesn't and produces an error about result of wrong length
nimIntegrate(f = OuterFunc, lower = 0, upper = a, param = theta)
Error in integrate(f, lower, upper, param, subdivisions = subdivisions, :
evaluation of function gave a result of wrong length
The first integration works, but I don’t think it is correct because it is not a density with an integrated hazard.
I also tried the following, but it doesn’t quite work either. I am not quite sure how to define the integration limits for the time-varying hazard:
# time varying hazard function
InnerFunc1 <- nimbleFunction(
run = function(
x = double(1),
theta = double(1)){
f2 = exp(theta[1] + theta[2]* exp(-x/10))
return(f2)
returnType(double(1))
})
# integration of hazard function to specify distribution function
# (I'm not sure how to define the upper limit) – seems like it
# should be y, but that generates an error
InnerFunc2 <- nimbleFunction(
run = function(
y = double(1),
theta = double(1)){
lam = nimIntegrate(InnerFunc1, 0, theta[3], param = theta)[1]
return(dexp(x,lam))
returnType(double(1))
})
# outer integral
OuterFunc <- nimbleFunction(
run = function(
z = double(1),
theta = double(1)){
return(nimIntegrate(InnerFunc2, 0, theta[3], param = theta)[1])
returnType(double(1))
})
# this works, I think
nimIntegrate(InnerFunc1, 0, theta[3], param = theta)[1]
# this doesn't work either
nimIntegrate(f = OuterFunc, lower = 0, upper = age, param = theta)
Is it possible to do what I am trying? I could do unit cumulative hazard approximations, but execution is much slower than using integrals. Appreciate if anyone can set me straight.
--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/nimble-users/0092b556-934a-442a-83c9-c9dfc33a21efn%40googlegroups.com.