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

Terry Therneau therneau at mayo.edu
Mon Oct 15 15:17:56 CEST 2012


Your df object (newdata) has to have ALL the variables in it.  Normally you wouldn't need 
the strata variable, but in this case cov1 is also a covariate.  Look at the example I 
gave earlier.

Terry T.

On 10/15/2012 05:00 AM, r-help-request at r-project.org wrote:
> Many thanks for your very quick reply!  I tried replacing ?~ strata(cov1) +
> cov1:cov2 ? with  ?~ strata(cov1):cov2 ?. As a result, I get an error
> message saying that ?object 'cov1' not found?. I guess R refers to the data
> frame. Any idea how to fix the error? Complete code follows.
>
> Best regards,
> Roland
>
>
>
> require(survival)
> data(lung)
> #
> 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(
>    cov2=factor("female", levels = levels(lung$cov2))
> )
> sCox<- coxph(Surv(time, status) ~ strata(cov1):cov2, data=lung)
> sfCox<- survfit(sCox,newdata=df)
>




More information about the R-help mailing list