Re: [R] find mean of a list of timeseries

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Wed 18 Jan 2006 - 13:11:07 EST

          I suggest you not worry about the loops. A decade ago (e.g., with S-Plus 3 or 3.1), loops were a major problem. Releases of S-Plus and R since then have made substantial improvements in loop computations.

          My preferred solution to your problem, as I understand it, is as follows:

a<-ts(matrix(c(1,1,1,10,10,10,20,20,20),nrow=3),names=c('var1','var2','var3'))
b<-ts(matrix(c(2,2,2,11,11,11,21,21,21),nrow=3),names=c('var1','var2','var3'))
c<-ts(matrix(c(3,3,3,12,12,12,22,22,22),nrow=3),names=c('var1','var2','var3'))

data<-list(a,b,c)

mean.list <- function(object){

   n <- length(object)
   a1 <- object[[1]]
   if(n>1)for(i in 2:n){
     a1 <- a1+object[[i]]
   }
   a1/n
}

gc()
start.time <- proc.time()
mean.list(data)
(et <- proc.time()-start.time)

> mean.list(data)

Time Series:
Start = 1
End = 3
Frequency = 1

   a1.a1.var1 a1.a1.var2 a1.a1.var3

1          2         11         21
2          2         11         21
3          2         11         21

> (et <- proc.time()-start.time)

[1] 0.04 0.00 0.09 NA NA

          My attempt to avoid loops is the following:

apply.list <- function(object, FUN){

   dim.list <- sapply(object, dim)
   if(is.list(dim.list))

     stop("attributes of ", deparse(substitute(object)),
          " do not have the same number of dimensions.")
   allEqual <- apply(dim.list, 1, function(x)diff(range(x)))    if(any(allEqual !=0))
     stop("attributes of ", deparse(substitute(object)),
          " don't all have the same dimensions")
   n <- length(object)
   Dim <- c(n, dim.list[,1])
#

   obj.array <- array(unlist(object), dim=Dim)    k <- length(dim)
   apply(obj.array, 2:k, FUN)
}

> start.time <- proc.time()
> apply.list(data, mean)

      [,1] [,2] [,3]

[1,]    2    2    2
[2,]   11   11   11
[3,]   21   21   21

> (et <- proc.time()-start.time)

[1] 0.00 0.00 0.07 NA NA         

         This seems to run slightly faster on this miniscule data. However the answer is transposed, and I don't want to take the time to understand and fix that problem.

	  hope this helps.
	  spencer graves

tom wright wrote:

> Can someone please give me a clue how to 're'write this so I dont need
> to use loops.
> 
> a<-ts(matrix(c(1,1,1,10,10,10,20,20,20),nrow=3),names=c('var1','var2','var3'))
> b<-ts(matrix(c(2,2,2,11,11,11,21,21,21),nrow=3),names=c('var1','var2','var3'))
> c<-ts(matrix(c(3,3,3,12,12,12,22,22,22),nrow=3),names=c('var1','var2','var3'))
> 
> data<-list(a,b,c)
> 
> I now want to find the means of all vectors var1,var2 and var3
> 
> i.e. I need to end up with a new time series with three data vectors
> (var1, var2 and var3)
> result<-ts(matrix(c(2,2,2,11,11,11,21,21,21),nrow=3),names=c('var1','var2','var3))
> 
> I think its the list thats throwing my use of apply, I might be wrong
> but what other data structure could I use?
> 
> Many thanks
> Tom
> 
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 18 13:36:50 2006

This archive was generated by hypermail 2.1.8 : Wed 18 Jan 2006 - 18:08:04 EST