title: predict.lm with newdata
body: <p>I've built an <code>lm</code> model without using the <code>data=</code> parameter:</p>
  
  <pre><code>m1 &lt;- lm( mdldvlp.trim$y ~  gc.pc$scores[,1] + gc.pc$scores[,2] + gc.pc$scores[,3] + 
                              gc.pc$scores[,4] + gc.pc$scores[,5] + gc.pc$scores[,6] + predict(gc.tA))
  </code></pre>
  
  <p>Now I'd like to predict <code>m1</code> using <code>newdata</code> and so name my new data.frame to match the variables used in the <code>lm()</code> call above.</p>
  
  <p>With <code>newComps</code> as my new <code>gc.pc</code> (which, like the <code>gc.tA</code> prediction, were predicted using the new data.frame without any issues), I've tried</p>
  
  <pre><code>newD &lt;- data.frame( newComps[1:100,1:6] ,
                      predict(gc.tA , newdata = mdldvlp[1:100,predKept]))
  
  
  names(newD) &lt;- names(m1$coefficients)[-1]
  names(newD) &lt;- names(m1$model)[-1]
  
  names(newD) &lt;- c( "gc.pc$scores[, 1]" , "gc.pc$scores[, 2]" , "gc.pc$scores[, 3]" , 
                    "gc.pc$scores[, 4]" , "gc.pc$scores[, 5]" , "gc.pc$scores[, 6]" , 
                    "predict(gc.tA)" )
  names(newD) &lt;- c( "gc.pc$scores[,1]" , "gc.pc$scores[,2]" , "gc.pc$scores[,3]" , 
                    "gc.pc$scores[,4]" , "gc.pc$scores[,5]" , "gc.pc$scores[,6]" , 
                    "predict(gc.tA)" )
  </code></pre>
  
  <p>Unfortunately, <code>predict.lm</code> does not accept the naming strategies above and returns the dreaded <code>newdata</code> warning along with the predictions from the original data.frame that built <code>m1</code>:</p>
  
  <pre><code>Warning message:
  'newdata' had 100 rows but variable(s) found have 1414 rows  
  </code></pre>
  
  <p>How should I name the <code>newD</code> columns to make the <code>predict</code> call work? Thanks.</p>
  
  <p>The code below recreates the issue:</p>
  
  <pre><code>    require(rpart)
  
      set.seed(123)
      X &lt;- matrix(runif(200) , 20 , 10)
      gc.pc &lt;- princomp(X)
      y &lt;- runif(20)
      mdldvlp.trim &lt;- data.frame(y,X)
      names(mdldvlp.trim) &lt;- c("y",paste("x",1:10,sep=""))
      predKept &lt;- paste("x",1:10,sep="")
  
      gc.tA &lt;- rpart( y ~ . , data = mdldvlp.trim)
  
      m1 &lt;- lm( mdldvlp.trim$y ~  gc.pc$scores[,1] + gc.pc$scores[,2] + gc.pc$scores[,3] + 
                                  gc.pc$scores[,4] + gc.pc$scores[,5] + gc.pc$scores[,6] + predict(gc.tA))
  
      mdldvlp &lt;- data.frame(matrix(runif(2000) , 200 , 10))
      names(mdldvlp) &lt;- predKept
  
      newComps &lt;- predict( gc.pc , newdata=mdldvlp )
  
      newD &lt;- data.frame( newComps[1:100,1:6] ,
                          predict(gc.tA , newdata = mdldvlp[1:100,predKept]))
  
  # enter newD naming strategy here
  
      predict( m1 , newdata=newD )
  </code></pre>
  
  <h2>4/20 Follow up:</h2>
  
  <p>Thanks all for your answers. I understand things would be easier by first creating a data.frame with properly named predictors. I understand that. My question is if the modeling data frame does indeed evaluate to a data frame with variables named <code>gc.pc$scores[,1]</code> etc. then why won't the naming 'strategies' used above work with <code>predict.lm</code>? In other words, does <code>lm</code> really evaluate its modeling data frame with <code>gc.pc$scores[,1]</code> and so on? If it did, wouldn't the renaming strategies above work in <code>predict.lm</code>?</p>