有没有办法使用ggplot2从svycoxph对象绘制生存曲线?

huangapple go评论82阅读模式
英文:

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 &lt;- svycoxph(Surv(time, status) ~ group, design=design)

And then I used predict() as documented here on page 88 to predict curves:

prediction &lt;- predict(object, type=&quot;curve&quot;, newdata = data.frame(group=c(&quot;A&quot;, &quot;B&quot;)) )

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 &lt;- data.frame(time=prediction[[&quot;1&quot;]][[&quot;time&quot;]], 
                              A_surv=prediction[[&quot;1&quot;]][[&quot;surv&quot;]], 
                              B_surv=prediction[[&quot;2&quot;]][[&quot;surv&quot;]])

ggplot(prediction.data, aes(x=time)) + 
  geom_line(aes(y=A_surv), color=&quot;red&quot;) + 
  geom_line(aes(y=B_surv), color=&quot;blue&quot;)

答案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) 

有没有办法使用ggplot2从svycoxph对象绘制生存曲线?

创建于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 &lt;- data.frame(time = round(c(rexp(50, 0.01), rexp(50, 0.015))),
                 group  = rep(c(&quot;A&quot;, &quot;B&quot;), each = 50))
df$status &lt;- ifelse(df$time &gt; 60, 0, 1)
df$time[df$time &gt; 60] &lt;- 60

design &lt;- svydesign(id = ~ group, weights = 1, data = df)

Now we create our model

object &lt;- svycoxph(Surv(time, status) ~ group, data = df, design = design)

Now let's get prediction curves for our two levels of group

preds &lt;- predict(object, newdata = data.frame(group = c(&quot;A&quot;, &quot;B&quot;)), 
                 type = &quot;curve&quot;, se = TRUE)

The trickiest part is extracting the curves into a data frame

plot_df &lt;- do.call(rbind, 
        Map(function(x, y) cbind(as.data.frame(unclass(x)), group = y),
            preds, c(&quot;A&quot;, &quot;B&quot;)))

plot_df$lower &lt;- exp(log(plot_df$surv) - 1.96 * sqrt(plot_df$varlog))
plot_df$upper &lt;- 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(&quot;orangered&quot;, &quot;deepskyblue4&quot;)) +
  theme_minimal(base_size = 16) +
  scale_y_continuous(&quot;Survival&quot;, limits = 0:1, labels = scales::percent) 

有没有办法使用ggplot2从svycoxph对象绘制生存曲线?

<sup>Created on 2023-06-15 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月16日 01:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定