library(contsurvplot)
library(riskRegression)
library(survival)
library(ggplot2)
library(splines)
# using data from the survival package
data(nafld, package="survival")
# take a random sample to keep example fast
set.seed(42)
nafld1 <- nafld1[sample(nrow(nafld1), 150), ]
# fit cox-model with age
model <- coxph(Surv(futime, status) ~ age, data=nafld1, x=TRUE)
# plot effect of age on the median survival time
plot_surv_quantiles(time="futime",
status="status",
variable="age",
data=nafld1,
model=model)
# plot multiple survival time quantiles
plot_surv_quantiles(time="futime",
status="status",
variable="age",
data=nafld1,
model=model,
p=c(0.1, 0.25, 0.5, 0.75, 0.9))
## showing non-linear effects
# fit cox-model with bmi modelled using B-Splines,
# adjusting for age and sex
model2 <- coxph(Surv(futime, status) ~ age + male + bs(bmi, df=3),
data=nafld1, x=TRUE)
# plot effect of bmi on survival
plot_surv_quantiles(time="futime",
status="status",
variable="bmi",
data=nafld1,
model=model2,
p=c(0.1, 0.25, 0.5, 0.75, 0.9))
Run the code above in your browser using DataLab