# [R] integrate / Vectorize question

Gerrit Draisma gdraisma at xs4all.nl
Thu Oct 4 12:35:56 CEST 2012

```Dag Rui,
Yes, it helped a lot!
Thanks and greetings,
Gerrit.

Op 10/4/2012 12:06 AM, Rui Barradas schreef:
> Hello,
>
> Try
>
>
> Fnum <- function(x, beta){
>     sapply(x, function(x) integrate(f,lower=-x,upper=0,beta=beta)\$value)
> }
>
>
> And all results were the same.
>
> Hope this helps,
>
> Em 03-10-2012 21:59, Gerrit Draisma escreveu:
>> 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))
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help