[R] using a loop with an integration

David Winsemius dwinsemius at comcast.net
Sun Apr 22 22:14:01 CEST 2012


On Apr 22, 2012, at 3:41 PM, piltdownpunk wrote:

> Hi, all.
>
> I've written a function that returns the survival function for a  
> Gompertz
> mortality model.  I've specified the two model parameters.  Using 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?  Please see the code
> below.

?Vectorize

(Essentially a wrapper to mapply. No data example so no tested code.)

-- 
David.
>
> --Trey
>
> 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
>
> -----
> Trey Batey---Anthropology Instructor
> Division of Social Sciences
> Mt. Hood Community College
> Gresham, OR  97030
> Alt. Email:  trey.batey[at]mhcc[dot]edu
> --
> View this message in context: http://r.789695.n4.nabble.com/using-a-loop-with-an-integration-tp4578752p4578752.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list