[R] integrate / Vectorize question

Rui Barradas ruipbarradas at sapo.pt
Thu Oct 4 00:06:20 CEST 2012


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,

Rui Barradas
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
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list