Tristan,
A geometric series a + as + as^2 + ... + as^(n-1)
can be written as
(a-s.k)/(1-s) where k = a.s^(n-1)
For your specific problem, try:
k <- data[,"T0"]*(1+r)^data[,"Age"]
data[,4] <- -(data[,"T0"]-(1+r)*k)/r-data[,"T0"]
hope this helps,
Patrick Agin
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Tristan Lorino
Sent: Tuesday, February 13, 2007 10:02 AM
To: S-News
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
|