英文:
Is there a way to use ggplot2 to plot survival curves from a svycoxph object?
问题
我使用了调查包中的svycoxph来创建一个对象:
object <- svycoxph(Surv(time, status) ~ group, design=design)
然后,我使用了第88页上文档中的predict()函数来预测曲线:
prediction <- predict(object, type="curve", newdata = data.frame(group=c("A", "B")) )
是否有一种方法可以使用ggplot2功能来绘制从预测中获得的生存曲线?我尝试过使用同一第88页上文档中记录的基本绘图功能,但我无法区分哪条线是A,哪条线是B,并且无法使其与我使用ggsurvfit()创建的绘图模板匹配。
我尝试过另一种方法,从预测对象中提取数据并使用ggplot(),但我无法确定我是否正确地标记了A和B线:
prediction.data <- data.frame(time=prediction[["1"]][["time"]],
A_surv=prediction[["1"]][["surv"]],
B_surv=prediction[["2"]][["surv"]])
ggplot(prediction.data, aes(x=time)) +
geom_line(aes(y=A_surv), color="red") +
geom_line(aes(y=B_surv), color="blue")
英文:
I used svycoxph from the survey package to create an object:
object <- svycoxph(Surv(time, status) ~ group, design=design)
And then I used predict() as documented here on page 88 to predict curves:
prediction <- predict(object, type="curve", newdata = data.frame(group=c("A", "B")) )
Is there a way to use ggplot2 functionality to plot the survival curves from the prediction? I tried using the base plotting features as documented on the same page 88, but I can't tell which line is A and which line is B, and I can't make it match plot templates I have that were made with ggsurvfit().
One other thing I tried was extracting the data from the prediction object and using ggplot(), but I can't tell if I labeled the A and B lines correctly:
prediction.data <- data.frame(time=prediction[["1"]][["time"]],
A_surv=prediction[["1"]][["surv"]],
B_surv=prediction[["2"]][["surv"]])
ggplot(prediction.data, aes(x=time)) +
geom_line(aes(y=A_surv), color="red") +
geom_line(aes(y=B_surv), color="blue")
答案1
得分: 1
你正在走在正确的道路上。基本上,你需要将预测结果提取到一个数据框中,并将其用于绘图。
这里是一个完整的示例。首先,我们将加载包,创建一些可复现的样本数据,名称与你的示例中相同,并指定一个设计:
library(survey)
set.seed(1)
df <- data.frame(time = round(c(rexp(50, 0.01), rexp(50, 0.015))),
group = rep(c("A", "B"), each = 50))
df$status <- ifelse(df$time > 60, 0, 1)
df$time[df$time > 60] <- 60
design <- svydesign(id = ~ group, weights = 1, data = df)
现在我们创建我们的模型:
object <- svycoxph(Surv(time, status) ~ group, data = df, design = design)
现在让我们为我们的两个group
级别获取预测曲线:
preds <- predict(object, newdata = data.frame(group = c("A", "B")),
type = "curve", se = TRUE)
最棘手的部分是将曲线提取到一个数据框中:
plot_df <- do.call(rbind,
Map(function(x, y) cbind(as.data.frame(unclass(x)), group = y),
preds, c("A", "B")))
plot_df$lower <- exp(log(plot_df$surv) - 1.96 * sqrt(plot_df$varlog))
plot_df$upper <- exp(log(plot_df$surv) + 1.96 * sqrt(plot_df$varlog))
现在我们准备好绘图了:
library(ggplot2)
ggplot(plot_df, aes(time, surv, color = group)) +
geom_step(linewidth = 1) +
geom_step(aes(y = lower), linetype = 2, alpha = 0.5) +
geom_step(aes(y = upper), linetype = 2, alpha = 0.5) +
scale_color_manual(values = c("orangered", "deepskyblue4")) +
theme_minimal(base_size = 16) +
scale_y_continuous("Survival", limits = 0:1, labels = scales::percent)
创建于2023年6月15日,使用 reprex v2.0.2
英文:
You're on the right track. Essentially you need to cajole the predictions into a data frame and use this for the plot.
Here's a full reprex. First we will load the package, create some reproducible sample data with the same names as in your example, and specify a design:
library(survey)
set.seed(1)
df <- data.frame(time = round(c(rexp(50, 0.01), rexp(50, 0.015))),
group = rep(c("A", "B"), each = 50))
df$status <- ifelse(df$time > 60, 0, 1)
df$time[df$time > 60] <- 60
design <- svydesign(id = ~ group, weights = 1, data = df)
Now we create our model
object <- svycoxph(Surv(time, status) ~ group, data = df, design = design)
Now let's get prediction curves for our two levels of group
preds <- predict(object, newdata = data.frame(group = c("A", "B")),
type = "curve", se = TRUE)
The trickiest part is extracting the curves into a data frame
plot_df <- do.call(rbind,
Map(function(x, y) cbind(as.data.frame(unclass(x)), group = y),
preds, c("A", "B")))
plot_df$lower <- exp(log(plot_df$surv) - 1.96 * sqrt(plot_df$varlog))
plot_df$upper <- exp(log(plot_df$surv) + 1.96 * sqrt(plot_df$varlog))
Now we are ready to plot:
library(ggplot2)
ggplot(plot_df, aes(time, surv, color = group)) +
geom_step(linewidth = 1) +
geom_step(aes(y = lower), linetype = 2, alpha = 0.5) +
geom_step(aes(y = upper), linetype = 2, alpha = 0.5) +
scale_color_manual(values = c("orangered", "deepskyblue4")) +
theme_minimal(base_size = 16) +
scale_y_continuous("Survival", limits = 0:1, labels = scales::percent)
<sup>Created on 2023-06-15 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论