library(dplyr)
# the eventual survival time
shilong1 <- shilong %>%
arrange(bras.f, id, tstop) %>%
group_by(bras.f, id) %>%
slice(n()) %>%
select(-c("ps", "ttc", "tran"))
# the last value of time-dependent covariates before pd
shilong2 <- shilong %>%
filter(pd == 0 | tstart <= dpd) %>%
arrange(bras.f, id, tstop) %>%
group_by(bras.f, id) %>%
slice(n()) %>%
select(bras.f, id, ps, ttc, tran)
# combine baseline and time-dependent covariates
shilong3 <- shilong1 %>%
left_join(shilong2, by = c("bras.f", "id"))
# apply the two-stage method
fit1 <- tsesimp(
data = shilong3, id = "id", time = "tstop", event = "event",
treat = "bras.f", censor_time = "dcut", pd = "pd",
pd_time = "dpd", swtrt = "co", swtrt_time = "dco",
base_cov = c("agerand", "sex.f", "tt_Lnum", "rmh_alea.c",
"pathway.f"),
base2_cov = c("agerand", "sex.f", "tt_Lnum", "rmh_alea.c",
"pathway.f", "ps", "ttc", "tran"),
aft_dist = "weibull", alpha = 0.05,
recensor = TRUE, swtrt_control_only = FALSE, offset = 1,
boot = FALSE)
c(fit1$hr, fit1$hr_CI)
Run the code above in your browser using DataLab