I am using the survfit
function in the R package survival
to create survival curves from a survfit.coxph
object output by 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.
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