# NOT RUN {
# we construct the composite event indicator (transplantation or death)
pbc2$status2 <- as.numeric(pbc2$status != "alive")
pbc2.id$status2 <- as.numeric(pbc2.id$status != "alive")
# we fit the joint model using splines for the subject-specific
# longitudinal trajectories and a spline-approximated baseline
# risk function
lmeFit <- lme(log(serBilir) ~ ns(year, 2), data = pbc2,
random = ~ ns(year, 2) | id)
survFit <- coxph(Surv(years, status2) ~ drug, data = pbc2.id, x = TRUE)
jointFit <- jointModelBayes(lmeFit, survFit, timeVar = "year")
# we will compute survival probabilities for Subject 2 in a dynamic manner,
# i.e., after each longitudinal measurement is recorded
ND <- pbc2[pbc2$id == 2, ] # the data of Subject 2
survPreds <- vector("list", nrow(ND))
for (i in 1:nrow(ND)) {
survPreds[[i]] <- survfitJM(jointFit, newdata = ND[1:i, ])
}
survPreds
###########################################################################
# Predictions from multivariate models
pbc2 <- pbc2[!is.na(pbc2$serChol), ]
pbc2.id <- pbc2[!duplicated(pbc2$id), ]
pbc2.id$Time <- pbc2.id$years
pbc2.id$event <- as.numeric(pbc2.id$status != "alive")
# Fit a trivariate joint model
MixedModelFit <- mvglmer(list(log(serBilir) ~ year + (year | id),
sqrt(serChol) ~ year + (year | id),
hepatomegaly ~ year + (year | id)), data = pbc2,
families = list(gaussian, gaussian, binomial), engine = "STAN")
CoxFit <- coxph(Surv(Time, event) ~ drug + age, data = pbc2.id, model = TRUE)
JMFit <- mvJointModelBayes(MixedModelFit, CoxFit, timeVar = "year")
# We want survival probabilities for three subjects
ND <- pbc2[pbc2$id %in% c(2, 25, 81), ]
sprobs <- survfitJM(JMFit, ND)
sprobs
# Basic plot
plot(sprobs)
# split in a 2 rows 2 columns and include the survival function in
# a separate panel; plot only the third & first subjects; change various defaults
plot(sprobs, split = c(3, 2), surv_in_all = FALSE, which_subjects = c(3, 1),
lty_lines_CI = 3, col_lines = "blue", col_fill_CI = "red",
col_points = "pink", pch_points = 12)
###########################################################################
# run Shiny app
runDynPred()
# }
Run the code above in your browser using DataLab