[R] Error in Rolling window of function - rollapply
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jul 8 02:00:11 CEST 2009
Your excel calculation does not correspond to your rollapply call.
rollapply(x, k, f) should have length(x) - k + 1 elements so in
this case it should have 37 - 20 + 1 = 18.
To take a simpler example,
> rollapply(zoo(1:5), 3, sum)
2 3 4
6 9 12
This has 5 - 3 + 1 = 3 elements and the result is:
zoo(c(1+2+3, 2+3+4, 3+4+5), 2:4)
You can use na.pad = TRUE to get 5 elements out:
> rollapply(zoo(1:5), 3, sum, na.pad = TRUE)
1 2 3 4 5
NA 6 9 12 NA
Suggest you re-read ?rollapply and try out all the
examples there.
On Tue, Jul 7, 2009 at 3:38 PM, Andriy Fetsun<fetsun at googlemail.com> wrote:
> Dear Colleagues,
>
> I have faced with the problem that function rollaply with rolling window for
> calculation of volatility doesn't give the all results of calculations.
>
> I have run the rolling window for calculation in Excel and obtained that the
> number of outputs for Excel is 36 and for R is 18. The total number of
> observations is 37. In the attachment you can find pdf of the Excel and
> Excel file. Below you can find my commands in R.
>
>> Ret<-read.csv("data.csv")
>> Ret_1<-zoo((Ret))
>> Ret_2<-rollapply(Ret_1,20,sd)
>> Volatility_20<-sd(Ret_2)
>> Volatility_20
> Data
> 0.03802453
>> Ret_2
> Data
> 10 0.9170356
> 11 0.9147269
> 12 0.8982093
> 13 0.9169873
> 14 0.9156607
> 15 0.8961262
> 16 0.9615083
> 17 0.9804045
> 18 0.9853978
> 19 0.9888628
> 20 0.8798952
> 21 0.8408839
> 22 0.9429053
> 23 0.9498478
> 24 0.9464991
> 25 0.9305918
> 26 0.9408653
> 27 0.9434271
>>
>
> --
> Best regards,
>
> Andy
>
More information about the R-help
mailing list