kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
502 stars 162 forks source link

Problems with creating a ggsurvplot function #299

Open JuliaTutzauer opened 6 years ago

JuliaTutzauer commented 6 years ago

I'm trying to code a function with ggsurvplot that returns a Kaplan Meier curve. I've previously done this with the Kaplan Meier function from the survival package without any problem, but I can't make it work with ggsurvplot.

Expected behavior

I want the function to return a Kaplan Meier curve showing the time to metastasis for different groups of breast cancer patients.

The time variable I use is numeric (values are given in years from first diagnosis to metastasis).

Actual behavior

I get the message "Error in Surv(time, event) : Time variable is not numeric".

I have also tried to use as.numeric(time) but then I get the message "Error in Surv(as.numeric(time), event) : Invalid status value, must be logical or numeric In addition: Warning message: In Surv(as.numeric(time), event) : NAs introduced by coercion"

Steps to reproduce the problem

surv2 <- function(data, time, event, grpvar){

SurvObj <- Surv(time, event)

fit<- do.call(survfit(SurvObj ~ grpvar, data = data))
ggsurvplot(fit, data = data)
}

surv2(dataj, "time_BC1_to_met", "metastasis","tam_bc1")`

session_info()


# please paste here the result of
Session info ---------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, mingw32             
 ui       RStudio (1.1.442)           
 language (EN)                        
 collate  Swedish_Sweden.1252         
 tz       Europe/Berlin               
 date     2018-04-03                  

Packages -------------------------------------------------------------------------------------------------------------
 package      * version   date       source                               
 acepack        1.4.1     2016-10-29 CRAN (R 3.4.3)                       
 assertthat     0.2.0     2017-04-11 CRAN (R 3.4.2)                       
 backports      1.1.2     2017-12-13 CRAN (R 3.4.3)                       
 base         * 3.4.2     2017-09-28 local                                
 base64enc      0.1-3     2015-07-28 CRAN (R 3.4.1)                       
 bindr          0.1.1     2018-03-13 CRAN (R 3.4.4)                       
 bindrcpp     * 0.2.2     2018-03-29 CRAN (R 3.4.4)                       
 broom          0.4.4     2018-03-29 CRAN (R 3.4.4)                       
 caret        * 6.0-78    2017-12-10 CRAN (R 3.4.4)                       
 checkmate      1.8.5     2017-10-24 CRAN (R 3.4.3)                       
 class          7.3-14    2015-08-30 CRAN (R 3.4.2)                       
 cluster        2.0.6     2017-03-10 CRAN (R 3.4.2)                       
 cmprsk         2.2-7     2014-06-17 CRAN (R 3.4.4)                       
 codetools      0.2-15    2016-10-05 CRAN (R 3.4.2)                       
 colorspace     1.3-2     2016-12-14 CRAN (R 3.4.2)                       
 compiler       3.4.2     2017-09-28 local                                
 CVST           0.2-1     2013-12-10 CRAN (R 3.4.2)                       
 data.table     1.10.4-3  2017-10-27 CRAN (R 3.4.3)                       
 datasets     * 3.4.2     2017-09-28 local                                
 ddalpha        1.3.1.1   2018-02-02 CRAN (R 3.4.4)                       
 DEoptimR       1.0-8     2016-11-19 CRAN (R 3.4.1)                       
 devtools       1.13.5    2018-02-18 CRAN (R 3.4.3)                       
 digest         0.6.15    2018-01-28 CRAN (R 3.4.3)                       
 dimRed         0.1.0     2017-05-04 CRAN (R 3.4.2)                       
 dplyr        * 0.7.4     2017-09-28 CRAN (R 3.4.3)                       
 DRR            0.0.3     2018-01-06 CRAN (R 3.4.4)                       
 foreach        1.4.4     2017-12-12 CRAN (R 3.4.4)                       
 foreign        0.8-69    2017-06-22 CRAN (R 3.4.2)                       
 Formula      * 1.2-2     2017-07-10 CRAN (R 3.4.1)                       
 ggkm         * 0.6.0     2018-04-01 Github (sachsmc/ggkm@ce24953)        
 ggplot2      * 2.2.1     2016-12-30 CRAN (R 3.4.4)                       
 ggpubr       * 0.1.6     2017-11-14 CRAN (R 3.4.4)                       
 glue           1.2.0     2017-10-29 CRAN (R 3.4.2)                       
 gower          0.1.2     2017-02-23 CRAN (R 3.4.2)                       
 graphics     * 3.4.2     2017-09-28 local                                
 grDevices    * 3.4.2     2017-09-28 local                                
 grid           3.4.2     2017-09-28 local                                
 gridExtra      2.3       2017-09-09 CRAN (R 3.4.4)                       
 gtable         0.2.0     2016-02-26 CRAN (R 3.4.2)                       
 Hmisc        * 4.1-1     2018-01-03 CRAN (R 3.4.3)                       
 htmlTable      1.11.2    2018-01-20 CRAN (R 3.4.3)                       
 htmltools      0.3.6     2017-04-28 CRAN (R 3.4.3)                       
 htmlwidgets    1.0       2018-01-20 CRAN (R 3.4.3)                       
 ipred          0.9-6     2017-03-01 CRAN (R 3.4.2)                       
 iterators      1.0.9     2017-12-12 CRAN (R 3.4.3)                       
 kernlab        0.9-25    2016-10-03 CRAN (R 3.4.1)                       
 km.ci          0.5-2     2009-08-30 CRAN (R 3.4.4)                       
 KMsurv         0.1-5     2012-12-03 CRAN (R 3.4.1)                       
 knitr          1.20      2018-02-20 CRAN (R 3.4.3)                       
 labeling       0.3       2014-08-23 CRAN (R 3.4.1)                       
 lattice      * 0.20-35   2017-03-25 CRAN (R 3.4.3)                       
 latticeExtra   0.6-28    2016-02-09 CRAN (R 3.4.3)                       
 lava           1.6.1     2018-03-28 CRAN (R 3.4.4)                       
 lazyeval       0.2.1     2017-10-29 CRAN (R 3.4.2)                       
 lubridate      1.7.2     2018-02-06 CRAN (R 3.4.3)                       
 magrittr     * 1.5       2014-11-22 CRAN (R 3.4.2)                       
 MASS           7.3-49    2018-02-23 CRAN (R 3.4.4)                       
 Matrix         1.2-12    2017-11-16 CRAN (R 3.4.4)                       
 MatrixModels   0.4-1     2015-08-22 CRAN (R 3.4.4)                       
 memoise        1.1.0     2017-04-21 CRAN (R 3.4.4)                       
 methods      * 3.4.2     2017-09-28 local                                
 mnormt         1.5-5     2016-10-15 CRAN (R 3.4.1)                       
 ModelMetrics   1.1.0     2016-08-26 CRAN (R 3.4.2)                       
 multcomp       1.4-8     2017-11-08 CRAN (R 3.4.4)                       
 munsell        0.4.3     2016-02-13 CRAN (R 3.4.2)                       
 mvtnorm        1.0-7     2018-01-26 CRAN (R 3.4.3)                       
 nlme           3.1-131.1 2018-02-16 CRAN (R 3.4.4)                       
 nnet           7.3-12    2016-02-02 CRAN (R 3.4.2)                       
 parallel       3.4.2     2017-09-28 local                                
 pillar         1.2.1     2018-02-27 CRAN (R 3.4.4)                       
 pkgconfig      2.0.1     2017-03-21 CRAN (R 3.4.2)                       
 plyr           1.8.4     2016-06-08 CRAN (R 3.4.2)                       
 polspline      1.1.12    2015-07-14 CRAN (R 3.4.1)                       
 prodlim        1.6.1     2017-03-06 CRAN (R 3.4.2)                       
 psych          1.8.3.3   2018-03-30 CRAN (R 3.4.4)                       
 purrr          0.2.4     2017-10-18 CRAN (R 3.4.2)                       
 quantreg       5.35      2018-02-02 CRAN (R 3.4.4)                       
 R6             2.2.2     2017-06-17 CRAN (R 3.4.2)                       
 ranger       * 0.9.0     2018-01-09 CRAN (R 3.4.3)                       
 RColorBrewer * 1.1-2     2014-12-07 CRAN (R 3.4.1)                       
 Rcpp           0.12.16   2018-03-13 CRAN (R 3.4.4)                       
 RcppRoll       0.2.2     2015-04-05 CRAN (R 3.4.2)                       
 recipes        0.1.2     2018-01-11 CRAN (R 3.4.4)                       
 reshape2       1.4.3     2017-12-11 CRAN (R 3.4.4)                       
 rlang          0.2.0     2018-02-20 CRAN (R 3.4.4)                       
 rms          * 5.1-2     2018-01-07 CRAN (R 3.4.4)                       
 robustbase     0.92-8    2017-11-01 CRAN (R 3.4.2)                       
 rpart          4.1-13    2018-02-23 CRAN (R 3.4.4)                       
 rstudioapi     0.7       2017-09-07 CRAN (R 3.4.3)                       
 sandwich       2.4-0     2017-07-26 CRAN (R 3.4.4)                       
 scales       * 0.5.0     2017-08-24 CRAN (R 3.4.3)                       
 sfsmisc        1.1-2     2018-03-05 CRAN (R 3.4.4)                       
 SparseM      * 1.77      2017-04-23 CRAN (R 3.4.1)                       
 splines        3.4.2     2017-09-28 local                                
 stats        * 3.4.2     2017-09-28 local                                
 stats4         3.4.2     2017-09-28 local                                
 stringi        1.1.7     2018-03-12 CRAN (R 3.4.4)                       
 stringr        1.3.0     2018-02-19 CRAN (R 3.4.4)                       
 survival     * 2.41-3    2017-04-04 CRAN (R 3.4.4)                       
 survminer    * 0.4.2     2018-03-29 Github (kassambara/survminer@94fb918)
 survMisc       0.5.4     2016-11-23 CRAN (R 3.4.4)                       
 TH.data        1.0-8     2017-01-23 CRAN (R 3.4.4)                       
 tibble         1.4.2     2018-01-22 CRAN (R 3.4.4)                       
 tidyr          0.8.0     2018-01-29 CRAN (R 3.4.4)                       
 tidyselect     0.2.4     2018-02-26 CRAN (R 3.4.4)                       
 timeDate       3043.102  2018-02-21 CRAN (R 3.4.3)                       
 tools          3.4.2     2017-09-28 local                                
 utils        * 3.4.2     2017-09-28 local                                
 withr          2.1.2     2018-03-15 CRAN (R 3.4.4)                       
 xtable         1.8-2     2016-02-05 CRAN (R 3.4.4)                       
 yaml           2.1.18    2018-03-08 CRAN (R 3.4.4)                       
 zoo            1.8-1     2018-01-08 CRAN (R 3.4.4) ````
sbihorel commented 6 years ago

Hi,

The error you report comes from the Surv function, which is part of the survival package, not survminer.

Did you check the class of your time object or run a summary?

JuliaTutzauer commented 6 years ago

Thank you for your reply. The time object is numeric.

Is there a risk that the definition of the event is the problem?

sbihorel commented 6 years ago

You may have a problem of non-standard evaluation. Can you try

surv2 <- function(data, timevar, eventvar, grpvar){
  str <- sprintf(
    'survfit(formula = Surv(%s,%s) ~ %s, data = data)',
    timevar,
    eventvar,
    grpvar)
  fit<- eval(parse(text = str))
  ggsurvplot(fit, data = data)
}
surv2(dataj, "time_BC1_to_met", "metastasis","tam_bc1")`
emilliman5 commented 6 years ago

This should work. A few, notes: 1) I changed time to tte and data to dat to prevent conflicts with the time and data functions (more a preference than a necessity. 2) Constructing a Surv requires the use of with so that Surv knows which object the columns come from. i.e. try

data(lung) Surv(time,status) with(Surv(time,status), data=lung)

Though this did not solve the error, so I took another route by creating a string on the fly and coercing it to a formula.

surv2 <- function(dat, tte, event, grpvar){
  # SurvObj <- Surv(time, event)
  # fit<- do.call(survfit(SurvObj ~ grpvar, data = data))

  fit <- survfit(as.formula(paste('Surv(',tte, ",",event,') ~', grpvar)), data = dat)
  ggsurvplot(fit, data = dat)
}

I am not an expert in parametrizing against the formula interface, but this seems a bit more natural when using a formula interface. HTH.