title: Flexible, variable number of regressors in glm
body: <p>I have a set of linear regressions (lets assume 100) that I need to run with variable number of regressors in R. Some of the regressors are common to all 100 regression models, but others are variable and depends on the specific dependent variable. As an example, here are three such models:</p>
  
  <p>Y1 ~ x1 + x2 + x3 + z1 + z2</p>
  
  <p>Y2 ~ x1 + x2 + x3 + z4 + z5 + z6</p>
  
  <p>Y3 ~ x1 + x2 + x3 + z3</p>
  
  <p>As you can note, the dependent variables (Y1, Y2, and Y3) are all different. With the independent variables (i.e. regressor), there are three which are constant to all regression models (x1, x2, and x3), but then there are a number of regressors that are dependent-variable specific (z1, z2, z3, z4, z5, z6). </p>
  
  <p>If I were to store all the variables (dependent and independent) in one dataframe with rows corresponding to each sample, and columns corresponding to variables, is there an easy way to run all the regressions by creating some sort of loop and without having to write each regression separately?  Subsequently, I want to extract the residuals and store them in a new dataframe.</p>