如何将 summary() 提取到适用于 ggplot() 中数据可视化的数据框?

How to extract summary() to a data frame applicable for data visualization in ggplot()?

我在 competing risks 的情况下进行生存分析。我使用 prodlim-package,我觉得它很有用。但是,我不喜欢内置图形,而是想应用 ggplot

问题: 如何提取 prodlim summary()- 输出并将其加载到 ggplot2 可访问的数据框中?

也许可以编写一个函数来做到这一点?我之前在 Whosebug 上收到过关于将 summary() 输出加载到 dataframe 中的 ,但使用的包与 prodlim 不同。

library(prodlim)
library(riskRegression)

# Build-in data
data(Melanoma)

# Simple competing risk analysis
fit.aj <- prodlim(Hist(time,status)~age+sex,data=Melanoma)

# Defining the event of interest as cause=1
summary(fit.aj,conf.int=FALSE,newdata=data.frame(age=50,sex="Male"),cause=1)

产生

> summary(fit.aj,conf.int=FALSE,newdata=data.frame(age=50,sex="Male"),cause=1)

----------> Cause:  1 

age=50, sex=Male :
  time n.risk n.event n.lost cuminc se.cuminc lower upper
1   10     33       0      0  0.000    0.0000 0.000 0.000
2 1513     22       0      0  0.282    0.0795 0.126 0.437
3 2006     15       0      0  0.282    0.0795 0.126 0.437
4 3042     10       0      0  0.396    0.0998 0.201 0.592
5 5565      0       0      0     NA        NA    NA    NA

摘要很容易画出来;但是,图形不是我的风格。

plot(fit.aj,conf.int=FALSE,newdata=data.frame(age=50,sex="Male"),cause=1)

我想将 summary() 转换成 dataframe(我们称之为 df),它可以包含在 ggplot() 中。

基于 summary() 中的 names,我正在尝试实现如下目标:

ggplot(df, aes(x=time)) +
geom_line(aes(y=cuminc) +
geom_ribbon(aes(ymin = lower, ymax = upper)

感谢您的建议。

您可以访问结果列表,直到找到绘图的 table 级别并将其另存为 data.frame。您可以使用 str 检查列表的结构。

summ_list <- summary(fit.aj,conf.int=FALSE,newdata=data.frame(age=50,sex="Male"),cause=1)
df<-as.data.frame(summ_list $table$`1`$`age=50, sex=Male`)

#The desired plot with ggplot
ggplot(df, aes(x=time)) +
  geom_line(aes(y=cuminc)) +
  geom_ribbon(aes(ymin = lower, 
                              ymax = upper))