s-news
[Top] [All Lists]

Re: Recurrent sum

To: "Tristan Lorino" <tristan.lorino@lcpc.fr>
Subject: Re: Recurrent sum
From: "Dimitris Rizopoulos" <dimitris.rizopoulos@med.kuleuven.be>
Date: Tue, 13 Feb 2007 16:16:23 +0100
Cc: <s-news@lists.biostat.wustl.edu>
References: <1075318034.20070213160136@lcpc.fr>
you can try something like this:

data$out <- data$T0 * sapply(data$Age, function(i, r.) sum((1 + r)^(1:i)), r. = r)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
    http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- From: "Tristan Lorino" <tristan.lorino@lcpc.fr>
To: "S-News" <s-news@lists.biostat.wustl.edu>
Sent: Tuesday, February 13, 2007 4:01 PM
Subject: [S] Recurrent sum


Hi,

I have a problem to calculate the following thing: I have subjects
(IdS) at different ages (age) and with a nominal variable
(T0). I also have a rate r.
The aim si to calculate: $T0 \times \sum_{i=1}^{Age} (1+r)^i$

For instance:
r_2
data_data.frame(IdS=rep(1:3,rep(3,3)),T0=rep(c(100,150,120),each=3)
,Age=c(1,3,4,1,2,4,2,3,4))
data[,4]_ifelse(data$Age==1,data$T0*(1+r),
       ifelse(data$Age==2,data$T0*((1+r)+(1+r)^2),
               ifelse(data$Age==3,data$T0*((1+r)+(1+r)^2+(1+r)^3),
                       data$T0*((1+r)+(1+r)^2+(1+r)^3+(1+r)^4)
)))

For small values of "age", this code is reasonnable... My problem is
that the age can reach 15 or 20... and I have 20,000 subjects.

Is there a simpler way to calculate this quantity?

Thank you in advance,
Tristan Lorino



--
Laboratoire Central des Ponts et Chaussées
[Division ESAR ? Section AGR]
Route de Bouaye BP 4129
44341 Bouguenais Cedex
France
Tél 33 (0)2 40 84 56 18
Fax 33 (0)2 40 84 59 92

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


<Prev in Thread] Current Thread [Next in Thread>