Tobias
2006-Oct-11 03:59 UTC
[R] Using integrate() with vectors as boundaries rather than scalars
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#a6749926
Sent from the R help mailing list archive at Nabble.com.
Tobias
2006-Oct-11 04:37 UTC
[R] Using integrate() with vectors as boundaries rather than scalars
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.