Search code examples
rfunctionsurvival-analysis

Writing a function to produce a Kaplan-Meier curve


I am trying to write a function that spits out a KM survival curve. I am going to use this in a ShineyApp which is why I want to write a function so I can easily pass in arguments from a dropdown menu (which will input as a string into the strata argument). Here is a simplified version of what I need:

survival_function <- function(data_x, strata_x="1"){
  
survFormula <- Surv(data_x$time, data_x$status)
my_survfit <- survfit(data=data_x, as.formula(paste("survFormula~", {{strata_x}})))
ggsurvplot(my_survfit, data = data_x, pval=T)
  
}

survival_function(inputdata, "strata_var")

I get an error: Error in paste("survFormula1~", { : object 'strata_x' not found

I'm at a loss because as.formula(paste("~", {{arg}})) has worked in other functions I've written to produce plots using ggplot to easily change variables to facet by, but this doesn't even seem to recognize strata_x as an argument.


Solution

  • Your function needs a couple of tweaks to get it working with ggsurvplot. It would be best to create the Surv object as a new column in the data frame and use this column in your formula. You also need to make sure you have an actual symbolic formula as the $call$formula member of the survfit object, otherwise ggsurvplot will fail to work due to non-standard evaluation deep within its internals.

    library(survival)
    library(survminer)
    
    survival_function <- function(data_x, strata_x) {
      
      data_x$s <- Surv(data_x$time, data_x$status)
      survFormula <- as.formula(paste("s ~", strata_x))
      my_survfit <- survfit(survFormula, data = data_x)
      my_survfit$call$formula <- survFormula
      ggsurvplot(my_survfit, data = data_x)
    
    }
    

    We can test this on the included lung data set:

    survival_function(lung, "sex")
    

    enter image description here

    Created on 2022-08-03 by the reprex package (v2.0.1)