On 2012-04-06 07:19, Navin Goyal wrote:
Thank you so much for your help Berend.
I did not see that my code had a typo and it was thus wrongly written ( I
overlooked the i that was supposed to be actually 1)

instead of       for (q in *1*:length(comb1$ID))
  I had it as         for (q in *i*:length(comb1$ID))

This is a good example to show that it's usually better to
use seq_along() or seq_len() instead of 1:x.

Peter Ehlers


It works correctly as expected
Thanks again.


Navin






On Fri, Apr 6, 2012 at 9:56 AM, Berend Hasselman<b...@xs4all.nl>  wrote:


On 06-04-2012, at 13:14, Navin Goyal wrote:

Apologies for the lengthy code.
I tried a simple (and shorter) piece of code (pasted below) and it still
gives me the same error for last few rows. Is this a bug or am I doing
something totally wrong?  Could anyone please provide some help/pointers ?


You are not specifying what the  error is for the last few rows?
You are doing something completely wrong (AFAICT).
See below.

PS.  beta0 was fixed to 0.001 in the previous code. Also if I manually
estimated the integral isnt 0. If I scramble the row order, it is again
only the last few rows that dont integrate.

See below.

Thanks

data1<-expand.grid(1:5,0:6,10)
names(data1)<-c("ID","TIME", "DOSE")
data1<-data1[order(data1$DOSE,data1$ID,data1$TIME),]
ed<-data1[!duplicated(data1$ID) , c(1,3)]
ed$base=1
ed$drop=1
set.seed(5234123)
k<-0
for (i in 1:length(ed$ID))
{
k<-k+1
ed$base[k]<-100*exp(rnorm(1,0,0.2))
ed$drop[k]<-0.20*exp(rnorm(1,0,0.5))
}

Why are you not using i to index ed$XXX?
You are not using i.
Simplify to

for (k in 1:length(ed$ID))
{
ed$base[k]<-100*exp(rnorm(1,0,0.2))
ed$drop[k]<-0.20*exp(rnorm(1,0,0.5))
}

comb1<-merge(data1[, c("ID","TIME")], ed)
comb1$disprog<-comb1$base*exp(-comb1$drop*comb1$TIME)
comb1$integral=0
hz.func1<-function(t,bshz,beta1, change)
{ ifelse(t==0,bshz, bshz*exp(beta1*change)) }

Insert here

comb1
i
length(comb1$ID)

and you should see that i is 5 and that length(comb1$ID) is 35.

q<-0
for (m in i:length(comb1$ID))
{
q<-q+1
comb1$integral[q]<-integrate(hz.func1, lower=0, upper=comb1$TIME[q],
           bshz=0.001,beta1=0.035,
          change=comb1$disprog[q])$value
}
comb1


1. Why does your for loop variable m start with i and not 1? (as I told
you in my first reply)
2. Why are you not using the for loop variable m?
3. So from the above m starts at 5 and stops at 35 (==>  312 steps)
4, so you are filling elements 1 to 31 of comb1 and items 32 to 35 are
unchanged.

5. why don't you do this

for (q in 1:length(comb1$ID))
{
comb1$integral[q]<-integrate(hz.func1, lower=0, upper=comb1$TIME[q],
          bshz=0.001,beta1=0.035,
         change=comb1$disprog[q])$value
}
comb1

This avoids a useless variable m and fill all elements of comb1.
And you could just as well reuse variable k; there is no need for a new
variable (q) here.

Berend






______________________________________________
R-help@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.

Reply via email to