title: Feeding newdata to R predict function
body: <p>R's <code>predict</code> function can take a <code>newdata</code> parameter and its document reads:</p>
  
  <blockquote>
    <p>newdata An optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used.</p>
  </blockquote>
  
  <p>But I found that it is not totally true depending on how the model is fit. For instance, following code works as expected:</p>
  
  <pre><code>x &lt;- rnorm(200, sd=10)
  y &lt;- x + rnorm(200, sd=1)
  data &lt;- data.frame(x, y)
  train = sample(1:length(x), size=length(x)/2, replace=F)
  dataTrain &lt;- data[train,]
  dataTest &lt;- data[-train,]
  m &lt;- lm(y ~ x, data=dataTrain)
  head(predict(m,type="response"))
  head(predict(m,newdata=dataTest,type="response"))
  </code></pre>
  
  <p>But if the model is fit as such:</p>
  
  <pre><code>m2 &lt;- lm(dataTrain$y ~ dataTrain$x)
  head(predict(m2,type="response"))
  head(predict(m2,newdata=dataTest,type="response"))
  </code></pre>
  
  <p>The last two line will produce exactly the same result. The <code>predict</code> function works in a way ignoring <code>newdata</code> parameter, i.e. it can't really compute the prediction on new data at all.</p>
  
  <p>The culprit, of course, is <code>lm(y ~ x, data=dataTrain)</code> versus <code>lm(dataTrain$y ~ dataTrain$x)</code>. But I didn't find any document that mentioned the difference between these two. Is it a known issue?</p>
  
  <p>I'm using R 2.15.2.</p>