Dario.gs
Dario.gs

Reputation: 65

R - apply survfit to a list and plot with corresponding names

I have a list of named dataframes:

library(survival)
library(survminer)

surv.days<- runif(n = 50, min = 0, max = 500)
censor<- sample(c(0,1), 50, replace=TRUE)
survdata<- data.frame(surv.days, censor)
survlist<- list(survdata, survdata)
names(survlist)<- c("name1", "name2")
rm(survdata, censor, surv.days)

I want to run a survfit on each dataframe and then generate several plots (I put only one here for the sake of simplicity), each plot with the corresponding title. I think Map is the way to do it, so:

titles<- names(survlist)

Then I define the function that I want to use to run the survival analysis and plots:

survival.function<- function(survivaldata, datanames){
  sfit<- survfit(Surv(surv.days, censor)~1, data=survivaldata)
  ggsurvplot(sfit, conf.int=TRUE, risk.table=TRUE,
             surv.median.line = "v",
             title=datanames,
             risk.table.height=.25)
}

And try to apply it:

Map(survival.function, survlist, titles)

But the idea didn't work:
"Error in eval(fit$call$data) : object 'survivaldata' not found "
Is there a way to properly assign the objects to the survival functions? Thank you!.

Upvotes: 1

Views: 792

Answers (1)

IRTFM
IRTFM

Reputation: 263342

In this case the error message seems to be misleading, at least to my reading. It appears to point to the error being in the call to survfit when in fact it's an error within ggsurvplot as can be seen from the output of traceback(). I first tried to change the name of the object passed to survival.function. Then no error. But also no plot. So also added a print call inside survival.function. I'll attache the first of the two plots that result.

survival.function<- function(data, datanames){
  sfit<- survfit(Surv(surv.days, censor)~1, data=data)
  print( ggsurvplot(sfit, conf.int=TRUE, risk.table=TRUE,
             surv.median.line = "v",
             title=datanames,
             risk.table.height=.25) )
}

enter image description here

I wish I could better explain why this hack works. (I'm only guessing at the cause on the basis of the error message and traceback()-results.) I'm using the fact that the name of the "missing" object was "data". It's possible this is a semantic bug in the ggsurvplot and you would be doing a favor to the maintainer of the package to send him a link to this fully documented example. I wonder whetehr there would be possible improvement if the maintainer changed the code so that the list member name were not accessed from the environment with $data but rather [[data]].

Upvotes: 1

Related Questions