title: Is there a way to loop a regression by a subset?
body: <p>For a regression project, I created clusters of data via nbclust using kmeans. There are currently 4 clusters in this project, but if successful, the project could involve analyzing multiple US cities. Although I could build each regression manually, I would prefer to minimize the amount of code and, hence, the opportunity for coding errors.</p>
  
  <p>The current model uses glm with multiple subsets and 10-15 different independent variables. I am currently building each cluster regression by hand.</p>
  
  <pre><code>Tulsa$Cluster &lt;- Tulsa$Best.partition
  #This creates a vector of clusters; there are currently 4 distinct clusters
  
  summary(Tulsa_Cluster1 &lt;- glm(formula = Tulsa$Result ~ Tulsa$FactorA Tulsa$FactorB + Tulsa$FactorC + Tulsa$FactorD + Tulsa$FactorE + Tulsa$FactorF, 
                                method = "glm.fit", family = gaussian(),subset = Tulsa$Cluster =="1"))
  
  summary(Tulsa_Step &lt;- stepAIC(Tulsa_Cluster1, direction = "both",trace = FALSE, 
                                scope=list(lower = ~ Tulsa_Cluster1$FactorA)))
  </code></pre>
  
  <p>My hope is to develop a regression by cluster which I can then summarize.</p>