Dear R users,

Kindly help me to look into the following problem. I have a dataset like the 
following
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))
I want to create waiting times using 
 
B <- rexp(1)
f1 <- (1/phi)+ ntid; f2 <- abs(1+phi*delta*tid*exp(beta*trt))
f3 <- phi*delta*exp(beta*trt)
wait <- ((exp((B/f1)+log(f2))-1)/f3)-tid}

for each subject. When the waiting time is computed, it is added to the 
previous waiting time (tid). The result is checked if it is less than some 
time, say 112. If the new tid (i.e tid +wait) is less, ntid is incremented by 1 
(i.e ntid=ntid+1), and the process is repeated until new tid is greater than 
112. At this stage, ntid =ntid.

Here is my code:

waiting <- function(seed,phi,delta,beta,maxt,dat){
set.seed(seed)
 sp <- lapply(split(dat,dat$id),function(x){
 while(x$tid < maxt){
  B <- rexp(1)
f1 <- (1/phi)+ x$ntid; f2 <- abs(1+phi*delta*x$tid*exp(beta*x$trt))
f3 <- phi*delta*exp(beta*x$trt)
wait <- ((exp((B/f1)+log(f2))-1)/f3)-x$tid
 x$tid <- x$tid + wait
  x$ntid <- x$ntid+1}
 x
})
  mydat <- do.call(rbind,sp)
}
john <- waiting(seed=1,phi=0.5,delta=0.02,beta=-0.3,maxt=112,dat=dat)
The returned data is

 id trt      tid ntid
1  1   0 116.2947    2
2  2   0 124.2211    4
3  3   1 114.6370    1
4  4   1 117.3167    3
5  5   1 135.5860    1

Please how do I return the values of tid and ntid in each loop and not just the 
final tid and ntid such that the final data is now in longitudinal form.

Thank you very much

John
        [[alternative HTML version deleted]]

______________________________________________
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