Terry Therneau
2012-Oct-14 20:08 UTC
[R] Problems with coxph and survfit in a stratified model, with interactions
First, here is your message as it appears on R-help. On 10/14/2012 05:00 AM, r-help-request@r-project.org wrote:> I?m trying to set up proportional hazard model that is stratified with > respect to covariate 1 and has an interaction between covariate 1 and > another variable, covariate 2. Both variables are categorical. In the > following, I try to illustrate the two problems that I?ve encountered, using > the lung dataset. > > > > The first problem is the warning: > > > > To me, it seems that there are too many dummies generated. > > The second problem is the error: >Please try to fix this in the future (Nabble issue?) As to the problems: handling strata by covariate interactions turns out to be a bit of a pain in the posteriorin the survival code. It would have worked, however, if you had done the following: fit <- coxph(Surv(time, status) ~ strata(cov1) * cov2, data=...) or ~ strata(cov1):cov2 or ~ strata(cov1):cov2 + cov2 But by using ~ strata(cov1) + cov1:cov2 you fooled the program into thinking that there was no strata by covariate interaction, and so it did not follow the special logic necessary for that case. Second issue: The model.matrix function of R, common to nearly all the modeling functions (including coxph) tries to guess which dummy variables will be redundant, and thus can be removed from the X matrix before the fit. Such an approach is doomed to failure. I'm actually surprised at how often R guesses correctly, because until a matrix decomposition is actually performed the only thing possible is an informed guess. Your particular case gives rise to a larger than usual number of NA coefs (redundant columns), but short of building your own X matrix by hand there isn't anything to be done about it. Just ignore them. Terry Therneau [[alternative HTML version deleted]]
rm
2012-Oct-14 22:59 UTC
[R] Problems with coxph and survfit in a stratified model, with interactions
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) -- View this message in context: http://r.789695.n4.nabble.com/Re-Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646171p4646183.html Sent from the R help mailing list archive at Nabble.com.