[R] How to stop Kaplan-Meier curve at a time point

Andrews, Chris chrisaa at med.umich.edu
Thu Nov 21 14:08:31 CET 2013

That subset will give you right truncation, not right censoring.  See code below.  Use Thomas's solution.


ngroup <- 100
xxx <- rep(0:1, e=ngroup)
ttt <- rexp(length(xxx), rate=xxx+.5)
plot(survfit(Surv(ttt) ~ xxx))
survdiff(Surv(ttt) ~ xxx)

# impose earlier stop time?
tstop <- 2
lines(survfit(Surv(ttt) ~ xxx, subset=ttt<tstop), col=2)
survdiff(Surv(ttt) ~ xxx, subset=ttt<tstop)

# censor at earlier stop time
ddd <- ttt<tstop
ttt2<- pmin(ttt, tstop)

lines(survfit(Surv(ttt2, ddd) ~ xxx), col=3)
survdiff(Surv(ttt2, ddd) ~ xxx)
# green lines match black lines up to tstop

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: Wednesday, November 20, 2013 5:49 PM
To: Dr.Vinay Pitchika
Cc: r-help at r-project.org
Subject: Re: [R] How to stop Kaplan-Meier curve at a time point

On Nov 20, 2013, at 12:01 PM, Dr.Vinay Pitchika wrote:

> Hello R users
> I have a question with Kaplan-Meier Curve with respect to my research. We
> have done a retrospective study on fillings in the tooth and their survival
> in relation to the many influencing factors. We had a long follow-up time
> (upto 8yrs for some variables). However, we decided to stop the analysis at
> the 6year follow up time, so that we can have uniform follow-up time for
> all the variables.
> I did play a bit with the formula and tried to stop the Kaplan-Meier curve
> at my desired time (2190days)or roughly 6 years. However, my question is I
> want to find the significance (log rank test) at this time point between
> the two curves; because I am not able to find a way to stop the survfit at
> this time point with my knowledge. Below is the code I used.
> Gender2<-survfit(Surv(Survival_days, Outcome)~Gender)

I'm assuming that you have a dataframe with those variables and have attached it. If so, then:

dfrm <- detach(said_df)

# If not, then:

dfrm <- data.frame(Survival_days, Outcome, Gender)

Gender2<-survfit(Surv(Survival_days, Outcome)~Gender, 
                  data=dfrm, subset = Survival_days < 6*365.25 )

> plot (Gender2, xmax=2190, mark.time=FALSE, col=c(1:2), xlab="Survival time
> (Days)", ylab="Survival probability", main="Gender") # mark.time=FALSE will
> remove the censoring lines in the graph. If censoring lines are needed,
> then remove the mark.time section in the formula.
> legend("topright",c("Females", "Males"), col=(1:2), lwd=0.5)
> Am sure, the code in the first line has to be modified. Please help me with
> this and I will be very thankful to you.

David Winsemius
Alameda, CA, USA

Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues 

More information about the R-help mailing list