title: R formula in survfit
body: <p>I am having trouble with formulas, environments, and <code>survfit()</code>.</p>
  
  <p>Things work fine for <code>lm()</code> but they fail for <code>survfit()</code>.</p>
  
  <h1>General problem statement:</h1>
  
  <p>I am fitting a series of formulas to some data.  So, I call the
  modeling function with the formula passed as a variable.  Later,
  I want to work with the formula from the fitted object.</p>
  
  <p>(From my naive point of view, the trouble comes from survfit not
  recording the environment.)</p>
  
  <h1>Detailed Example</h1>
  
  <p>Expected behaviour as seen in <code>lm()</code>:</p>
  
  <pre><code>library("plyr")
  
  preds &lt;- c("wt", "qsec")
  
  f &lt;- function() {
    lm(mpg ~ wt, data = mtcars)
  }
  
  fits &lt;- alply(preds, 1, function(pred)
  {
    modform &lt;- reformulate(pred, response = "mpg")
  
    lm(modform, data = mtcars)
  })
  
  fits[[1]]$call$formula
  ##modform
  formula(fits[[1]])
  ## mpg ~ wt
  ## &lt;environment: 0x1419d1a0&gt;
  </code></pre>
  
  <p>Even though <code>fits[[1]]$call$formula</code> resolves to <code>modform</code> I can
  still get the original formula with <code>formula(fits[[1]])</code>.</p>
  
  <p>But things fail for <code>survfit()</code>:</p>
  
  <pre><code>library("plyr")
  library("survival")
  
  preds &lt;- c("resid.ds", "rx", "ecog.ps")
  
  fits &lt;- 
    alply(preds, 1, function(pred)
    {
      modform &lt;- paste("Surv(futime, fustat)", pred, sep = " ~ ")
      modform &lt;- as.formula(modform)
      print(modform)
  
      fit &lt;- survfit(modform, data = ovarian)
    })
  
  fits[[1]]$call$formula
  ## modform
  formula(fits[[1]])
  ## Error in eval(expr, envir, enclos) : object 'modform' not found
  </code></pre>
  
  <p>Here (and in contrast to lm-fits), <code>formula(fits[[1]])</code> does not
  work!</p>
  
  <p>So, my specific question is: How can I get back the formula used
  to fit <code>fits[[1]]</code>?</p>