[R] integrate / Vectorize question

Gerrit Draisma gdraisma at xs4all.nl
Wed Oct 3 22:59:33 CEST 2012


Dear R-users,

I want to use the function Fnum below
in another integrate procedure.

How do I write Fnum so that it returns a vector of values?
And will the last integrate return the right answer?

(I tried Vectorize() in several places,
but got all sorts of errors.)

Thanks for helping out.

Gerrit.


This my code
=========================
beta=c(3,0.3)*1E-2
mu<-5

f<-function(x,beta){beta[1]+beta[2]*x}

Fnum<-function(x,beta){
    integrate(f,lower=-x,upper=0,beta=beta)$value
    }

Fsym<-function(x,beta){x*(beta[1]-beta[2]/2*x)}

y<-1:4
Fsym(y,beta)
Fnum(y,beta)

g<-function(y,mu){exp(-y/mu)/mu}

integrate((function(y,beta,mu){Fsym(y,beta)*g(y,mu)}),lower=0,upper=2*mu,
   beta=beta,mu=mu)

#This fails
integrate((function(y,beta,mu){Fnum(y,beta)*g(y,mu)}),lower=0,upper=2*mu,
   beta=beta,mu=mu)

t<-seq(0,2*mu, by=0.2)
0.2*sum(tapply(t,t,function(y,beta,mu){Fnum(y,beta)*g(y,mu)},
    beta=beta,mu=mu))



More information about the R-help mailing list