1

Kaplan Meier

KMsufit1_2<-survfit(survobj1_2 ~ 1, data = set_1_2_grp)

Parametric Survival Analysis

Gamma1_2<-flexsurvreg(survobj1_2 ~ 1, data = set_1_2_grp, dist = 'gamma')
gen_Gamma1_2<-flexsurvreg(survobj1_2 ~ 1, data = set_1_2_grp, dist = 'gengamma')

I am trying to overlay the parametric survival plots from the above parametric survival objects on to the plot of Kaplan Meier plot. Can someone suggest me a way to do that.

James Z
  • 12,209
  • 10
  • 24
  • 44
Hari
  • 41
  • 3

1 Answers1

1

Looking at the source code for ggsurvplot, you can extract the flexsurv models' fitted data then add the lines to your kap meier plot, e.g.

library(tidyverse)
library(survival)
library(survminer)
#> Loading required package: ggpubr
#> 
#> Attaching package: 'survminer'
#> The following object is masked from 'package:survival':
#> 
#>     myeloma
# install.packages("flexsurv")
library(flexsurv)
data(cancer)

survobj1_2 <- Surv(aml$time, aml$status)
KMsufit1_2 <- survfit(survobj1_2 ~ 1, aml)
Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = aml, dist = 'gamma')
gen_Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = aml, dist = 'gengamma')

p1 <- ggsurvplot(KMsufit1_2, data = aml)$plot

summ <- survminer:::.get_data(Gamma1_2, data = aml)
summ_p2 <- left_join(summ, survminer:::.summary_flexsurv(Gamma1_2, type = c("cumhaz")))

summ <- survminer:::.get_data(gen_Gamma1_2, data = aml)
summ_p3 <- left_join(summ, survminer:::.summary_flexsurv(gen_Gamma1_2, type = c("cumhaz")))


p1 +
  geom_line(aes(time, est, color = "Gamma1_2"),
            data = summ_p2, size = 1) +
  geom_line(aes(time, est, color = "gen_Gamma1_2"),
            data = summ_p3, size = 1)

Created on 2023-07-20 with reprex v2.0.2


With different legend labels:

library(tidyverse)
library(survival)
library(survminer)
# install.packages("flexsurv")
library(flexsurv)
data(cancer)

survobj1_2 <- Surv(aml$time, aml$status)
KMsufit1_2 <- survfit(survobj1_2 ~ 1, aml)
Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = aml, dist = 'gamma')
gen_Gamma1_2 <- flexsurvreg(survobj1_2 ~ 1, data = aml, dist = 'gengamma')

p1 <- ggsurvplot(KMsufit1_2, data = aml, legend.labs = "Kaplan Meier")$plot +
  theme(legend.title = element_blank())

summ <- survminer:::.get_data(Gamma1_2, data = aml)
summ_p2 <- left_join(summ, survminer:::.summary_flexsurv(Gamma1_2, type = c("cumhaz")))

summ <- survminer:::.get_data(gen_Gamma1_2, data = aml)
summ_p3 <- left_join(summ, survminer:::.summary_flexsurv(gen_Gamma1_2, type = c("cumhaz")))

p1 +
  geom_line(aes(time, est, color = "Gamma Distribution"),
            data = summ_p2, size = 1) +
  geom_line(aes(time, est, color = "Generalized Gamma Distribution"),
            data = summ_p3, size = 1) +
  guides(fill = "none") +
  scale_color_hue(direction = -1)

Created on 2023-07-20 with reprex v2.0.2

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46