Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using survfit object's formula in survdiff call

I'm doing some survival analysis in R, and looking to tidy up/simplify my code.

At the moment I'm doing several steps in my data analysis:

  1. make a Surv object (time variable with indication as to whether each observation was censored);
  2. fit this Surv object according to a categorical predictor, for plotting/estimation of median survival time processes; and
  3. calculate a log-rank test to ask whether there is evidence of "significant" differences in survival between the groups.

As an example, here is a mock-up using the lung dataset in the survival package from R. So the following code is similar enough to what I want to do, but much simplified in terms of the predictor set (which is why I want to simplify the code, so I don't make inconsistent calls across models).

library(survival)

# Step 1: Make a survival object with time-to-event and censoring indicator.
# Following works with defaults as status = 2 = dead in this dataset.
# Create survival object
lung.Surv <- with(lung, Surv(time=time, event=status))

# Step 2: Fit survival curves to object based on patient sex, plot this.
lung.survfit <- survfit(lung.Surv ~ lung$sex)
print(lung.survfit)
plot(lung.survfit)

# Step 3: Calculate log-rank test for difference in survival objects
lung.survdiff <- survdiff(lung.Surv ~ lung$sex)
print(lung.survdiff)

Now this is all fine and dandy, and I can live with this but would like to do better.

So my question is around step 3. What I would like to do here is to be able to use information in the formula from the lung.survfit object to feed into the calculation of the differences in survival curves: i.e. in the call to survdiff. And this is where my domitable [sic] programming skills hit a wall. Below is my current attempt to do this: I'd appreciate any help that you can give! Once I can get this sorted out I should be able to wrap a solution up in a function.

lung.survdiff <- survdiff(parse(text=(lung.survfit$call$formula)))
## Which returns following:
# Error in survdiff(parse(text = (lung.survfit$call$formula))) : 
# The 'formula' argument is not a formula
like image 213
James Stanley Avatar asked Dec 11 '12 23:12

James Stanley


1 Answers

As I commented above, I actually sorted out the answer to this shortly after having written this question.

So step 3 above could be replaced by:

lung.survdiff <- survdiff(formula(lung.survfit$call$formula))

But as Ben Barnes points out in the comment to the question, the formula from the survfit object can be more directly extracted with

lung.survdiff <- survdiff(formula(lung.survfit))

Which is exactly what I wanted and hoped would be available -- thanks Ben!

like image 196
James Stanley Avatar answered Nov 01 '22 09:11

James Stanley