# looping a procedure that utilizes an integration of a function

#### @nthRo

##### New Member
Hi, all.

I've written a function that returns the survival function for a Gompertz mortality model. I've specified the two model parameters. With a calculation that includes a simple integration, I can calculate the life expectancy at any age. Is there a way I can use a loop with the integration that will quickly return life expectancy over a range of ages, say 0 to 80, so that I don't have to manually type in the age in which I'm interested? You can copy and paste the code below to see an example. Thanks so much.

--Trey

Code:
hk.bothsex_Gompsurv <- function (t)
{
x=c(0.02342671, 0.05837508)
a3<-x[1]
b3<-x[2]
shift<-15

S.t<-exp(a3/b3*(1-exp(b3*(t-shift))))
return<-S.t
}
integrate(hk.bothsex_Gompsurv,0,Inf)$value/hk.bothsex_Gompsurv(0) # life expectancy at birth (change lower limit of integral and corresponding "t" in denominator to calculate life expectancy at any age #### chippy ##### New Member If i understand you correctly you would like to include a for-structure. Perhaps something like : Code:  A <- array(0,81) for(i in 0:80){ A[i + 1] = integrate(hk.bothsex_Gompsurv,i,Inf)$value/hk.bothsex_Gompsurv(i)
}
On the other hand you could try to pass array arguments to your function - but it appears that the integration function does not find this train of thought appealing.

An advanced method would be to create workers and to pass the command to multiple processors. However, I see that the procedure runs fairly quickly and should not give problems - so you can just put this one in a normal loop.

#### @nthRo

##### New Member
Thanks so much, chippy. Straight answer w/o the condescending tone that sometimes accompanies the official r-help mailing list. Much appreciated.