title: Model fitting functions and environemnts
body: <p>Here is a kind of minimal example for a problem I stumbled upon:</p>
  
  <pre><code>mylm &lt;- function(formula,data,subset=NULL){
    mysubset &lt;- subset # some other clever manipulation
    lm(formula,data,mysubset)
  }
  mydata &lt;- data.frame(x=rnorm(10),y=rnorm(10))
  mylm(y~x,mydata) # this fails!
  </code></pre>
  
  <p>The reason why the last line fails is, that <code>lm</code> contains a call to <code>model.frame</code>, which is evaluated in the <code>parent.frame</code>, i.e. <code>lm</code> contains the line of code</p>
  
  <pre><code>mf &lt;- eval(mf, parent.frame())
  </code></pre>
  
  <p>Here the <code>mf</code> on the right side is a cleverly constructed call to <code>model.frame</code>. I am passing on <code>mysubset</code>, but <code>eval</code> looks for it (I believe, but correct me if I am wrong) in the basic environment and doesn't find it. I know that I could probably use <code>lm.fit</code>, but is there a way to make the environment inside <code>mylm</code> the <code>parent.frame</code> for <code>lm</code>?</p>