Marshall Knoderbane
2009-Aug-10 18:48 UTC
[R] survival:: plotting survfit with two predictors
Hi R-Helpers, I am having difficulty plotting a coxph model with two predictors. My predictors are "morder" (a factor with five levels where the mean of each level is plotted as a separate line) and tmean (continuous). When I run a model with just morder it is fine and the plot is fine. When I add tmean, the coxph model runs fine but this model will not plot and I receive an error message. Someone posting back in April 2009 received a similar error in R2.9.0 (which I am using also) when running predict(coxphmodel,newdata=expand.grid()). This code worked for them in R2.8.1 and they thought there might be a bug. I ran my code in R2.6.2 and I did not have any improvement. I suspect it is not a bug but rather a problem with my code. I have not been able to find any other solutions in the achieve and I would greatly appreciate any suggestions. Thanks in advance, Marshall Knoderbane Below is my code for the two models and the error message: a<-read.delim("FDT_weath_dttrans.txt") library(survival) model1<-coxph(Surv(a$deg.below35,a$censor)~a$morder); plot(survfit(model1,conf.type="none",newdata=data.frame(morder=c("Brachycera","Hymenoptera","Nematocera","OtherCol","Zygoptera"))),lty=1:5) #produces a fine plot with a separate curve for each level of morder model2<-coxph(Surv(a$deg.below35,a$censor)~a$morder+a$tmean); summary(model2) #is fine plot(survfit(model2,conf.type="none",newdata=data.frame(morder=c("Brachycera","Hymenoptera","Nematocera","OtherCol","Zygoptera"))),lty=1:5) #produces the following error: Error in model.frame.default(delete.response(Terms), newdata, xlev = object$xlevels) : variable lengths differ (found for 'a$tmean') In addition: Warning message: 'newdata' had 5 rows but variable(s) found have 355 rows