title: How to handle empty/incomplete subsets with data.table
body: <p>I have a panel of data with year, country, and firm identifiers. I would like to fit logit models to each year-country subset using <code>data.table</code>. I don't have a problem if I have enough entries in each year-country subset to fit a model, but if there are not enough data in a year-country subset, then <code>glm</code> throws an error and I can't fit all the models. (I get essentially the same error with <code>lm</code>.)</p>
  
  <p>Is there a solution within <code>data.table</code>? Or should I groom my data upstream to make sure there are no year-country subsets without insufficient data?</p>
  
  <p>Thanks!</p>
  
  <pre><code>library(data.table)
  
  # similar data
  DT &lt;- data.table(year=rep(2001:2010, each=100),
                   country=rep(rep(1:10, each=10), 10), 
                   firm=rep(1:100, 10), 
                   y=round(runif(100)), 
                   x=runif(100)
                   )
  setkey(DT, year, country)
  
  # no problems if there are enough data per year-country subset
  DT2 &lt;- DT[, as.list(coef(glm(y ~ x), family="binomial")), by="year,country"]
  
  # but `lm` throws and error if there are missing data
  DT[(DT$year == 2001) &amp; (DT$country == 1), "y"] &lt;- NA
  DT3 &lt;- DT[, as.list(coef(glm(y ~ x, family="binomial"))), by="year,country"]
  </code></pre>
  
  <p>yields</p>
  
  <pre><code>&gt; DT3 &lt;- DT[, as.list(coef(glm(y ~ x, family="binomial"))), by="year,country"]
  Error in family$linkfun(mustart) : 
    Argument mu must be a nonempty numeric vector
  </code></pre>