I have a dataset with a time-varying continuous exposure and a time-varying three-level categorical variable. I want to plot the hazard ratios obtained from the cubic spline term of exposure and its interaction with the categorical variable for each level of the categorical variable. I would like to use the rms package in R. How can I do this?
is my approach is correct
library(survival)
library(rms)
# Fit Cox proportional hazards model
model <- cph(Surv(time_start, time_end, composite_mace) ~ rcs(exposure,3) * category,
data =data, x=TRUE, y=TRUE)
p <- Predict(model, exposure, risk_cat, ref.zero = TRUE, fun = exp)
# Plot hazard ratios
ggplot(p, aes(x = exposure, y = label, ymin = lower, ymax = upper)) +
geom_pointrange(position = position_dodge(width = 0.8)) +
facet_wrap(~ category, nrow = 1) +
xlab("Exposure") +
ylab("Hazard ratio") +
ggtitle("Hazard ratios of exposure and interaction with categorical variable")
Moreover, do i really need a reference category for the categorical variable predictor?
I got results but I am not sure if my approach is correct