-1

i hope the example will work. i wanted to know if there is a way to extract the coordninates of EACH point od a survival curve. i know from ?plot.survfit that it will produce a values with the coordinates of the end of each class, but i would like to know all the points in which the line changes

library(survival)
status <- c(1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,0,0,1,1,0,0,0,0)
classification <- c(1,1,1,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8)
df <-data.frame( Sub = c(letters,LETTERS[1:24]), 
                         status= status, 
                         time= round(rnorm(50,300,100),0), 
                         class = classification )
fit <- survfit(Surv(time, status)~class, data= df)
plotFit <- plot(fit)

i know i can extract the surv value from fit$surv, and that i can combine them with the fit$time and fit$n.event but from this matrix i had to create a cycle to create a point at every 90 degree turn when a status changes, so was wandering if there was a faster way to have directly the values of all those line turning points. thanks in advance

SeGa
  • 9,454
  • 3
  • 31
  • 70
xallalon
  • 1
  • 1

1 Answers1

0

I think its survfit with lowercase and I guess the data.frame was constructed wrong. Like this, the code runs.

library(survival)
status <- c(1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,0,0,1,1,0,0,0,0)
classification <- c(1,1,1,2,2,2,2,2,2,2,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8)


df <-data.frame( Sub = c(letters,LETTERS[1:24]),
                 status= status,
                 time= round(rnorm(50,300,100),0), 
                 class = classification )

fit <- survfit(Surv(time,status)~class, data= df)
plotFit <- plot(fit)

For the coordinates, below is just a start. I think you have to split the lines at the n.events to plot it correctly.

x <- c(1,fit$time)
y <- c(1,(fit$surv))
plot(x,y, type="S")

You could also check out the package survminer and check out the function ggsurvplot. The plots are nicer and the plot-function is exported. Maybe that will help?

library(survminer)
ggsurvplot(fit, data = df)
ggsurvplot
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • thanks for the corrections to the code, I don't need the actual plot, but the coordinates for other calculations, and i made a matrix for each rank, with fit$time,fit$n.event , fit$surviva, i think i have to make a cycle to see when the event changes and take it s Time coordinate twice with different y ,but was wandering if there was a faster/more direct way of extracting them from the function. – xallalon Jun 01 '18 at 08:36
  • I know, it was just a start but I am not familiar with the survival package and the `plot.survfit` is not an exported function, so we cannot look into it. But from the help page of `plot.survfit` I think you also need to include `fit$strata` somehow. As it says: *one curve for each strata* – SeGa Jun 01 '18 at 11:30
  • 1
    just for future reader, yes, unfortunately the strata changes the names of the suddivision you put, and list them as a table e.g. giving me class=1 3 class=2 7 so i actually added them in a dataframe using the classification vector itself. thanks for the help anyway! – xallalon Jun 11 '18 at 10:19