I wish to create a toy survival (time to event) data which is right censored and follows some distribution with proportional hazards and constant baseline hazard.
I created the data as follows, but I am unable to obtain estimated hazard ratios that are close to the true values after fitting a Cox proportional hazards model to the simulated data.
What did I do wrong?
R codes:
library(survival)
#set parameters
set.seed(1234)
n = 40000 #sample size
#functional relationship
lambda=0.000020 #constant baseline hazard 2 per 100000 per 1 unit time
b_haz <-function(t) #baseline hazard
{
lambda #constant hazard wrt time
}
x = cbind(hba1c=rnorm(n,2,.5)-2,age=rnorm(n,40,5)-40,duration=rnorm(n,10,2)-10)
B = c(1.1,1.2,1.3) # hazard ratios (model coefficients)
hist(x %*% B) #distribution of scores
haz <-function(t) #hazard function
{
b_haz(t) * exp(x %*% B)
}
c_hf <-function(t) #cumulative hazards function
{
exp(x %*% B) * lambda * t
}
S <- function(t) #survival function
{
exp(-c_hf(t))
}
S(.005)
S(1)
S(5)
#simulate censoring
time = rnorm(n,10,2)
S_prob = S(time)
#simulate events
event = ifelse(runif(1)>S_prob,1,0)
#model fit
km = survfit(Surv(time,event)~1,data=data.frame(x))
plot(km) #kaplan-meier plot
#Cox PH model
fit = coxph(Surv(time,event)~ hba1c+age+duration, data=data.frame(x))
summary(fit)
cox.zph(fit)
Results:
Call:
coxph(formula = Surv(time, event) ~ hba1c + age + duration, data = data.frame(x))
n= 40000, number of events= 3043
coef exp(coef) se(coef) z Pr(>|z|)
hba1c 0.236479 1.266780 0.035612 6.64 3.13e-11 ***
age 0.351304 1.420919 0.003792 92.63 < 2e-16 ***
duration 0.356629 1.428506 0.008952 39.84 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
hba1c 1.267 0.7894 1.181 1.358
age 1.421 0.7038 1.410 1.432
duration 1.429 0.7000 1.404 1.454
Concordance= 0.964 (se = 0.006 )
Rsquare= 0.239 (max possible= 0.767 )
Likelihood ratio test= 10926 on 3 df, p=0
Wald test = 10568 on 3 df, p=0
Score (logrank) test = 11041 on 3 df, p=0
but true values are set as
B = c(1.1,1.2,1.3) # hazard ratios (model coefficients)