1
votes

The following example is modified from document example by adding "age"

d.coxph <- (survfit(Surv(time, status) ~ sex+age, data = lung))
autoplot(d.coxph)

I'll get the following error:

Error in levels<-(*tmp*, value = if (nl == nL) as.character(labels) else paste0(labels, : factor level [41] is duplicated

Enter a frame number, or 0 to exit

1: autoplot(d.coxph)> Error in levels<-(*tmp*, value = if (nl == nL) as.character(labels) else paste0(labels, : factor level [41] is duplicated

Enter a frame number, or 0 to exit

1: autoplot(d.coxph) 2: autoplot.survfit(d.coxph) 3: fortify(object, surv.connect = surv.connect, fun = fun) 4: fortify.survfit(object, surv.connect = surv.connect, fun = fun) 5: factor(rep(groupIDs, model$strata), levels = groupIDs)

2: autoplot.survfit(d.coxph) 3: fortify(object, surv.connect = surv.connect, fun = fun) 4: fortify.survfit(object, surv.connect = surv.connect, fun = fun) 5: factor(rep(groupIDs, model$strata), levels = groupIDs)

1
I don't think that formula is what you want, look at plot(d.coxph) and after minimal testing, it doesn't seem like autoplot supports a survfit formula with two variables on the rightrawr

1 Answers

3
votes

A possible solution is to divide the continuous variable age into categories and to consider the interaction between sex and age in the survfit formula:

library(survival)
data(lung)
lung$age2cat <- cut(lung$age,breaks=2)
lung$sex <- factor(lung$sex, labels=c("F","M"))
d.coxph <- survfit(Surv(time, status) ~ interaction(sex,age2cat), data = lung)

autoplot(d.coxph, conf.int=F, surv.size=1)

enter image description here