Home > Back-end >  apply Integrate function to a vector
apply Integrate function to a vector

Time:04-18

I'm a newbie to R programming and I have an assignment to submit so my prob is the following: I wrote the following function :

   phi <- function(x)
{
exp(-x^2/2/sqrt(2 * pi)
}

quantiles <- seq(from = 0, to = 5.5, by = 0.01) 

and I should be using integrate () function to calculate phi(x) for all x of the vector quantiles and store them in a vector named probs.

I started the following and I got blocked

Rep <- function (x) {

probs <-integrate (phi, lower = Inf , upper = x)$Value } 

Not sure if this is the right way to apply the function integrate() to a vector. Your help is much appreciated

CodePudding user response:

You could do the following:

phi <- function(x)exp(-x^2/2)/sqrt(2 * pi)
quantiles <- seq(from = 0, to = 5.5, by = 0.01) 


fun<-Vectorize(function(x)integrate(phi,-Inf, x)[[1]])

fun(quantiles)
  • Related