title: Specifying in R points to predict using lm() and predict() with interactions and as.factor vars
body: <p>I want to calculate a few predicted values based on a regression model esimated in R using lm().  </p>
  
  <p>The points to be predicted are not included in the dataset used for regression-- 
  although I suppose they could be with NA's standing in for the dependent variable.  That works in SAS but I'd rather not in this instance.</p>
  
  <p>The simple approach below initially worked well for my simple purpose.</p>
  
  <pre><code>myLm1 = lm(log(p) ~ u + v + w, data=myDat)                                          
  
  DatToPred1 = data.frame(u=72, v=20, w=85)                                                     
  
  predict(myLm1, DatToPred1)            
  </code></pre>
  
  <p>But suppose now the model specification includes an interaction x*y.  The lines
  below throw an error.</p>
  
  <pre><code>myLm2 = lm(log(p) ~ u + v + w + x*y, data=myDat)
  
  DatToPred2 = data.frame(u=72, v=20, w=85, x=1, y=45)                                                     
  
  predict(myLm2, DatToPred2)
  
  Error in data.frame(u=72, v=20, w=85, x=1, y=45,  : 
    argument is missing, with no default
  </code></pre>
  
  <p>This seems strange since lm() can find x and y to form x*y, seems like
  predict() might be able to do the same.</p>
  
  <p>Incidently, including x*y in the definition of DatToPred2 as below also fails.</p>
  
  <pre><code>DatToPred2 = data.frame(u=72, v=20, w=85, x*y=45)
  </code></pre>
  
  <p>Finally suppose the model has been further augmented to include a full suite of dummies for the categorical variable z.</p>
  
  <pre><code>myLm3 = lm(log(p) ~ u + v + w + x*y + as.factor(z), data=myDat)
  </code></pre>
  
  <p>I'm at a loss for a way to specify the values for a point to be estimated.
  Further, z can take on a large number n of values and listing all the values for its dummies corresponding to a particular point to be predicted would be tedious:</p>
  
  <pre><code>   d_z1=0, d_z2=0, ... , d_zi=1, d_z(i+1)=0, ... , d_zn=0
  </code></pre>
  
  <p>And in any event I don't know how R would expect to see these dummies named in the data.frame() definition for the point to be predicted.</p>
  
  <p>The time will come when there are a large number of points to be predicted and
  their values will be stored together in a dataframe.  But at this point it would be a great advance to find a way to predict a single point in a model with interactions and as.factor's.</p>
  
  <p>There are a lot of online examples involving lm() and predict(), but those I've
  found tend not to involve the tweaks raised here.</p>
  
  <p>Thanks in advance. </p>