Dear R users,
Please help me with the following problem. I have a data set of the form ´dat´
 
dat <- data.frame(id = seq(1:5), trt=c(0,0,1,1,1),tid=c(0,0,0,0,0),
                  ntid=c(0,0,0,0,0))
 
The function auto is used to generate the autoregressive model:
 
auto <- function(seed,delta,beta,maxt,dat){
set.seed(seed)
 sp <- lapply(split(dat,dat$id),function(x){
 while(x$tid < maxt){
 lambda <- delta*exp(beta*x$trt +0.1*x$ntid)
 wait <- rexp(1,lambda)
 x$tid <- x$tid + wait
  x$ntid <- ifelse(x$tid < maxt, x$ntid+1,x$ntid)
#browser()
  }
 x
})
  mydat <- do.call(rbind,sp)
}
and the result looks like

dat <- auto(seed=1,delta=0.02,beta=-0.3,maxt=112,dat=dat)

 id trt      tid ntid
1  1   0 116.9770    4
2  2   0 144.7484    0
3  3   1 115.9453    1
4  4   1 150.3917    2
5  5   1 127.0123    1
However, at each iteration for unique patients, I wish to see the results. The expected result in longitudinal form which I got using browser is
 
dat2
 id trt      tid ntid
1  1   0 37.75909    1
2  1   0 91.21882    2
3  1   0 97.18355    3  
4  1   0 102.3617    4  
5  1   0 116.977     4
6  2   0 144.7484    0  
7  3   1 82.98676    1  
8  3   1 115.9453    1  
9  4   1 64.56155    1  
10 4   1 73.54167    2 
11 4   1 150.3917    2
12 5   1 51.43164    1
13 5   1 127.0123    1
 
Please how do I modify the function 'auto' such that it produce dat2.
 
Thanks
John
	[[alternative HTML version deleted]]

