Skip to content
Prev 7260 / 10988 Next

[Rcpp-devel] rollapply

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:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140226/40ade546/attachment.html>
Message-ID: <7DF90594-58C9-4C56-A262-5BF7D91A8DB1@gmail.com>
In-Reply-To: <CAJXgQP0uoMhSEHt1dWDgrjvhJ74esfj6MW-73R4ubYNa+qgncw@mail.gmail.com>