[Rcpp-devel] rollapply

Hideyoshi Maeda hideyoshi.maeda at gmail.com
Wed Feb 26 18:56:23 CET 2014


Hi Kevin,

I just had a quick look, at RcppRoll and noticed that almost all of the results have a single value as an output, and was wondering if it was possible to be able to get more than one output per loop…

to give a quick example, i would be grateful if you could show me how the following could be converted using rollit(). Hopefully this is possible.

n <- 50
m <- 3
mu <- 0.0001
sigma <- 0.003

require(xts)
x <- xts(matrix(rnorm(n*m,mu,sigma),ncol=m),Sys.Date()-c(n:1))

foo <- function(x){
  c(mean(x),sd(x))
}

foo1 <- function(x){
  y <- x[,1]+(3*x[,2])+(log(abs(x[,3])))
  tmp <- data.frame(cbind(y=y,x=x))
  colnames(tmp) <- c("y","a","b","c")
  c(sum(y), sd(y), coef(lm(y~a+b+c,data=tmp)))
}

rollapply(x,width=10,function(y){foo(y)},by.column=FALSE)
rollapply(x,width=10,function(y){foo1(y)},by.column=FALSE)

Thanks

HLM

On 26 Feb 2014, at 16:57, Kevin Ushey <kevinushey at gmail.com> wrote:

> And, I'm the RcppRoll developer -- I haven't touched the package in a while so let me know if you run into any rough corners / have ideas for improvements. The package is still pretty 'young' and not quite battle-worn yet.
> 
> Thanks,
> Kevin
> 
> 
> On Wed, Feb 26, 2014 at 8:36 AM, Hideyoshi Maeda <hideyoshi.maeda at gmail.com> wrote:
> Didn’t even know it existed! will take a look now. Thanks
> On 26 Feb 2014, at 16:35, Dirk Eddelbuettel <edd at debian.org> wrote:
> 
> >
> > On 26 February 2014 at 16:23, Hideyoshi Maeda wrote:
> > | Hi Rcpp devel list,
> > |
> > | One of the functions I like to use is zoo’s rollapply() function. However sometimes this can be slow as it is basically a long loop using mapply.
> > |
> > | Given how fast loops are in C++ this seems like this would be the perfect thing to covert into C++, BUT, one of the benefits of rollapply, is that it is flexible and you can put in any R custom function you like. I think there are some functionality in Rcpp that allow you to carry out custom R functions, but not sure what it is or if it would be helpful as you might have to recursively keep calling R each time it need the custom function in the loop.
> > |
> > | So I guess my question is 1) is there an existing implementation that has converted rollapply to C++ and 2) if not, then given the desired flexibility of using custom functions in rollapply, would converting it into C++ code be worthwhile? 3) if it would be worthwhile, then what should I be researching to be able to get custom functions into Rcpp/C++?
> >
> > Have you looked at RcppRoll yet?  See
> >
> >   http://cran.r-project.org/web/packages/RcppRoll/index.html
> >
> > Dirk
> >
> > --
> > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> 
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140226/40ade546/attachment.html>


More information about the Rcpp-devel mailing list