mercredi 25 mai 2016

R: fast table lookup with ifelse vectorization without negative subscript error

I want to use a table-lookup to speed up some but not other calculations. For example, for the normal distribution cdf:

BINS <- 10
MINZ <- (-5)
MAXZ <- 5

cdftbl <- rep(NA, BINS+2)

xi <- 0; SSZ <- (MAXZ-MINZ)/BINS
while (xi<=BINS+2) {
    x <- MINZ+xi*SSZ
    cdftbl[xi+1] <- pnorm(x, log=TRUE)
    xi <- xi+1
}

fastlogcdf <- function( x, m=0, sd=1 ) {
    z <- (x-m)/sd
    zi <- (z-MINZ)/(MAXZ-MINZ) * BINS
    zi.whole <- as.integer(zi)
    zi.frac <- zi-zi.whole
    ifelse( (z<=MINZ)|(z>=MAXZ), cdf(x,m,sd,log=TRUE),
           cdftbl[zi.whole+1]+zi.frac*(cdftbl[zi.whole+2]-cdftbl[zi.whole+1]))
}

this works well with

   fastlogcdf( seq(-2,2,0.5))

but not with

   fastlogcdf( seq(-8,8,0.5))

because the ifelse wants to evaluate the two outcomes completely before it assigns based on the condition. usually, I can ignore this, but this produces an intermediate error that only 0's may be mixed with negative subscripts . of course, there is no cdftable[-8+1].

what is the R way of solving this little dilemma? advice appreciated.

regards, /iaw

Aucun commentaire:

Enregistrer un commentaire