Cumulative Hazard
Cox Model
cox.fit <- coxph(Surv(time, status=="1") ~ cardiovasc + tabac2 +
dial + apkd01 +
bmic + sex + age + diabetes, data = rdb)
survfit(), in addition to the survival function, also computes the cumulative baseline hazard function.
SF <- survfit(cox.fit)
rdb$cumhaz <- NA
for(i in 1:nrow(rdb)) {
if (!is.na(rdb$time[i])) {
rdb$cumhaz[i] <- summary(SF, times = rdb$time[i])$cumhaz
}
}
rdb.for.imp <- rdb %>%
select(cumhaz, cardiovasc, tabac2, dial, apkd01,
sex, age, diabetes, bmic, status)
summary(rdb.for.imp)
Data imputation (mice)
imp.rdb <- mice(rdb.for.imp,
seed = 21051986,
m = 20, #nimpute(rdb.for.imp),
maxit = 10,
meth = "pmm",
print = F)
Transform imputed datasets in their long form
imp.rdb.dat <- complete(imp.rdb, "long", include = TRUE)
Repeat time variable m + 1 times since impdat
This step includes the original data as well as m imputations
imp.rdb.dat$time <- rep(rdb$time, imp.rdb$m + 1)
Replace missing time values with time corresponding to the imputed cumulative hazard value
Create a data frame with the unique event times and corresponding cumulative hazards from the complete case analysis.
SUB <- imp.rdb.dat$.imp > 0 & is.na(imp.rdb.dat$time)
if(sum(SUB) > 0) {
bhz <- data.frame(time = survfit(cox.fit)$time,
cumhaz = survfit(cox.fit)$cumhaz)
# Only under pmm (default)
for(i in 1:sum(SUB)) {
# Use max since last 2 times have the same cumhaz
imp.rdb.dat$time[SUB][i] <-
max(bhz$time[bhz$cumhaz == imp.rdb.dat$cumhaz[SUB][i]])
}
}
Convert back to a mids object
Here the new dataset
imp.rdb.new <- as.mids(imp.rdb.dat)