[R] Avoid loop with the integrate function
Berend Hasselman
bhh at xs4all.nl
Mon Apr 9 07:26:09 CEST 2012
On 09-04-2012, at 01:26, Navin Goyal wrote:
> Hi,
> I am not sure I follow the scalar part of the suggestion. Could you please elaborate it a bit more? Each row has a different value of score for each subject at each time point.
That last remark has nothing to do with it.
The point is that in your function func1 the returned value doesn't depend on the argument t.
So func1 is returning a vector of length==length(t) of identical values except for the first entry which is zero.
Do this after the loop for comb3
# No integral since expression in func1 doesn't depend on t
comb4<-comb2
comb4$cmhz=0
comb4<-comb4[order(comb4$ID, comb4$TIME), ]
for (q in 1:length(comb4$ID))
{
comb4$cmhz[q]<-comb4$TIME[q]*func1(comb4$TIME[q],
cov1=0.001,beta1=0.02,
change=comb4$score[q], other=comb4$frac[q])
}
head(comb4)
all.equal(comb3$cmhz, comb4$cmhz)
You don't need integrate to get what you seem to be wanting.
Berend
> Also I simplified the example but there are other terms that change over each row for each subject.
> Does this mean that loops is the only way to go ?
>
> Thanks again for your help and time.
>
> Navin Goyal
>
>
> On Sun, Apr 8, 2012 at 4:03 AM, Berend Hasselman <bhh at xs4all.nl> wrote:
>
> 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
>
>
>
>
> --
> Navin Goyal
>
More information about the R-help
mailing list