[R] Avoid loop with the integrate function

Berend Hasselman bhh at xs4all.nl
Sun Apr 8 10:03:52 CEST 2012


On 08-04-2012, at 08:28, Navin Goyal wrote:

> Dear R users,
> I am running a loop with the integrate function. I have pasted the code
> below. I am integrating a function from time=0 to the time value in every
> row.
> I have to perform this integration over thousands of rows with different
> parameters in each row. Could someone please suggest if there is an
> efficient/faster/easier way to do this by avoiding the loops ?
> 
> Thank you so much for your help and time.
> --
> Navin Goyal
> 
> #####################
> dose<-10
> time<-0:5
> id<-1:5
> data1<-expand.grid(id,time,dose)
> names(data1)<-c("ID","TIME", "DOSE")
> data1<-data1[order(data1$ID,data1$TIME),]
> 
> ed<-data1[!duplicated(data1$ID) , c("ID","DOSE")]
> set.seed(5324123)
> 
> for (k in 1:length(ed$ID))
> {
> ed$base[k]<-100*exp(rnorm(1,0,0.05))
> ed$drop[k]<-0.2*exp(rnorm(1,0,0.01))
> ed$frac[k]<-0.5*exp(rnorm(1,0,0.1))
> }

Why not

ed$base <- 100*exp(rnorm(length(ed$ID), 0, 0.05))

etc.

> comb1<-merge(data1[, c("ID","TIME")], ed)
> comb2<-comb1
> comb2$score<-comb2$base*exp(-comb2$drop*comb2$TIME)
> 
> func1<-function(t,cov1,beta1, change,other)
> {
> ifelse(t==0,cov1, cov1*exp(beta1*change+other))
> }

AFAICS, cov1, beta1, change and other are scalars. 
So when an item of t is == 0 then the function value is cov1 and in all other cases it is the same scalar and does not depend on t. So func1 is a step function. You could calculate the integral directly.

Berend



More information about the R-help mailing list