title: Automated formula construction
body: <p>In a recent homework assignment, we were instructed to run 27 linear models, each time adding an additional variable (the goal was to plot the changes in R<sup>2</sup> vs. changes in adjusted R<sup>2</sup>). I found it difficult to algorithmically create formulas like this. The code I ended up using looked like this following (note that the first column in the data frame is the dependent variable, all the rest are prospective independent variables.</p>
  
  <pre><code> make.formula &lt;- function(howfar) {
    formula &lt;- c()
    for (i in 1:howfar) {
      if (i == 1) {
        formula &lt;- paste(formula, names(d)[i], '~')}
      else if (i == howfar) {
        formula &lt;- paste(formula, names(d)[i], '')
      }
      else {
        formula &lt;- paste(formula, names(d)[i], '+')}
    }
    return(formula)
  }
  
  formulas &lt;- lapply(seq(2, length(d)), make.formula)
  formulas &lt;- lapply(formulas, as.formula)
  fits &lt;- lapply(formulas, lm, data = d)
  </code></pre>
  
  <p>This works, but seems far from ideal, and my impression is that anything I'm doing with a for-loop in R is probably not being done the best way. Is there an easier way to algorithmically construct formulas for a given data frame?</p>