The apply with the cumprod was exactly what I was after. The apply just
wasn't clicking with me, and I had overlooked the cumprod. Thanks to all
for pushing me down the right path!
Actually, what I am ultimately after is a way to link this series, without
having to use a for loop ( the only way I can think of ... ). But, would
like to see if it can be linked using mapply or apply against the rows
and to compute the linked results.
zz = matrix(rnorm(20), ncol=2)
zzcum = apply(zz/100 + 1, 2, cumprod)
zzlinkcum = 100*zzcum
for(i in 2:length(zz[,1])){ zzlinkcum[i,]=zzlinkcum[i-1,]*zzcum[i,]} ###
Is there a better way here ?
zz
[,1] [,2]
[1,] 0.8563323 -0.3895789
[2,] 0.8311070 -0.7483010
[3,] 0.3526344 -1.1702419
[4,] 1.1105516 0.5238831
[5,] 0.6471324 -0.1419063
[6,] -0.9821008 0.8378471
[7,] 1.6799099 1.7025973
[8,] -0.1904968 -2.9203921
[9,] 0.4218418 0.4799355
[10,] -0.5989616 -1.2121375
zzcum
[,1] [,2]
[1,] 1.007118 1.001036
[2,] 1.011022 1.011093
[3,] 1.007719 1.023287
[4,] 1.026238 1.030673
[5,] 1.031242 1.023557
[6,] 1.029199 1.021431
[7,] 1.044428 1.017244
[8,] 1.034568 1.026164
[9,] 1.032941 1.024926
[10,] 1.032490 1.026172
zzlinkcum
[,1] [,2]
[1,] 100.7118 100.1036
[2,] 101.8219 101.2140
[3,] 102.6078 103.5711
[4,] 105.3000 106.7479
[5,] 108.5898 109.2626
[6,] 111.7605 111.6042
[7,] 116.7259 113.5287
[8,] 120.7608 116.4990
[9,] 124.7388 119.4028
[10,] 128.7916 122.5278
Dirk Eddelbuettel <[EMAIL PROTECTED]>
08/31/2006 05:03 PM
To
[EMAIL PROTECTED]
cc
[email protected]
Subject
Re: [R] cumulative growth rates indexed to a common starting point over n
series of observations
On 31 August 2006 at 15:22, [EMAIL PROTECTED] 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
------------------------------------------------------------
CONFIDENTIALITY NOTICE: This electronic mail transmission (i...{{dropped}}
______________________________________________
[email protected] 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.