TABLE OF CONTENTS
simulation/gen2AG [ Functions ]
NAME
gen2AG --- create AG data frame
FUNCTION
Convert generated data into Anderson - Gill format
SYNOPSIS
319 gen2AG <- function(m, Ji, Zij, Rij1, Rij2, Cij, alternating, timedep){
INPUTS
m number of clusters Ji cluster size Zij list of generated covariates and times Rij1 recurrent event gap times 1 Rij2 recurrent event gap times 2 Cij censoring times alternating boolean epsisodic data indicator timedep time-dependent covariate indicator
OUTPUTS
data frame in format used by bivrec.agdata
SOURCE
322 if(timedep){ 323 Zij1 <- Zij$Zij1 324 Zij1times <- Zij$Zij1times 325 Zij2 <- matrix(0, dim(Zij1)[1], 100) 326 }else{ 327 if(!is.matrix(Zij) && !is.list(Zij)) { 328 Zij1 <- matrix(rep(Zij, 100), length(Zij), 100) 329 Zij2 <- matrix(0, dim(Zij1)[1], 100) 330 } 331 if(is.list(Zij)){ 332 Zij1 <- matrix(rep(Zij$Zij1, 100), length(Zij$Zij1), 100) 333 Zij2 <- matrix(rep(Zij$Zij2, 100), length(Zij$Zij2), 100) 334 } 335 Zij1times <- matrix(Inf, dim(Zij1)[1], 100) 336 } 337 kmax <- 100 # maximum number of events allowed before censoring 338 ## Compute the number of observed events for each individual 339 nevents1 <- rep(0, sum(Ji)) 340 ## Compute the number of observed events for each individual 341 nevents2 <- rep(0, sum(Ji)) 342 ncovchange <- rep(0, sum(Ji)) 343 ## observed events 344 for(ij in 1:sum(Ji)) nevents1[ij] <- sum(cumsum(Rij1[ij, ]) < Cij[ij]) 345 for(ij in 1:sum(Ji)) nevents2[ij] <- sum(cumsum(Rij2[ij, ]) < Cij[ij]) 346 ## observed covariate changes 347 for(ij in 1:sum(Ji)) ncovchange[ij] <- sum(cumsum(Zij1times[ij, ]) < Cij[ij]) 348 outdata.matrix <- matrix(0, sum(nevents1 + nevents2 + ncovchange + 1), 10) 349 outrow <- 1 350 # Loop runs over all individuals and generates an Anderson - Gill line 351 # for each event time that is less than the follow - up time 352 # Columns: i: Cluster, j: Individual, k: At risk for which event 353 # start / stop: Cumulative times, Z: Covariate values, Delta / delta: indicators 354 if(!alternating){ 355 for(i in 1:m){ 356 for(j in 1:Ji[i]){ 357 ij <- sum(Ji[0:(i - 1)]) + j 358 ThisFollowTime <- Cij[ij] 359 CumTimes1 <- cumsum(Rij1[ij, ]) 360 CumTimes2 <- cumsum(Rij2[ij, ]) 361 CumTimesZ <- cumsum(Zij1times[ij, ]) 362 CumTimes1 <- CumTimes1[CumTimes1 < ThisFollowTime] 363 CumTimes2 <- CumTimes2[CumTimes2 < ThisFollowTime] 364 CumTimesZ <- CumTimesZ[CumTimesZ < ThisFollowTime] 365 l1 <- length(CumTimes1) 366 l2 <- length(CumTimes2) 367 lZ <- length(CumTimesZ) 368 thisoutdata <- matrix(0, l1 + l2 + lZ + 1, 10) 369 thisoutdata[, 1] <- i;thisoutdata[, 2] <- j; 370 thisoutdata[, 6] <- c(CumTimes1, CumTimes2, CumTimesZ, ThisFollowTime) 371 thisoutdata[, 7] <- c(rep(1, l1), rep(0, l2 + lZ + 1)) 372 thisoutdata[, 8] <- c(rep(0, l1), rep(1, l2), rep(0, lZ + 1)) 373 thisoutdata[, 9] <- c(rep(NA, l1 + l2), Zij1[ij, (lZ > 0) * (1:lZ)], 374 Zij1[ij, min(which(cumsum(Zij1times[ij, ]) > ThisFollowTime))]) 375 thisoutdata[, 10] <- c(rep(NA, l1 + l2), Zij2[ij, (lZ > 0) * (1:lZ)], 376 Zij2[ij, min(which(cumsum(Zij1times[ij, ]) > ThisFollowTime))]) 377 thisoutdata <- matrix(thisoutdata[order(thisoutdata[, 6]), ], 378 dim(thisoutdata)[1], dim(thisoutdata)[2]) 379 thisoutdata[, 3] <- cumsum(c(1, thisoutdata[, 7] + 380 thisoutdata[, 8]))[ - (dim(thisoutdata)[1] + 1)] 381 for(ind in (dim(thisoutdata)[1]):1){ 382 if(!is.na(thisoutdata[ind, 9])) thisZ1 <- thisoutdata[ind, 9] 383 else thisoutdata[ind, 9] <- thisZ1 384 if(!is.na(thisoutdata[ind, 10])) thisZ2 <- thisoutdata[ind, 10] 385 else thisoutdata[ind, 10] <- thisZ2 386 k <- thisoutdata[ind, 3] 387 thisoutdata[ind, 4] <- L(i, j, k, Zij1[ij, k]) 388 if(ind > 1) thisoutdata[ind, 5] <- thisoutdata[ind - 1, 6] 389 } 390 newoutrow <- outrow + dim(thisoutdata)[1] 391 outdata.matrix[outrow:(newoutrow - 1), ] <- thisoutdata 392 outrow <- newoutrow 393 } 394 } 395 }else{ 396 for(i in 1:m){ 397 for(j in 1:Ji[i]){ 398 ij <- sum(Ji[0:(i - 1)]) + j 399 ThisFollowTime <- Cij[ij] 400 CumTime <- 0 401 k <- 1;eventct <- 1 402 while(CumTime < ThisFollowTime){ 403 if((CumTime + Rij1[ij, eventct]) < ThisFollowTime){ 404 thisoutdata <- c(i, j, k, L(i, j, k, Zij1[ij, eventct]), 405 CumTime, CumTime + Rij1[ij, eventct], 1, 0, 406 Zij1[ij, eventct], Zij2[ij, eventct]) 407 CumTime <- CumTime + Rij1[ij, eventct] 408 outdata.matrix[outrow, ] <- thisoutdata 409 k <- k + 1;outrow <- outrow + 1 410 }else{ 411 thisoutdata <- c(i, j, k, L(i, j, k, Zij1[ij, eventct]), 412 CumTime, ThisFollowTime, 0, 0, Zij1[ij, eventct], 413 Zij2[ij, eventct]) 414 CumTime <- ThisFollowTime 415 outdata.matrix[outrow, ] <- thisoutdata 416 k <- k + 1;outrow <- outrow + 1 417 } 418 if((CumTime + Rij2[ij, eventct]) < ThisFollowTime){ 419 thisoutdata <- c(i, j, k, L(i, j, k, Zij2[ij, eventct]), 420 CumTime, CumTime + Rij2[ij, eventct], 0, 1, 421 Zij1[ij, eventct], Zij2[ij, eventct]) 422 CumTime <- CumTime + Rij2[ij, eventct] 423 outdata.matrix[outrow, ] <- thisoutdata 424 k <- k + 1;outrow <- outrow + 1 425 }else{ 426 if(CumTime < ThisFollowTime){ 427 thisoutdata <- c(i, j, k, L(i, j, k, Zij2[ij, eventct]), 428 CumTime, ThisFollowTime, 0, 0, Zij1[ij, eventct], 429 Zij2[ij, eventct]) 430 CumTime <- ThisFollowTime 431 outdata.matrix[outrow, ] <- thisoutdata 432 k <- k + 1;outrow <- outrow + 1 433 } 434 } 435 eventct <- eventct + 1 436 } 437 } 438 } 439 440 } 441 outdata <- as.data.frame(outdata.matrix[1:(outrow - 1), ]) 442 colnames(outdata) <- c("i", "j", "k", "r", "start", "stop", 443 "delta", "Delta", "Z1", "Z2") 444 return(outdata) 445 }