library(adjustedCurves)
library(survival)
set.seed(42)
## This example has been taken from the online supplement of the original
## article by Martinez-Camblor et al. (2021)
# generate some data
n <- 1000
t <- seq(0, 10, 0.01)
bu <- log(2)
hr <- 2
v <- 2
a <- 1
U <- stats::rnorm(n)
Z <- stats::rnorm(n)
W <- stats::rnorm(n)
e <- stats::rnorm(n)
X0 <- (U + Z + a*W + (v - a^2)^0.5*e >= 0)
L0 <- Z + bu*U
L1 <- log(hr) + Z + bu*U
T <- stats::rexp(n, 0.005)
T0 <- T/exp(L0)
T1 <- T/exp(L1)
censor <- stats::rexp(n, 0.05)
time1 <- pmin(ifelse(X0==1,T1,T0), censor)
status1 <- 1-(censor==time1)
time <- pmin(time1, 10)
status <- ifelse(time1 > 10, 0, status1)
dt <- as.data.frame(cbind(time, status, X0, Z, W))
dt$X0 <- factor(dt$X0)
# calculate adjusted survival curves
adjsurv <- adjustedsurv(data=dt,
variable="X0",
ev_time="time",
event="status",
method="iv_2SRIF",
adjust_vars="Z",
instrument="W")
# plot the curves
plot(adjsurv)
Run the code above in your browser using DataLab