[R] plotting coxph results using survfit() function

Stephen Weigand weigand.stephen at gmail.com
Wed Nov 14 05:08:33 CET 2007


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.
>



More information about the R-help mailing list