toby_marks at americancentury.com
2006-Aug-31 20:22 UTC
[R] cumulative growth rates indexed to a common starting point over n series of observations
What is the R way of computing cumulative growth rates given a series of
discrete values indexed .
For instance, given a matrix of 20 observations for each of 5 series (zz),
what is the most straight forward technique in R for computing cumulative
growth (zzcum) ?
It seems for the solution I'm after might be imbedding the following cum
growth rate calc as a function into a function call to apply, mapply, ...?
zz = rnorm(100)
dim(zz) = c(20,5)
zz
zzcum=matrix(nrow=20,ncol=5)
zzcum
zzcum[1,]=100*(1+zz[1,]/100)
zzcum
for(i in 2:20){zzcum[i,] = zzcum[i-1,]*(1+zz[i,]/100)}
zzcum
------------------------------------------------------------
CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}}
MARK LEEDS
2006-Aug-31 21:27 UTC
[R] cumulative growth rates indexed to a common starting point over n series of observations
i think you can add 1 to whatever series you are considering and then use cumprod() but check that that works. also do ?cumprod ----- Original Message ----- From: <toby_marks at americancentury.com> To: <r-help at stat.math.ethz.ch> Sent: Thursday, August 31, 2006 4:22 PM Subject: [R] cumulative growth rates indexed to a common starting point over n series of observations> What is the R way of computing cumulative growth rates given a series of > discrete values indexed . > > For instance, given a matrix of 20 observations for each of 5 series (zz), > what is the most straight forward technique in R for computing cumulative > growth (zzcum) ? > It seems for the solution I'm after might be imbedding the following cum > growth rate calc as a function into a function call to apply, mapply, ...? > > > > zz = rnorm(100) > dim(zz) = c(20,5) > zz > zzcum=matrix(nrow=20,ncol=5) > zzcum > zzcum[1,]=100*(1+zz[1,]/100) > zzcum > for(i in 2:20){zzcum[i,] = zzcum[i-1,]*(1+zz[i,]/100)} > zzcum > > > > > > ------------------------------------------------------------ > CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Dirk Eddelbuettel
2006-Aug-31 22:03 UTC
[R] cumulative growth rates indexed to a common starting point over n series of observations
On 31 August 2006 at 15:22, toby_marks at americancentury.com wrote:
| What is the R way of computing cumulative growth rates given a series of
| discrete values indexed .
|
| For instance, given a matrix of 20 observations for each of 5 series (zz),
| what is the most straight forward technique in R for computing cumulative
| growth (zzcum) ?
| It seems for the solution I'm after might be imbedding the following cum
| growth rate calc as a function into a function call to apply, mapply, ...?
|
|
|
| zz = rnorm(100)
| dim(zz) = c(20,5)
| zz
| zzcum=matrix(nrow=20,ncol=5)
| zzcum
| zzcum[1,]=100*(1+zz[1,]/100)
| zzcum
| for(i in 2:20){zzcum[i,] = zzcum[i-1,]*(1+zz[i,]/100)}
| zzcum
How about cumprod() inside apply() ?
zzcum <- matrix(rnorm(100), ncol=5)
apply(zzcum/100 + 1, 2, cumprod)
Hth, Dirk
--
Hell, there are no rules here - we're trying to accomplish something.
-- Thomas A. Edison