[R] Problems with coxph and survfit in a stratified model with interactions

rm rm at wippies.se
Sun Oct 14 19:45:01 CEST 2012

Dear all,

Reading a previous post to this forum, I think I managed to get the part of
the code that generates the dataframe (that I input as newdata to survift)
fixed. The problem was that I had not specified the full set of possible
levels for the 2 factors in the dataframe.

Unfortunately, I still get the error ” number of variables != number of
variable names” when I try to fit newdata to a very simple model that is
stratified with respect to cov1 and has an interaction between cov1 and
cov2. It seems that it is the interaction term cov1:cov2 that causes the

Any help would be much appreciated. Complete code follows.


lung$cov1 <- as.factor(lung$ph.ecog)
lung$cov2 <- as.factor(lung$sex)
levels(lung$cov1)[levels(lung$cov1)==0] <- "zero"
levels(lung$cov1)[levels(lung$cov1)==1] <- "one"
levels(lung$cov1)[levels(lung$cov1)==2] <- "two"
levels(lung$cov1)[levels(lung$cov1)==3] <- "three"
levels(lung$cov2)[levels(lung$cov2)==1] <- "male"
levels(lung$cov2)[levels(lung$cov2)==2] <- "female"
df <- data.frame(
  cov1=factor("one", levels = levels(lung$cov1)),
  cov2=factor("male", levels = levels(lung$cov2))
#the following 2 lines work
sCox <- coxph(Surv(time, status) ~ cov1 + cov1:cov2, data=lung)
sfCox <- survfit(sCox,newdata=df)
#as do the following
sCox <- coxph(Surv(time, status) ~ strata(cov1) + cov2, data=lung)
sfCox <- survfit(sCox,newdata=df)
#whereas the following 2 lines doesn't, any suggestions of how to fix them?
sCox <- coxph(Surv(time, status) ~ strata(cov1) + cov1:cov2, data=lung)
sfCox <- survfit(sCox,newdata=df)

View this message in context: http://r.789695.n4.nabble.com/Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646096p4646159.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list