# This function is to simulate the appropriate data and to produce the joint modelling # results for Cox.b, Cox.tp, joineR, JM.pc, JM.sp and JM.Cox. sim.exp.fn=function(n,nr.gp,tframe,a0,a1,a2,randcov,err.sd,ld1,ld2,tm.f,cs,cs.rate,ax.time) { if (cs.rate<=0) stop("Censoring rate must be > 0.") ##### Simulate data ##### t=0:(tframe-1) # Timepoints tdv=vector("numeric") # Initialize time-dependent (explanatory) variable. tdvm=vector("numeric")# Initialize 'true' value of time-dependent (explanatory) variable. # Simulate time-dependent explanatory variable assuming no interaction with treatment. for (i in 1:(n*nr.gp)) { #print(i) e=rnorm(length(t),0,err.sd) #Simulate errors. if (dim(randcov)[1]==2) b.coef=mvrnorm(1,c(0,0),randcov) # Simulate random effects coefficients. else b.coef=mvrnorm(1,c(0,0,0),randcov) b0=b.coef[1] b1=b.coef[2] if (length(b.coef)==2) b2=0 else b2=b.coef[3] mx=round(a0+a1*t+a2*(t^2)+b0+b1*t+b2*(t^2)) x=round(mx+e) tdvm=rbind(tdvm,mx) tdv=rbind(tdv,x) } subj=1:(n*nr.gp) dimnames(tdv)[[1]]=NULL tdv=data.frame(tdv) varnames=paste("X",0:(tframe-1),sep="") names(tdv)=varnames M=data.frame(subj,tdv) TDV.w=M # Data of time-dependent variable in wide format ### Simulate transition data. transtat=rep(0,n*nr.gp) # Initialize transition status. nrdays=rep(tframe,n*nr.gp) # Initialize time to transition/censoring. for (i in 1:(n*nr.gp)) { #print(i) for (j in 1:tframe) { x=tdvm[i,j] lda=exp(ld1*x+ld2-tm.f*(ceiling(i/n)*0.5-0.5) p=1-exp(-lda) z=rbinom(1,1,p) if (z==1) { transtat[i]=1 nrdays[i]=j break } }} ### Simulate censoring. if (cs==1) cs.time=runif(n*nr.gp,1,tframe/cs.rate) if (cs==2) { lda=log(1-cs.rate)/(-tframe) # Assume Exponential distn to compute censoring rate. cs.time=rexp(n*nr.gp,lda) # Censoring times } cs.time[cs.time>tframe]=tframe x=(transtat==1 & nrdays>cs.time) nrdays[x]=cs.time[x] transtat[x]=0 x=(transtat==0 & nrdays>cs.time) nrdays[x]=cs.time[x] nrdays=round(nrdays) nrdays[nrdays==0]=1 ### Change post transition/censoring values of time-dependent variable to missing. for (i in 1:(n*nr.gp)) { if (nrdays[i]