title: anova test fails on lme fits created with pasted formula
body: <p>I often specify the formula argument to model fitting functions like <code>lm</code> or <code>lme</code> by pasting together the parts I need, as in @DWin's answer to this question: <a href="https://stackoverflow.com/questions/6877534/understanding-lm-and-environment/6878461#6878461">Understanding lm and environment</a>.</p>
  
  <p>In practice this looks like this:</p>
  
  <pre><code>library(nlme)
  set.seed(5)
  ns &lt;- 5; ni &lt;- 5; N &lt;- ns*ni
  d &lt;- data.frame(y=rnorm(N),
                  x1=rnorm(N),
                  x2=factor(rep(1:ni, each=ns)),
                  id=factor(rep(1:ns, ni)))
  
  getm &lt;- function(xs) {
    f &lt;- paste("y ~", paste(xs, collapse="+"))
    lme(as.formula(f), random=~1|id, data=d, method="ML")
  }
  m1 &lt;- getm("x1")
  m2 &lt;- getm(c("x1", "x2"))
  </code></pre>
  
  <p>However, with <code>lme</code> from the <code>nlme</code> package, comparing two models constructed in the way using <code>anova</code> doesn't work, because <code>anova.lme</code> looks at the saved formula argument to ensure that the models were fit on the same response, and the saved formula argument is simply <code>as.formula(f)</code>.  The error is:</p>
  
  <pre><code>&gt; anova(m1, m2)
  Error in inherits(object, "formula") : object 'f' not found
  </code></pre>
  
  <p>Here's what the <code>anova</code> command should do (refitting the models so that it works):</p>
  
  <pre><code>&gt; m1 &lt;- lme(y~x1, random=~1|id, data=d, method="ML")
  &gt; m2 &lt;- lme(y~x1+x2, random=~1|id, data=d, method="ML")
  &gt; anova(m1, m2)
     Model df      AIC      BIC    logLik   Test  L.Ratio p-value
  m1     1  4 76.83117 81.70667 -34.41558                        
  m2     2  8 72.69195 82.44295 -28.34597 1 vs 2 12.13922  0.0163
  </code></pre>
  
  <p>Any suggestions?</p>