library(adjustedCurves)
library(survival)
set.seed(42)
# simulate some example data
sim_dat <- sim_confounded_surv(n=50, max_t=1.2)
sim_dat$group <- as.factor(sim_dat$group)
# treatment assignment model
glm_mod <- glm(group ~ x2 + x3 + x5 + x6, data=sim_dat, family="binomial")
# outcome model
cox_mod <- coxph(Surv(time, event) ~ x1 + x2 + x4 + x5 + group,
data=sim_dat, x=TRUE)
if (requireNamespace("riskRegression")) {
# using direct adjustment with asymptotic confidence intervals
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="direct",
outcome_model=cox_mod,
conf_int=TRUE,
bootstrap=FALSE)
# using IPTW Kaplan-Meier with asymptotic confidence intervals
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="iptw_km",
treatment_model=glm_mod,
conf_int=TRUE,
bootstrap=FALSE)
# using AIPTW with asymptotic confidence intervals
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="aiptw",
outcome_model=cox_mod,
treatment_model=glm_mod,
conf_int=TRUE,
bootstrap=FALSE)
# using direct adjustment at custom points in time
custom_times <- c(0.001, 0.1, 0.2, 0.6, 1.1)
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="direct",
outcome_model=cox_mod,
conf_int=TRUE,
bootstrap=FALSE,
times=custom_times)
# using bootstrapping with direct adjustment
# NOTE: n_boot should be much higher than 10 in reality, only used
# here as a fast example
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="direct",
outcome_model=cox_mod,
conf_int=TRUE,
bootstrap=TRUE,
n_boot=10)
}
# not run because those are too slow
# \donttest{
if (requireNamespace("riskRegression")) {
# using bootstrapping with direct adjustment, run in parallel
# on two cores
adjsurv <- adjustedsurv(data=sim_dat,
variable="group",
ev_time="time",
event="event",
method="direct",
outcome_model=cox_mod,
conf_int=TRUE,
bootstrap=TRUE,
n_boot=4,
n_cores=2)
}
# using multiple imputation
if (requireNamespace("mice") & requireNamespace("WeightIt")) {
library(mice)
library(WeightIt)
# simulate some data as example
sim_dat <- sim_confounded_surv(n=50, max_t=1.2)
sim_dat$group <- as.factor(sim_dat$group)
# 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=2, printFlag=FALSE)
# IPTW KM using WeightIt on imputed data
adj <- adjustedsurv(data=mids,
variable="group",
ev_time="time",
event="event",
method="iptw_km",
treatment_model=group ~ x1 + x2 + x5 + x6,
weight_method="ps")
plot(adj)
}
# More specific examples can be found in the documentation of each
# respective surv_method. See ?surv_ + "method" for more information.
# }
Run the code above in your browser using DataLab