rm
2012-Oct-13 20:52 UTC
[R] Problems with coxph and survfit in a stratified model with interactions
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: -- View this message in context: http://r.789695.n4.nabble.com/Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646095.html Sent from the R help mailing list archive at Nabble.com.
rm
2012-Oct-13 20:55 UTC
[R] Problems with coxph and survfit in a stratified model with interactions
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: -- View this message in context: http://r.789695.n4.nabble.com/Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646096.html Sent from the R help mailing list archive at Nabble.com.
Terry Therneau
2012-Oct-15 13:17 UTC
[R] Problems with coxph and survfit in a stratified model with interactions
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) >
Chris Andrews
2012-Oct-15 14:30 UTC
[R] Problems with coxph and survfit in a stratified model with interactions
rm 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:Adding the main effect of cov2 allows the model to run library(survival) lung$cov1 <- factor(lung$ph.ecog, 0:3, c("zero","one","two","three")) lung$cov2 <- factor(lung$sex, 1:2, c("male","female")) sCox <- coxph(Surv(time, status) ~ strata(cov1) + cov2 + cov1:cov2, data=lung) df <- data.frame( cov1=factor(0:3, 0:3, c("zero","one","two","three"))[1], cov2=factor(1:2, 1:2, c("male","female"))[1] ) df sfCox <- survfit(sCox, newdata=df) plot(sfCox[1], conf=FALSE) lines(sfCox[2], col=2) lines(sfCox[3], col=3) lines(sfCox[4], col=4) -- View this message in context: http://r.789695.n4.nabble.com/Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646096p4646241.html Sent from the R help mailing list archive at Nabble.com.
rm
2012-Oct-15 15:09 UTC
[R] Problems with coxph and survfit in a stratified model with interactions
Dear Terry, Thanks for your valuable comments! Could you please restate the example that you refer to? If I include cov1 in the data frame, as you suggest, I get the following error message. Complete code follows. ? Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels" Regarding Chris?s suggestion, I would prefer not to have the main effect of cov2 in the model. I would prefer to stick with ?~ strata(cov1):cov2? to keep the interpretation of the regression coefficient straightforward. 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( cov1=factor("one", levels = levels(lung$cov1)), 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/Problems-with-coxph-and-survfit-in-a-stratified-model-with-interactions-tp4646096p4646245.html Sent from the R help mailing list archive at Nabble.com.