[英]survival curves produced by survfit function in package survival
I am using the survfit
function in the R package survival
to create survival curves from a survfit.coxph
object output by coxph
.我使用
survfit
函数在R包survival
创建从存活曲线survfit.coxph
通过输出对象coxph
。 I have two methods for creating the curve which give different results.我有两种创建曲线的方法,它们会给出不同的结果。 I believe the first is the correct answer, but I can't tell why method 2 does not work.
我相信第一个是正确答案,但我不知道为什么方法 2 不起作用。
library(survival)
set.seed(1234)
## generate small data set
n <- 10
z <- rnorm(n,mean=0.4)
x <- rexp(n,exp(z))
y <- pmin(1,x)
del <- 1*(x < 1)
dat <- data.frame(y,del,z)
## fit cox model
fit <- coxph(Surv(y,del)~z,ties="breslow",data=dat)
## method 1
newdata <- dat[1,]
newdata[1,3] <- 0
out <- survfit(fit,newdata=newdata)
out$surv
##[1] 0.9557533 0.9048870 0.8545721 0.7599743 0.6397022 0.4218647 0.4218647
## method 2, why not same as method 1?
dat[1,3] <- 0
out <- survfit(fit,newdata=dat[1,])
out$surv
##[1] 0.9570757 0.9079589 0.8593546 0.7710287 0.6610956 0.4787354 0.4787354
In both methods survfit
function receives two parameters: fit
and newdata
.在这两种方法中
survfit
函数接收两个参数: fit
和newdata
。
In the method 1 line newdata[1,3] <- 0
changes only the object newdata
and the object dat
and consequently object fit
are not changed.在方法 1 行
newdata[1,3] <- 0
仅更改对象newdata
和对象dat
,因此对象fit
不会更改。
In the method 2, instead, dat[1,3] <- 0
changes both, the object newdata
and the object fit
.相反,在方法 2 中,
dat[1,3] <- 0
会同时更改对象newdata
和对象fit
。
So the newdata
objects received by survfit
function are identical in both methods, as 42 correctly pointed out, but the fit
objects are not.因此
survfit
函数接收到的newdata
对象在两种方法中都是相同的,正如 42 正确指出的那样,但fit
对象不是。
If you make 3 identical dataframes in the beginning, you can see this.如果您一开始制作了 3 个相同的数据帧,您可以看到这一点。
dat1 <- data.frame(y,del,z)
dat2 <- data.frame(y,del,z)
dat3 <- data.frame(y,del,z)
## fit cox model
fit <- coxph(Surv(y,del)~z,ties="breslow",data=dat1)
## method 1
newdata <- dat2[1,]
newdata[1,3] <- 0
out <- survfit(fit,newdata=newdata)
out$surv
##[1] 0.9557533 0.9048870 0.8545721 0.7599743 0.6397022 0.4218647 0.4218647
## method 2, same as method 1
dat3[1,3] <- 0
out <- survfit(fit,newdata=dat3[1,])
out$surv
##[1] 0.9557533 0.9048870 0.8545721 0.7599743 0.6397022 0.4218647 0.4218647
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.