Quantitative finance collector
C++ Matlab VBA/Excel Java Mathematica R/Splus Net Code Site Other
Jun 16

R Sapply Problem

Posted by abiao at 19:07 | Code » R/Splus | Comments(2) | Reads(9750)
Any expert in R please educates me. I have got a problem about the sapply (or lapply), it made me headache for over two hours.

As "for loop" is very slow in R, we should try best to avoid using it, and to use vectorization instead. sapply is designed for this, for example, instead of:
for (i in 1:10) {
z[i] <- mean(x[1:i])
}

we could use
z <- sapply(1:10, function(i, x) {mean(x[1:i])}, x)


It went well, but what if besides computing z, I need to update another variable, for example, with loop, it is
temp <- 3
for (i in 1:10) {
x[i] <- x[i]-temp
z[i] <- mean(x[1:i])
temp <- x[i]-z[i]
}


in this case, temp is changing every step (it doesn't have to be a function of z[i]). How to vectorize that and use sapply then? since sapply can't return two variables z and temp.

I tried to define a matrix and store z in the first column and temp in the second column and return the matrix, however, failed.

Many thanks.


PS, NO, still not correct, working on it...
ah, I worked it out, it can be done by passing z itself to sapply, that's good.
the following is a sapply example returning the same result for "for loop" and "sapply".
sapply.example <- function(nsim = 10){
  x <- rnorm(nsim)
  y <- list()
  z.for <- array(0, nsim)  
  temp <- 3
  for (i in 1:nsim) {
    x[i] <- x[i]-temp
    z.for[i] <- mean(x[1:i])
    temp <- x[i]-z.for[i]
  }
  y$z.for <- z.for

  z.sapply <- array(0,2*nsim)
  z.sapply[1] <- 3
  z.sapply <- sapply(seq(1,2*nsim,by=2), function(i,x,z.sapply) {
        temp <- z.sapply[i]
        z.temp <- mean(x[1:((i+1)/2)])
        temp <- x[((i+1)/2)]-z.temp
        z.sapply[i] <- temp
        z.sapply[i+1] <- z.temp
        z.sapply[i:(i+1)]
      }, x, z.sapply, simplify =TRUE)  
  y$z.sapply <- z.sapply[seq(2,2*nsim, by=2)]
  y
}


Tags:
How about this? It must be too late, but i would be glad if it might be a help.


temp.ini <- 3
x.ini <- rnorm(10)

## loop version
temp <- temp.ini
x <- x.ini
z <- NA
for(i in 1:length(x)){
  x[i] <- x[i] - temp
  z[i] <- mean(x[1:i])
  temp <- x[i] - z[i]
}

## vectorized version
make.func <- function(temp, x){
  z <- NA
  function(i){
    x[i] <<- x[i] - temp
    z[i] <<- mean(x[1:i])
    temp <<- x[i] - z[i]
    list(temp=temp, x=x, z=z)
  }
}

func <- make.func(temp.ini, x.ini)
l <- sapply(1:length(x.ini), func, simplify=F)

## check of result
all(temp == l[[10]]$temp)
all(x == l[[10]]$x)
all(z == l[[10]]$z)


This code depends on the order of calls of func.
I'm not sure that sapply always calls a function in the order func(1), func(2), ....
If this is not the case, the code doesn't work.
Thanks so much, hotoku, I am checking it.
Pages: 1/1 First page 1 Final page
Add a comment
Emots
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember
Nickname   Password   Optional
Site URI   Email   [Register]