Joh*_*vat 5 r survival-analysis
我所拥有的是使用R进行机械性心脏支持的患者的Kaplan-Meier分析.
我需要的是将以下数据添加到图中(如示例中所示):
换句话说,有两组,其中一组是另一组(所有患者)的子集(移植患者).这两条曲线必须从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)
Run Code Online (Sandbox Code Playgroud)
我该如何添加另外两条曲线?
亲切的问候约翰
样本Kaplan Meier曲线:http://i.stack.imgur.com/158e8.jpg
演示数据:
生存数据,进入泵:
Run Code Online (Sandbox Code Playgroud)TIME CENSUS DEVICE 426 1 1 349 1 1 558 1 1 402 1 1 12 0 1 84 0 1 308 1 1 174 1 1 315 1 1 734 1 1 544 1 2 1433 1 2 1422 1 2 262 1 2 318 1 2 288 1 2 1000 1 2
TX数据:
Run Code Online (Sandbox Code Playgroud)TIME CENSUS DEVICE 426 1 1 288 1 2 308 1 1
死亡人数:
Run Code Online (Sandbox Code Playgroud)TIME CENSUS DEVICE 12 0 1 84 0 1
有了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)
Run Code Online (Sandbox Code Playgroud)

不需要plot.new()(虽然这是该范例的一个很好的插图).这可以通过lines()课堂方法实现"surfit".
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")
Run Code Online (Sandbox Code Playgroud)
这给出了使用@ Backlin的示例数据(但不同的种子,因此不同的数据)

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