1

I am trying to run ggsurvplot_facet() but cannot get past Error in model.frame.default(formula = surv_object2 ~ id, data = list( :variable lengths differ (found for 'id'). Any help would be greatly appreciated.

I believe the error is in the survfit()/surv_fit() component and not in the ggsurvplot_facet() component.

The data can be found here.

My code is as follows:

library(survival); library(survminer)

read.csv(file = 'test.csv', header = TRUE)

surv_object <- Surv(time = test$Days_survived, event = test$Event1) 
fit <- survfit(surv_object ~ id, data = test)
ggsurvplot_facet(fit = fit, data = test, facet.by = "method", pval = TRUE)

# I have also tried, without success
fit <- survfit(Surv(join$Days_survived, join$Event1) ~ id, data = join) 
ggsurvplot_facet(fit = fit, data = test, facet.by = "method", pval = TRUE)

# and
fit <- survfit(as.formula(paste("Surv(join$Days_survived, join$Event1) ~", id)), data = join)
ggsurvplot_facet(fit = fit, data = test, facet.by = "method", pval = TRUE)

# and
fit <- survfit(as.formula(paste('Surv(join$Days_survived, join$Event1) ~', id)), data = join)
ggsurvplot_facet(fit = fit, data = test, facet.by = "method", pval = TRUE)

# and
fit <- survfit(as.formula(Surv(join$Days_survived, join$Event1) ~ id), data=join)
ggsurvplot_facet(fit = fit, data = test, facet.by = "method", pval = TRUE)

I have also tried all the above combinations using the surv_fit() function.

I have found similar questions here and here, but I have not been able to solve my problem.

Thanks again.

Pat Taggart
  • 321
  • 1
  • 9

1 Answers1

0

This works for me:

lapply(c("survival", "survminer"), require, character.only=TRUE)
df <- read.csv(file = 'test.csv', header = TRUE)
fit <- survfit(Surv(time = Days_survived, event = Event1) ~ id, data=df)
ggsurvplot_facet(fit, facet.by = "method", pval = TRUE, data = df)

Result: enter image description here

user12728748
  • 8,106
  • 2
  • 9
  • 14