I want to simulate survival data with a sample size of N=100, which follows the Weibull distribution with proportional hazards and constant baseline hazard. Two correlated covariates, which follow different distributions and different censoring rates (e.g. .1, .3, .5) should be integrated. I took the R code from here and varied it a bit, but I only get errors.
R code:
library(survival)
set.seed(123)
simulWeib <- function(N, lambda, rho, beta)
{
correlated covariates X and Y
X <- rexp(N)
Y <- 2*X + rnorm(N)
Weibull latent event times
v <- runif(n=N)
Tlat <- (- log(v) / (lambda * exp(X * beta)))^(1 / rho)
censoring times
C <- rnorm(n=20, mean = 0, sd = 1)
follow-up times and event indicators
time <- pmin(Tlat, C)
status <- as.numeric(Tlat <= C)
data set
data.frame(id=1:N,
time=time,
status=status,
x=X,
y=Y)
}
fit = coxph(Surv(time, status) ~ x + y, data = dat)
What did I do wrong? A further question is how to divide the censoring into right, left and interval censoring.
This post is related to How to create a toy survival (time to event) data with right censoring.
Ygo? – Jarle Tufto May 05 '21 at 14:29Yis defined asY <- 2*X + rnorm(N)or what do you mean in detail? – Tino May 05 '21 at 15:19