[R] Zoo rolling window with increasing window size

Christofer Bogaso bogaso.christofer at gmail.com
Fri Aug 11 02:01:44 CEST 2017


Thanks Joshua, your solution is perfect.

On Fri, Aug 11, 2017 at 12:11 AM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> Replace "sum" with your custom function's name.  I don't see any
> reason why that wouldn't work, and the problem with my solution is not
> clear in your response.
>
> r <- rollapplyr(x, seq_along(x), yourCustomFunctionGoesHere)
>
> On Thu, Aug 10, 2017 at 1:39 PM, Christofer Bogaso
> <bogaso.christofer at gmail.com> wrote:
>> Hi Joshua, thanks for your prompt reply. However as I said, sum()
>> function I used here just for demonstrating the problem, I have other
>> custom function to implement, not necessarily sum()
>>
>> I am looking for a generic solution for above problem.
>>
>> Any better idea? Thanks,
>>
>> On Fri, Aug 11, 2017 at 12:04 AM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>>> Use a `width` of integer index locations.  And you likely want =
>>> "right" (or rollapplyr(), as I used).
>>>
>>> R> set.seed(21)
>>> R> x <- rnorm(10)
>>> R> rs <- rollapplyr(x, seq_along(x), sum)
>>> R> cs <- cumsum(x)
>>> R> identical(rs, cs)
>>> [1] TRUE
>>>
>>>
>>> On Thu, Aug 10, 2017 at 1:28 PM, Christofer Bogaso
>>> <bogaso.christofer at gmail.com> wrote:
>>>> Hi again,
>>>>
>>>> I am wondering there is any function for 'zoo' time series, where I
>>>> can apply a user defined function rolling window basis, wherein window
>>>> size is ever increasing i.e. not fixed. For example, let say I have
>>>> below user defined function and a zoo time series :
>>>>
>>>>> library(zoo)
>>>>
>>>>> UDF = function(x) sum(x)
>>>>
>>>>> TS = zoo(rnorm(10), seq(as.Date('2017-01-01'), as.Date('2017-01-10'), by = '1 day'))
>>>>
>>>>>
>>>>
>>>> Now I want to apply UDF (this can be any custom function, however here
>>>> I put it just quick example) rolling window basis like :
>>>>
>>>> 1st data point = 1st data point of TS
>>>> 2nd data point = sum of 1st and 2nd data points of TS
>>>> 3rd data point = sum of 1st 2nd and 3rd data points of TS
>>>>
>>>> so on
>>>>
>>>> I am aware of the rollapply() function from zoo, however, appears like
>>>> it is only for fixed window size.
>>>>
>>>> Appreciate any pointer how to achieve above strategy of implementing
>>>> rolling calculation based on increased window size.
>>>>
>>>> Thanks for your time.
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> 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.
>>>
>>>
>>>
>>> --
>>> Joshua Ulrich  |  about.me/joshuaulrich
>>> FOSS Trading  |  www.fosstrading.com
>>> R/Finance 2017 | www.rinfinance.com
>
>
>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com
> R/Finance 2017 | www.rinfinance.com



More information about the R-help mailing list