Drug-relapse of patients with time-varying covariates. This data set is simulated.

1 |

A data frame with 300 observations on the following 4 variables.

`event`

a numeric vector

`delta`

a logical vector

`gender`

a numeric vector

`inter`

a numeric vector

This data is simulated under the following pretense. Patient records were obtained for 150 days after they joined a rehabilitation program. The event of interest was drug-relapse and two covariates were recorded. The `event`

variable describes the observed or censored time; the `delta`

variable describes whether the time denotes an observed relapse (`TRUE`

) or a censored time; the `gender`

variable is a time-independent covariate; and `inter`

is a time-dependent covariate indicating whether the patient had was (randomly) assigned a second intervention: working 10 hours a week for a nonprofit. Each of these special interventions were assigned *after* the patients entered the clinic, meaning the intervention covariate changes for those patients who had an intervention before relapse.

Simulated (David M Diez)

Fox J (2002). "Cox Proportional-Hazards Regression for Survival Data. Appendix to An R and S-PLUS Companion to Applied Regression." Comprehensive R Archive Network. http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-cox-regression.pdf

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ```
#=====> 10. Cox PH model, time-dependent covariates <=====#
data(relapse)
relapse
attach(relapse)
N <- dim(relapse)[1]
t1 <- rep(0, N+sum(!is.na(inter))) # Initialize start times at 0
t2 <- rep(NA, length(t1)) # The end times for each record
e <- rep(NA, length(t1)) # Was the event censored?
g <- rep(NA, length(t1)) # Gender
PI <- rep(FALSE, length(t1)) # Initialize intervention at FALSE
R <- 1 # Row of new record
for(ii in 1:dim(relapse)[1]){
if(is.na(inter[ii])){ # no intervention, copy survival record
t2[R] <- event[ii]
e[R] <- delta[ii]
g[R] <- gender[ii]
R <- R+1
} else { # intervention, split records
g[R+0:1] <- gender[ii] # gender is same for each time
e[R] <- 0 # no relapse observed pre-intervention
e[R+1] <- delta[ii] # relapse occur post-intervention?
PI[R+1] <- TRUE # Intervention covariate, post-intervention
t2[R] <- inter[ii]-1 # End of pre-intervention
t1[R+1] <- inter[ii]-1 # Start of post-intervention
t2[R+1] <- event[ii] # End of post-intervention
R <- R+2 # Two records added
}
}
mySurv <- Surv(t1, t2, e)
coxphFit <- coxph(mySurv ~ g + PI)
detach(relapse)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.