title: Looping over independent variables in a regression to store P values and R squared values
body: <p>I'm trying to run 150 multiple regression models with the same dependent variable (grain yield) and a covariate (age), but a different predictor in every instance. I would like to store R-squared values and P values for the results of each regression in a data frame that I can easily look at to see which hypotheses are supported by the data. I'm open to alternate ways of doing this- basically I want to know which of the 150 potential predictors have significant interaction with age by glancing at a table, and it would also be nice to know which of the 150 predictors are significant on their own. I've looked through dozens of answers on SO, but I'm still not sure what the best approach is. I do understand that running so many models will lead to Type I error inflation. <a href="https://www.dropbox.com/s/5henzs8o83rvikj/grain%20yield%20by%20environment%20long.csv?dl=0" rel="nofollow noreferrer">Here</a> is a link to my data, but feel free to present a solution in a mtcars or iris if you'd prefer. So far, I've tried using lapply and sapply and writing a function, using a for loop, using tidyr, using list in a for loop, and using append in a for loop. Here's the solution using lapply and sapply, which is promising, but the output is not something that I can present to show which predictors are significant or not- I still have to call out each result one by one. The code takes forever to run, which is why I'm restricting it to the first four columns. All the results are for the same predictor. </p>
  
  <pre><code>`data$Grain&lt;- as.numeric(as.character(data$Grain))
  result &lt;- sapply(names(data)[1 : 4], 
               function(x) { 
                 lapply(names(data)[1 : 4], 
                        function(y) {
                          if (x != y) {
                            model &lt;- lm(as.formula(paste0("Grain", "~", "Age", "*", x)), data) 
                            return(list(x = x, 
                                        r.squared = summary(model)$r.squared, 
                                        coefficients =  summary(model)$coefficients))
                          }
                        })
               })`
  </code></pre>
  
  <p>I've tried removing <code>function(y){}</code> and <code>if(x!=y{}</code>, but I get "Error: unexpected '}' in "}". </p>
  
  <pre><code>`result &lt;- sapply(names(data)[1 : 4], 
               function(x) { 
                 lapply(names(data)[1 : 4],
                            model &lt;- lm(as.formula(paste0("Grain", "~", "Age", "*", x)), data) 
                            return(list(x = x, 
                                        r.squared = summary(model)$r.squared, 
                                        coefficients =  summary(model)$coefficients))
                        )
               })`
  </code></pre>
  
  <p>Perhaps a more promising solution is using lapply to generate a list of regressions:</p>
  
  <p><code>reg &lt;- lapply(data[,-c(1:5,8,18)], function(x) summary(lm(data$Grain~ data$Age*x)))</code></p>
  
  <p>The problem with that is that I can only print all 150 summaries at once- I haven't figured out how to store at least the P values and hopefully also R squared values in a data frame. It's ok if the P values and R squared values are in different data frames. I need to make a presentation on how these 150 predictors are related to Grain and Age, and I don't think a screenshot of 150 different summary print outs is ideal. Thanks for reading- I did my best to check for duplicates but if there's a post you think I missed please let me know.</p>