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() > 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. -- Regards, Rana Shoaaib Mehmood
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. >
The survfit function, when applied to the results of a Cox model, will give the predicted survival curve for any particular combination of covariates in the model. You cannot get what you are asking for, i.e., distinct levels of X while ignoring Y, from survfit. What you need to do is create a data frame containing values for the curves that you want, e.g., mydata <- data.frame(X=c(1,2,3,4), y=c(8,8,8,8)) cfit <- survfit(mod.phm, newdata=mydata) plot(cfit, lty=1:4) People often choose a 'common' value of y for the plot. Arguably the better approach is to average over the levels of y. For this, I would recommend that you read chapter 10 of Therneau and Grambsch, Modeling Survival Data. The discussion really does take a whole chapter, and is too long for a help-list synopsis. Terry Therneau ------------------------------------------------------------ begin included message 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)