# Solved – How to generate survival data with time dependent covariates using R

I want to generate survival time from a Cox proportional hazards model that contains time dependent covariate. The model is

\$h(t|X_i) =h_0(t) exp(gamma X_i + alpha m_{i}(t))\$

where \$X_i\$ is generated from Binomial(1,0.5) and \$m_{i}(t)=beta_0 + beta_1 X_{i} + beta_2 X_{i} t\$.

The true parameter values are used as \$gamma = 1.5, beta_0 = 0, beta_1 = -1, beta_2 = -1.5, h_0(t) = 1\$

For time-independent covariate (i.e. \$h(t|X_i) =h_0(t) exp(gamma X_i) \$ I generated as follows

``#For time independent case # h_0(t) = 1 gamma <- -1 u <- runif(n=100,min=0,max=1) Xi <- rbinom(n=100,size=1,prob=0.5) T <- -log(u)/exp(gamma*Xi) ``

Contents

OK from your R code you are assuming an exponential distribution (constant hazard) for your baseline hazard. Your hazard functions are therefore:

\$\$ hleft(t mid X_iright) = begin{cases} exp{left(alpha beta_0right)} & text{if \$X_i = 0\$,} \ exp{left(gamma + alphaleft(beta_0+beta_1+beta_2 tright)right)} & text{if \$X_i = 1\$.} end{cases} \$\$

We then integrate these with respect to \$t\$ to get the cumulative hazard function:

\$\$ begin{align} Lambdaleft(tmid X_iright) &= begin{cases} t exp{left(alpha beta_0right)} & text{if \$X_i=0\$,} \ int_0^t{exp{left(gamma + alphaleft(beta_0+beta_1+beta_2 tauright)right)} ,dtau} & text{if \$X_i=1\$.} end{cases} \ &= begin{cases} t exp{left(alpha beta_0right)} & text{if \$X_i=0\$,} \ exp{left(gamma + alphaleft(beta_0+beta_1right)right)} frac{1}{alphabeta_2} left(expleft(alphabeta_2 tright)-1right) & text{if \$X_i=1\$.} end{cases} end{align} \$\$

These then give us the survival functions:

\$\$ begin{align} Sleft(tright) &= exp{left(-Lambdaleft(tright)right)} \ &= begin{cases} exp{left(-t exp{left(alpha beta_0right)}right)} & text{if \$X_i=0\$,} \ exp{left(-exp{left(gamma + alphaleft(beta_0+beta_1right)right)} frac{1}{alphabeta_2} left(expleft(alphabeta_2 tright)-1right)right)} & text{if \$X_i=1\$.} end{cases} end{align} \$\$

You then generate by sampling \$X_i\$ and \$Usimmathrm{Uniformleft(0,1right)}\$, substituting \$U\$ for \$Sleft(tright)\$ and rearranging the appropriate formula (based on \$X_i\$) to simulate \$t\$. This should be straightforward algebra you can then code up in R but please let me know by comment if you need any further help.

Rate this post

# Solved – How to generate survival data with time dependent covariates using R

I want to generate survival time from a Cox proportional hazards model that contains time dependent covariate. The model is

\$h(t|X_i) =h_0(t) exp(gamma X_i + alpha m_{i}(t))\$

where \$X_i\$ is generated from Binomial(1,0.5) and \$m_{i}(t)=beta_0 + beta_1 X_{i} + beta_2 X_{i} t\$.

The true parameter values are used as \$gamma = 1.5, beta_0 = 0, beta_1 = -1, beta_2 = -1.5, h_0(t) = 1\$

For time-independent covariate (i.e. \$h(t|X_i) =h_0(t) exp(gamma X_i) \$ I generated as follows

``#For time independent case # h_0(t) = 1 gamma <- -1 u <- runif(n=100,min=0,max=1) Xi <- rbinom(n=100,size=1,prob=0.5) T <- -log(u)/exp(gamma*Xi) ``

OK from your R code you are assuming an exponential distribution (constant hazard) for your baseline hazard. Your hazard functions are therefore:

\$\$ hleft(t mid X_iright) = begin{cases} exp{left(alpha beta_0right)} & text{if \$X_i = 0\$,} \ exp{left(gamma + alphaleft(beta_0+beta_1+beta_2 tright)right)} & text{if \$X_i = 1\$.} end{cases} \$\$

We then integrate these with respect to \$t\$ to get the cumulative hazard function:

\$\$ begin{align} Lambdaleft(tmid X_iright) &= begin{cases} t exp{left(alpha beta_0right)} & text{if \$X_i=0\$,} \ int_0^t{exp{left(gamma + alphaleft(beta_0+beta_1+beta_2 tauright)right)} ,dtau} & text{if \$X_i=1\$.} end{cases} \ &= begin{cases} t exp{left(alpha beta_0right)} & text{if \$X_i=0\$,} \ exp{left(gamma + alphaleft(beta_0+beta_1right)right)} frac{1}{alphabeta_2} left(expleft(alphabeta_2 tright)-1right) & text{if \$X_i=1\$.} end{cases} end{align} \$\$

These then give us the survival functions:

\$\$ begin{align} Sleft(tright) &= exp{left(-Lambdaleft(tright)right)} \ &= begin{cases} exp{left(-t exp{left(alpha beta_0right)}right)} & text{if \$X_i=0\$,} \ exp{left(-exp{left(gamma + alphaleft(beta_0+beta_1right)right)} frac{1}{alphabeta_2} left(expleft(alphabeta_2 tright)-1right)right)} & text{if \$X_i=1\$.} end{cases} end{align} \$\$

You then generate by sampling \$X_i\$ and \$Usimmathrm{Uniformleft(0,1right)}\$, substituting \$U\$ for \$Sleft(tright)\$ and rearranging the appropriate formula (based on \$X_i\$) to simulate \$t\$. This should be straightforward algebra you can then code up in R but please let me know by comment if you need any further help.

Rate this post