当拟合包含连续协变量的样条项的cox模型时,我希望能够使用ggplot2生成该协变量范围内的风险比图(相对于固定参考值)。
我改编了Terry Therneau的花样小插图here(见第3页)。这种方法唯一的问题是腰部缺乏"在参考值的置信区间内,如下图所示:
以下示例生成以下图表,而不会缩小参考值的CI。
library(survival)
library(splines)
library(ggplot2)
# colon cancer death dataset
ccd <- na.omit(subset(colon, etype == 2))
# fit model with ns() term for age
cox <- coxph(Surv(time, status) ~ rx + sex + ns(age, knots = c(20, 50, 70)), data = ccd)
# get data for plot
tp <- termplot(cox, se = TRUE, plot = FALSE)
# hazard ratio plot for natural spline of age, with reference @ 50 yrs
ref <- tp$age$y[tp$age$x == 50]
ggplot() +
geom_line(data = tp$age, aes(x = x, y = exp(y - ref))) +
geom_line(data = tp$age, aes(x = x, y = exp(y - 1.96 * se - ref)), linetype = 2) +
geom_line(data = tp$age, aes(x = x, y = exp(y + 1.96 * se - ref)), linetype = 2) +
geom_hline(aes(yintercept = 1), linetype = 3) +
geom_rug(data = ccd, aes(x = age), sides = "b") +
labs(x = "Age at baseline, years",
y = "Hazard Ratio (95% CI) vs. 50 years",
title = "Mortality hazard ratio as a function of age",
subtitle = "Natural spline: knots at 20, 50, and 70 years")
我知道rms
包和smoothHR
包中有一些功能可以生成这些类型的图,但我正在寻找适合ggplot2
图形的解决方案以及coxph()
包中的survival
函数。因此,我的问题归结为:
termplot()
的输出来制作一个腰部&#34;在参考值?termplot()
,我如何通过其他方式获取相关的绘图数据? 修改1:正如第一条评论建议的那样,这可以一起使用rms
和ggplot2
来完成。例如:
library(rms)
dd <- datadist(ccd)
dd$limits$age[2] <- 50
options(datadist = "dd")
cph <- cph(Surv(time, status) ~ rx + sex + rcs(age, c(20, 50, 70)), data = ccd, x = TRUE, y = TRUE)
pdata <- Predict(cph, age, ref.zero = TRUE, fun = exp)
ggplot(data = pdata) +
geom_hline(aes(yintercept = 1), linetype = 3) +
labs(x = "Age at baseline, years",
y = "Hazard Ratio (95% CI) vs. 50 years",
title = "Mortality hazard ratio as a function of age",
subtitle = "Natural spline: knots at 20, 50, and 70 years")
这会产生一个非常接近我的情节:
但是,我仍然想知道是否有办法使用coxph()
和ns()
执行此操作。并非我对rms
包有任何反对意见,我只有一堆基于survival
功能的旧代码。