library(adjustedCurves)
library(survival)
if (requireNamespace("riskRegression") & requireNamespace("prodlim")) {
library(riskRegression)
library(prodlim)
set.seed(42)
# simulate some data as example
sim_dat <- sim_confounded_crisk(n=50, max_t=1.2)
sim_dat$group <- as.factor(sim_dat$group)
# estimate a cause-specific cox-regression for the outcome
cox_mod <- CSC(Hist(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group,
data=sim_dat)
# use it to calculate adjusted CIFs
adjcif <- adjustedcif(data=sim_dat,
variable="group",
ev_time="time",
event="event",
cause=1,
method="direct",
outcome_model=cox_mod,
conf_int=FALSE)
# plot the curves
plot(adjcif)
# estimate a Fine & Gray model for the outcome instead
fgr_mod <- FGR(Hist(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group,
data=sim_dat, cause=1)
# use it to calculate adjusted CIFs
adjcif <- adjustedcif(data=sim_dat,
variable="group",
ev_time="time",
event="event",
cause=1,
method="direct",
outcome_model=fgr_mod,
conf_int=FALSE)
# plot the curves
plot(adjcif)
}
# not run because it would be too slow
# \donttest{
## using multiple imputation
if (requireNamespace("riskRegression") & requireNamespace("prodlim") &
requireNamespace("mice")) {
library(mice)
# introduce random missingness in x1 as example
# NOTE: This is only done as an example, in reality you would
# already have missing data, not introduce it yourself.
sim_dat$x1 <- ifelse(runif(n=50) < 0.5, sim_dat$x1, NA)
# perform multiple imputation
mids <- mice::mice(data=sim_dat, method="pmm", m=5, printFlag=FALSE)
# fit model for each imputed dataset, using the CSC_MI helper function
mira <- CSC_MI(mids, Hist(time, event) ~ x1 + x2 + x3 + x4 + x5 + x6 + group)
# calculate adjusted CIFs on imputed data
adj <- adjustedcif(data=mids,
variable="group",
ev_time="time",
event="event",
method="direct",
cause=1,
outcome_model=mira)
plot(adj)
}
# }
Run the code above in your browser using DataLab