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. ______________________________________________ [hidden email] 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. |
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 <[hidden email]> 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. > > ______________________________________________ > [hidden email] 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 ______________________________________________ [hidden email] 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. |
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 <[hidden email]> 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 > <[hidden email]> 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. >> >> ______________________________________________ >> [hidden email] 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 ______________________________________________ [hidden email] 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. |
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 <[hidden email]> 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 <[hidden email]> 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 >> <[hidden email]> 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. >>> >>> ______________________________________________ >>> [hidden email] 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 ______________________________________________ [hidden email] 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. |
In reply to this post by Bogaso
Something like this?
set.seed(123) y <- rnorm(20) sapply(1:length(y), function(x) sum(y[1:x])) or this, depending what is the output of your custom function lapply(1:length(y), function(x) sum(y[1:x])) On Thu, Aug 10, 2017 at 8:39 PM, Christofer Bogaso < [hidden email]> 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 <[hidden email]> > 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 > > <[hidden email]> 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. > >> > >> ______________________________________________ > >> [hidden email] 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 > > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by Joshua Ulrich
Thanks Joshua, your solution is perfect.
On Fri, Aug 11, 2017 at 12:11 AM, Joshua Ulrich <[hidden email]> 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 > <[hidden email]> 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 <[hidden email]> 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 >>> <[hidden email]> 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. >>>> >>>> ______________________________________________ >>>> [hidden email] 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 ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |