Using integrate() with vectors as boundaries rather than scalars
Or just: lapply(x, integrate, f = dnorm, upper = Inf)
On 10/11/06, Tobias <tobias_elbert at hotmail.com> wrote:
I think I have figured it out myself, would however like to know the opinion
of more experienced coders. Is this a "good" way of approaching this:
cumdnorm1 <- function(x) {integrate(dnorm,x,Inf)}
evalvec <- function(x) {lapply(x,cumdnorm1)}
It does return the desired result.
Tobias wrote:
Hi all
my apologies if above title is misleading, but here is my problem anyways:
I need to evaluate an integral n times. Since I can't get my head around
vectorization as of yet, I have coded it up in a loop, i.e.:
for (i in 1:n)
{
z[i] <- integrate(dnorm,x[i],Inf)
}
Since n is quite large in my operation, ~40000, I would rather stack all
the elements of x into a vector and then evaluate the integral for each
vector element in x up to infinity. So that the operation would look
something like this:
z <- integrate(dnorm,x,Inf)
I am not sure if this works, as I am new to R, however managed the
equivalent in MatLab. Is this possible at all? If yes, your input would be
highly appreciated.
Regards
Tobias
-- View this message in context: http://www.nabble.com/Using-integrate%28%29-with-vectors-as-boundaries-rather-than-scalars-tf2421074.html#a6750170 Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at 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 and provide commented, minimal, self-contained, reproducible code.