title: R Loop: Multiple linear regression models (exclude 1 variable at a time)
body: <p>How do you create a loop that automates running several linear regression models? I have a full model with 12 independent variables. I want to create other models that exclude 1 independent variable at a time.</p>
  
  <p>Please see the example below:</p>
  
  <pre><code> #round 1 full model
    formula &lt;- Bound_Count~Days_diff_Eff_Subm_2 +
    TR_BS_BROKER_ID_360_2 + TR_BS_BROKER_ID_360_M +
    RURALPOP_P_CWR_2 + RURALPOP_P_CWR_M +
    TR_B_BROKER_ID_360 + TR_SCW_BROKER_ID_360 +
    PIP_Flag + TR_BS_BROKER_INDIVIDUAL_720_2 + TR_BS_BROKER_INDIVIDUAL_720_M +
    Resolved_Conflict + Priority_2
  
      # split train and test
      dataL_TT  &lt;- dataL[dataL$DataSplit_Ind=="Modeling",]
      dataL_V &lt;- dataL[dataL$DataSplit_Ind=="Validation",]
      # bind to submit model
      modelTT &lt;- glm(formula
                     ,family=binomial(link = "logit"), data=dataL_TT)
      modelTT$aic
  
      # round 2 exclude TR_BS_BROKER_ID_360_M
      formula2 &lt;- Bound_Count~Days_diff_Eff_Subm_2 +
        TR_BS_BROKER_ID_360_2 +
        RURALPOP_P_CWR_2 + RURALPOP_P_CWR_M +
        TR_B_BROKER_ID_360 + TR_SCW_BROKER_ID_360 +
        PIP_Flag +
        TR_BS_BROKER_INDIVIDUAL_720_2 + TR_BS_BROKER_INDIVIDUAL_720_M +
        Resolved_Conflict +
        Priority_2
      modelTT2 &lt;- glm(formula2 , family=binomial(link = "logit"), data=dataL_TT)
      modelTT2$aic
  
      # round 3 exclude Days_diff_Eff_Subm_2
      formula3 &lt;- Bound_Count~TR_BS_BROKER_ID_360_2 + TR_BS_BROKER_ID_360_M +
        RURALPOP_P_CWR_2 + RURALPOP_P_CWR_M +
        TR_B_BROKER_ID_360 + TR_SCW_BROKER_ID_360 +
        PIP_Flag +
        TR_BS_BROKER_INDIVIDUAL_720_2 + TR_BS_BROKER_INDIVIDUAL_720_M +
        Resolved_Conflict +
        Priority_2
      modelTT3 &lt;- glm(formula3 , family=binomial(link = "logit"), data=dataL_TT)
      modelTT3$aic
  
      # round 4 exclude TR_BS_BROKER_ID_360_2
      formula4 &lt;- Bound_Count~Days_diff_Eff_Subm_2 + TR_BS_BROKER_ID_360_M +
        RURALPOP_P_CWR_2 + RURALPOP_P_CWR_M +
        TR_B_BROKER_ID_360 + TR_SCW_BROKER_ID_360 +
        PIP_Flag +
        TR_BS_BROKER_INDIVIDUAL_720_2 + TR_BS_BROKER_INDIVIDUAL_720_M +
        Resolved_Conflict +
        Priority_2
      modelTT4 &lt;- glm(formula4 , family=binomial(link = "logit"), data=dataL_TT)
      modelTT4$aic
  </code></pre>
  
  <p>And so on.. Basically I need to have 12 models that exclude 1 distinct independent variable at a time.</p>