0

I would like to have those two plots gathered on one single plot. This would allow me to check how survival evolve on the long run while visually assessing the goodness of fit of the distribution.

Can you help me?

Current KM with flexsurvreg

library(survminer)
require(flexsurv)
data(bc)

su_obj <- Surv(bc$rectime, bc$censrec)

fit_0 <- do.call(flexsurvreg, list(formula =su_obj~group, data = bc, dist = "exponential"))

ggsurvplot(fit_0)

Extended predicted survival based on flexsurvreg

time_0 = 5000

survival_ext = summary(fit_0, type = "survival",t=1:time_0)

survival_ext = as.data.frame(survival_ext)

survival_ext = survival_ext[,grep(".est", names(survival_ext))]

survival_ext = cbind(1:time_0, survival_ext)

names(survival_ext)[1]="time"

survival_ext = reshape2::melt(survival_ext,id="time") 

ggplot(survival_ext,aes(x=time, y=value, color=variable)) +
  geom_line() +
  labs(x="Time",
       y="Survival probability",
       color="")
Ben
  • 28,684
  • 5
  • 23
  • 45

1 Answers1

2

I would imagine there are a number of different thoughts about this, but here is one simple approach if it helps.

fit_0_obj <- ggsurvplot(fit_0)

ggplot(survival_ext,aes(x=time, y=value, color=variable)) +
  geom_line() +
  labs(x="Time",
       y="Survival probability",
       color="") +
  geom_step(data = fit_0_obj$data.survplot, aes(x=time, y=surv, color=group))

Edit (9/20/21): With a bug in the survminer package, the "group" in the bc data needs to be character and not a factor. Until fixed, you can do the following to reproduce the plot:

bc$group <- as.character(bc$group)

survival curve

Ben
  • 28,684
  • 5
  • 23
  • 45
  • Waw exactly what I was looking for Txs – Augustin Terlinden Dec 23 '19 at 09:41
  • 1
    I tried using the code (after searching for a package with a bc data object that has the needed columns) and get an error "Error in FUN(X[[i]], ...) : object 'group' not found" – IRTFM Sep 19 '21 at 23:57
  • This works if you change group to character instead of factor in the bc data...I found a bug in the survminer package with `ggflexsurvplot` from the commit in July 2019 (the code has `as.facet` instead of `as.factor` when checking if factor or character...). I will follow up on github... – Ben Sep 20 '21 at 11:44