[Rd] Speed up code, profiling, optimization, lapply vs. loops

Thorn Thaler thothal at sbox.tugraz.at
Tue Jul 7 14:28:46 CEST 2009


Thanks for ur good advices. I've run Rprof and figured out that the 
bottleneck lies within the deviance function, which uses "integrate" to 
determine the deviance numerically. Since I'm not aware of a closed form 
solution for the integral

\int 1/(t^k*(1-t)^l) dt, \forall k,l \in R  ,

I've to rely on the numerical procedure. 'glm' itself is according to 
Rprof suprisingly not that time consuming and I could narrow the lack of 
performance down to multiple calls of the (numerically determined) 
deviance function, which is currently implemented by:

devf <- function(y, mu, ...) {
   mapply(function(y, mu, ...) {
     integrand <- function(u, y, ...)
       return((y - u) / varf(u, ...))
     int <- try(suppressWarnings(
       integrate(integrand, lower=y, upper=mu, y=y, ...)), silent=TRUE)
     if (class(int) == "try-error") {
       stop("Deviance could not be evaluated!\n", int)
     } else {
       return(-2 * int$value)
     }
   }, y, mu, ...)
}

where 'varf' denotes the particular variance function depending on some 
parameter vector.

I'd appreciate any comments on the code. Is it possible to speed up the 
computation?



More information about the R-devel mailing list