Re: [R] cumulative growth rates indexed to a common starting point over n series of observations

From: <toby_marks_at_americancentury.com>
Date: Fri 01 Sep 2006 - 20:42:24 GMT


That is choice ! I'm starting to love this language. Thank-you !

Dirk Eddelbuettel <edd@debian.org>
09/01/2006 02:59 PM

To
toby_marks@americancentury.com
cc
r-help@stat.math.ethz.ch
Subject
Re: [R] cumulative growth rates indexed to a common starting point over n series of observations

On 1 September 2006 at 14:30, toby_marks@americancentury.com wrote:
| 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 ?

Sure, why not call apply again?

> set.seed(42); zz <- matrix(rnorm(20), ncol=2)
> zzcum <- apply(1+zz/100, 2, cumprod)
> apply(rbind(c(1,1), zzcum), 2, cumprod)*100
          [,1]     [,2]
 [1,] 100.0000 100.0000
 [2,] 101.3710 101.3049
 [3,] 102.1804 104.9735
 [4,] 103.3704 107.2642
 [5,] 105.2360 109.2994
 [6,] 107.5684 111.2246
 [7,] 109.8358 113.9036
 [8,] 113.8461 116.3156
 [9,] 117.8912 115.6233

[10,] 124.5442 112.1301
[11,] 131.4900 110.1781
>

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}}

______________________________________________
R-help@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.
Received on Sat Sep 02 06:46:01 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 07 Sep 2006 - 07:51:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.