You can't vectorize the loop because integrate() is not vectorized, but you really need to pass only one index variable if you redefine your function so that you are not re-creating it for each step in the loop. That would let you use sapply(). Here's a toy example that should generalize to your problem:
> v <- 1:5 > w <- rep(1, 5) > fun <- function(x, m, n) n*exp(-m*x) > eta <- sapply(1:5, function(i) integrate(fun, 0, Inf, m=v[i], n=w[i])$value) > eta [1] 1.0000000 0.5000000 0.3333333 0.2500000 0.2000000 Creating the function outside the loop should speed things up. ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of David Winsemius > Sent: Thursday, December 06, 2012 12:59 PM > To: Doran, Harold > Cc: r-help@r-project.org > Subject: Re: [R] Vectorizing integrate() > > > On Dec 6, 2012, at 10:10 AM, Doran, Harold wrote: > > > I have written a program to solve a particular logistic regression > problem using IRLS. In one step, I need to integrate something out of > the linear predictor. The way I'm doing it now is within a loop and it > is as you would expect slow to process, especially inside an iterative > algorithm. > > > > I'm hoping there is a way this can be vectorized, but I have not > found it so far. The portion of code I'd like to vectorize is this > > > > for(j in 1:nrow(X)){ > > fun <- function(u) 1/ (1 + exp(- (B[1] + B[2] * (x[j] + u)))) * > dnorm(u, 0, sd[j]) > > eta[j] <- integrate(fun, -Inf, Inf)$value > > } > > > > The Vectorize function is just a wrapper to mapply. If yoou are able to > get that code to execute properly for your un-posted test cases, then > why not use mapply? > > > > Here X is an n x p model matrix for the fixed effects, B is a vector > with the estimates of the fixed effects at iteration t, x is a > predictor variable in the jth row of X, and sd is a variable > corresponding to x[j]. > > > > Is there a way this can be done without looping over the rows of X? > > > > Thanks, > > Harold > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > > and provide commented, minimal, self-contained, reproducible code. > > David Winsemius, MD > Alameda, CA, USA > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.