[R-SIG-Finance] Applying function on rolling window basis ** not rollapply() function in zoo

Bogaso Christofer bogaso.christofer at gmail.com
Tue Jan 4 14:52:37 CET 2011


I was trying the example given in apply.rolling() function, however it is
not that what I wanted. Let me simulate it here:

> head(get(data(managers)), 20)
              HAM1    HAM2    HAM3    HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR US
10Y TR US 3m TR
1996-01-31  0.0074      NA  0.0349  0.0222   NA   NA          NA   0.0340
0.00380  0.00456
1996-02-29  0.0193      NA  0.0351  0.0195   NA   NA          NA   0.0093
-0.03532  0.00398
1996-03-31  0.0155      NA  0.0258 -0.0098   NA   NA          NA   0.0096
-0.01057  0.00371
1996-04-30 -0.0091      NA  0.0449  0.0236   NA   NA          NA   0.0147
-0.01739  0.00428
1996-05-31  0.0076      NA  0.0353  0.0028   NA   NA          NA   0.0258
-0.00543  0.00443
1996-06-30 -0.0039      NA -0.0303 -0.0019   NA   NA          NA   0.0038
0.01507  0.00412
1996-07-31 -0.0231      NA -0.0337 -0.0446   NA   NA          NA  -0.0442
-0.00100  0.00454
1996-08-31  0.0395 -0.0001  0.0461  0.0351   NA   NA          NA   0.0211
-0.00448  0.00451
1996-09-30  0.0147  0.1002  0.0653  0.0757   NA   NA          NA   0.0563
0.02229  0.00470
1996-10-31  0.0288  0.0338  0.0395 -0.0180   NA   NA          NA   0.0276
0.02869  0.00428
1996-11-30  0.0156  0.0737  0.0666  0.0458   NA   NA          NA   0.0756
0.02797  0.00427
1996-12-31  0.0176  0.0298  0.0214  0.0439   NA   NA          NA  -0.0198
-0.02094  0.00442
1997-01-31  0.0212  0.0794  0.0771  0.0437   NA   NA      0.0281   0.0625
-0.00055  0.00457
1997-02-28  0.0022 -0.0082 -0.0374  0.0312   NA   NA     -0.0006   0.0078
-0.00167  0.00390
1997-03-31  0.0094 -0.0269 -0.0336  0.0113   NA   NA     -0.0084  -0.0411
-0.01958  0.00422
1997-04-30  0.0126 -0.0061  0.0286  0.0354   NA   NA      0.0084   0.0597
0.01954  0.00477
1997-05-31  0.0438  0.0539  0.0759  0.0789   NA   NA      0.0394   0.0609
0.01033  0.00513
1997-06-30  0.0231  0.0552  0.0054  0.0412   NA   NA      0.0223   0.0448
0.01665  0.00365
1997-07-31  0.0154  0.1150  0.1081  0.0794   NA   NA      0.0454   0.0796
0.04161  0.00450
1997-08-31  0.0237 -0.0197 -0.0028  0.0143   NA   NA      0.0107  -0.0560
-0.02148  0.00428

> head(apply.rolling(managers[,1,drop=FALSE], FUN="mean", width=3), 10)
                  calcs
1996-01-31           NA
1996-02-29           NA
1996-03-31  0.014066667
1996-04-30  0.008566667
1996-05-31  0.004666667
1996-06-30 -0.001800000
1996-07-31 -0.006466667
1996-08-31  0.004166667
1996-09-30  0.010366667
1996-10-31  0.027666667

What I wanted to have is (0.0074+0.0193+0.0155)/3,
(-0.0091+0.0076+-0.0039)/3 (with window size 3) etc... Do I need to put any
further argument in that function?

Thanks,

-----Original Message-----
From: r-sig-finance-bounces at r-project.org
[mailto:r-sig-finance-bounces at r-project.org] On Behalf Of Brian G. Peterson
Sent: 04 January 2011 18:37
To: r-sig-finance at r-project.org
Subject: Re: [R-SIG-Finance] Applying function on rolling window basis **
not rollapply() function in zoo

On 01/04/2011 07:20 AM, Bogaso Christofer wrote:
> Hi all, in the zoo package there is a function rollapply() to apply 
> some fixed function on rolling window basis. I am looking for some 
> same sort of function on **temporal sampling basis** means, I would 
> like to apply a function say mean() for data window of length 30 like 
> observations for 1:30, 31:60, . so on.


?period.apply  # for time series based apply

?apply.rolling # to apply with a fixed (rolling) width

or maybe the 'by' argument of rollapply.

   - Brian

_______________________________________________
R-SIG-Finance at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions
should go.



More information about the R-SIG-Finance mailing list