Kaplan Meier,包括存活和移植数据

Kaplan Meier,包括存活和移植数据,r,survival-analysis,R,Survival Analysis,我用R 我需要的是将以下数据添加到绘图中(如示例中所示): 因心脏移植(HTX)而存活的患者 死亡病人 换句话说,有两组,其中一组是另一组(所有患者)的子集(移植患者)。这两条曲线必须从0/0开始并将增加 我自己的情节是通过以下方式完成的: pump <- read.table(file=datafile, header=FALSE, col.names=c('TIME', 'CENSUS', 'DEVICE')) # convert days t

我用R

我需要的是将以下数据添加到绘图中(如示例中所示):

  • 因心脏移植(HTX)而存活的患者
  • 死亡病人
换句话说,有两组,其中一组是另一组(所有患者)的子集(移植患者)。这两条曲线必须从0/0开始并将增加

我自己的情节是通过以下方式完成的:

pump <- read.table(file=datafile, header=FALSE,
                   col.names=c('TIME', 'CENSUS', 'DEVICE'))
# convert days to months
pump$TIME <- pump$TIME/(730/24)
mfit.overall <- survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump)
plot(mfit.overall, xlab="months on device", ylab="cum. survival", xaxt="n")
axis(1, at=seq(from=0, to=24, by=6), las=0)
发送数据:

死亡人数:

使用
par(new=TRUE)
可以在与第一个图形相同的图形中绘制第二个图形

通常,我建议使用
lines()
将曲线添加到绘图中,因为
par(new=TRUE)
执行覆盖绘图的不同任务。当以不打算使用的方式使用函数时,您可能会犯错误,例如,我几乎忘记了重要的
xlim
参数。然而,从
survfit
对象中提取曲线并不是一件小事,因此我认为这是两个缺点中较小的一个

# Fake data for the plots
pump <- data.frame(TIME=rweibull(40, 2, 20),
                   CENSUS=runif(40) < .3,
                   DEVICE=rep(0:1, c(20,20)))
# load package
library("survival")

# Fit models
mfit.overall <-survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump)
mfit.htx <- survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump, subset=DEVICE==1)

# Plot
plot(mfit.overall, col=1, xlim=range(pump$TIME), fun=function(x) 1-x)
# `xlim` makes sure the x-axis is the same in both plots
# `fun` flips the curve to start at 0 and increase
par(new=TRUE)
plot(mfit.htx, col=2, xlim=range(pump$TIME), fun=function(x) 1-x,
    ann=FALSE, axes=FALSE, bty="n") # This hides the annotations of the 2nd plot
legend("topright", c("All", "HTX"), col=1:2, lwd=1)
#曲线图的假数据
泵不需要
plot.new()
(尽管这是一个很好的范例)。这一切都可以通过
类的
lines()
方法实现

使用@Backlin的示例数据(但是不同的种子,因此不同的数据)可以给出


lines()
进行两次调用的原因是为了安排用虚线绘制置信区间,我看不到在单个
lines()
调用中将多个
lty
传递到
lines()
(有效!)的方法。

您能在同一表单上提供用于绘图或虚拟数据的数据吗,如果您无法共享实际数据?老实说,尽管
par(new=TRUE)
是一种有用的黑客手段,但我认为@Gavin有更好的答案。如果我知道的话,我也会用他的方式解决的。巴克林,谢谢!是的,我知道。。。关键是如何从0,0(左下角)插入数据,这将增加到右上角…Backlin,完美!这管用!诀窍是:fun=function(x)1-x这里有一个
lines()
方法,所以不需要从survfit对象中删除曲线。非常感谢!我不知道有一个
行.survfit
函数,但这是一个比
par(new=TRUE)
更好的解决方案。
TIME    CENSUS  DEVICE
426     1        1
288     1        2
308     1        1
TIME    CENSUS  DEVICE
12      0        1
84      0        1
# Fake data for the plots
pump <- data.frame(TIME=rweibull(40, 2, 20),
                   CENSUS=runif(40) < .3,
                   DEVICE=rep(0:1, c(20,20)))
# load package
library("survival")

# Fit models
mfit.overall <-survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump)
mfit.htx <- survfit(Surv(TIME, CENSUS==0) ~ 1, data=pump, subset=DEVICE==1)

# Plot
plot(mfit.overall, col=1, xlim=range(pump$TIME), fun=function(x) 1-x)
# `xlim` makes sure the x-axis is the same in both plots
# `fun` flips the curve to start at 0 and increase
par(new=TRUE)
plot(mfit.htx, col=2, xlim=range(pump$TIME), fun=function(x) 1-x,
    ann=FALSE, axes=FALSE, bty="n") # This hides the annotations of the 2nd plot
legend("topright", c("All", "HTX"), col=1:2, lwd=1)
plot(mfit.overall, col=1, xlim=range(pump$TIME), fun=function(x) 1-x)
lines(mfit.htx, col=2, fun=function(x) 1-x)
lines(mfit.htx, col=2, fun=function(x) 1-x, lty = "dashed", conf.int = "only")
legend("topleft", c("All", "HTX"), col=1:2, lwd=1, bty = "n")