[R] plotting coxph results using survfit() function

Shoaaib Mehmood shoaaib at gmail.com
Tue Nov 20 15:46:11 CET 2007


Thanks for your help. I want to draw a plot which shows separate
survival curves for each category of X on the same plot(same set of
axes). Your code produces a separate curve for each combination of X
and Y but I don't want curves for combinations of X and Y since Y has
many levels and also the values of Y don't have any significance in my
case. Is there  a way of doing what i want to do i.e. getting separate
survival curves for each level of X using the function survfit() on an
object mod.phm which is a coxph object such that:

mod.phm<-coxph(formula=Surv(time,Flag_Death)~X+Y, data= datFrame)


On Nov 14, 2007 9:08 AM, Stephen Weigand <weigand.stephen at gmail.com> wrote:
> On Nov 13, 2007 5:53 AM, Shoaaib Mehmood <shoaaib at gmail.com> wrote:
> > i want to make survival plots for a coxph object using survfit
> > function. mod.phm is an object of coxph class which calculated results
> > using columns X and Y from the DataFrame. Both X and Y are
> > categorical. I want survival plots which shows a single line for each
> > of the categories of X i.e. '4' and 'C'. I am getting the following
> > error:
> >
> > > attach(DataFrame)
> > > DataFrame.X<-data.frame(X=c('4','C'),Y=rep(mean(Y),2))
> > > detach()
> As you have it, Y in DataFrame.X will be created as numeric
> which isn't what you probably  want (since you say Y is
> categorical).
> Also, you will run into problems if you create newdata and it
> doesn't have all the levels of all the factors in your model. You
> can do this:
> newdat <- expand.grid(X = c("4", "C", plus all other levels),
>                       Y = c("2", plus all other levels))
> plot(survfit(cox.mod, newdata = subset(newdat, Y == "2")))
> > > plot(survfit(mod.phm, newdata=DataFrame.X), lty=c(1,2),ylim=c(.6,1))
> > Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
> >         contrasts can be applied only to factors with 2 or more levels
> >
> > any help will be appreciated.
> >

Rana Shoaaib Mehmood

More information about the R-help mailing list