title: Use Predict on data.table with Linear Regression
body: <p>Regrad to this <a href="https://stackoverflow.com/questions/11266930/data-table-vs-plyr-regression-output?rq=1">Post</a>, I have created an example to play with linear regression on <strong>data.table</strong> package as follows:</p>
  
  <pre><code>## rm(list=ls()) # anti-social
  library(data.table)
  set.seed(1011)
  DT = data.table(group=c("b","b","b","a","a","a"),
                  v1=rnorm(6),v2=rnorm(6), y=rnorm(6))
  setkey(DT, group)
  ans &lt;- DT[,as.list(coef(lm(y~v1+v2))), by = group]
  </code></pre>
  
  <p>return,</p>
  
  <pre><code>   group (Intercept)        v1        v2
  1:     a    1.374942 -2.151953 -1.355995
  2:     b   -2.292529  3.029726 -9.894993
  </code></pre>
  
  <p>I am able to obtain the coefficients of the <code>lm</code> function. </p>
  
  <p><strong>My question is:</strong>
  How can we directly use <code>predict</code> to new observations ?  If we have the new observations as follows:</p>
  
  <pre><code>new &lt;- data.table(group=c("b","b","b","a","a","a"),v1=rnorm(6),v2=rnorm(6))
  </code></pre>
  
  <p>I have tried:</p>
  
  <pre><code>setkey(new, group)
  DT[,predict(lm(y~v1+v2), new), by = group]
  </code></pre>
  
  <p>but it returns me strange answers:</p>
  
  <pre><code>    group         V1
   1:     a  -2.525502
   2:     a   3.319445
   3:     a   4.340253
   4:     a   3.512047
   5:     a   2.928245
   6:     a   1.368679
   7:     b  -1.835744
   8:     b  -3.465325
   9:     b  19.984160
  10:     b -14.588933
  11:     b  11.280766
  12:     b  -1.132324
  </code></pre>
  
  <p>Thank you</p>