<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/how-to-create-a-loop-to-run-multiple-regression-models/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/how-to-create-a-loop-to-run-multiple-regression-models/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A friend asked me whether I can create a loop which will run multiple regression models. She wanted to evaluate the association between 100 dependent variables (outcome) and 100 independent variable (exposure), which means 10,000 regression models. Regression models with multiple dependent (outcome) and independent (exposure) variables are common in genetics. </p>
<p>So models will be something like this: (dx is dependent and ix is independent variable, v are other variables)</p>
<pre>
dx1 = ix1 + v1 + v2 + v3
dx1 = ix2 + v1 + v2 + v3
dx1 = ix3 + v1 + v2 + v3
...
dx1 = ix100 + v1 + v2 + v3
dx2 = ix1 + v1 + v2 + v3
...
dx100 = ix100 + v1 + v2 + v3</pre>
<p>The output should be a data frame with 5 columns, including dependent variable, independent variable, beta estimate, standard error and the p-value.<br>
Something like this (those numbers are just for illustration purposes):</p>
<pre><em> d i beta se pvalue
1 dx1 ix1 0.1 0.002 0.950
2 dx2 ix2 0.2 0.002 0.826
3 dx3 ix3 0.3 0.005 0.123</em></pre>
<p>OK, now lets begin: the dataset that I received had all the variables in columns and observations in rows (the data is not real, just random numbers for illustration purposes):</p>
<pre><em>id dx1 dx2 ... dx100 ix1 ... 1x100 v1 v2 v3
10 324 124 ... 214 32 ... 32 ax b4 c3
11 431 982 ... 114 12 ... 77 ce b2 c5
12 545 123 ... 104 34 ... 11 ar c2 a5
....</em></pre>
<h2>Position of variables</h2>
<p>Create vectors for the position of the dependent and independent variables in your dataset.</p>
<pre>
# outcome
out_start=2
out_end= 101
out_nvar=out_end-out_start+1

out_variable=rep(NA, out_nvar)
out_beta=rep(NA, out_nvar)
out_se = rep(NA, out_nvar)
out_pvalue=rep(NA, out_nvar)

# exposure
exp_start=102
exp_end=203
exp_nvar=exp_end-exp_start+1

exp_variable=rep(NA, exp_nvar)
exp_beta=rep(NA, exp_nvar)
exp_se = rep(NA, out_nvar)
exp_pvalue=rep(NA, exp_nvar)

number=1</pre>
<h2>For Loop</h2>
<p>I used linear mixed effect model and therefore I loaded the <code>lme4</code> library. The loop should work with other regression analysis (i.e. linear regression), if you modify it according to your regression model. If you don’t know which part to modify, leave a comment below and I will try to help. </p>
<p>As other loops, this call variables of interest one by one and for each of them extract and store the betas, standard error and p value. Remember, this code is specific for linear mixed effect models. </p>
<pre>
library(lme4)
for (i in out_start:out_end){
  outcome = colnames(dat)[i]
  for (j in exp_start:exp_end){
    exposure = colnames(dat)[j]
    model &lt;- lmer(get(outcome) ~ get(exposure) + v1 + (1|v2) + (1|v3),
      na.action = na.exclude,
      data=dat)

    Vcov &lt;- vcov(model, useScale = FALSE)
    beta &lt;- fixef(model)
    se &lt;- sqrt(diag(Vcov))
    zval &lt;- beta / se
    pval &lt;- 2 * pnorm(abs(zval), lower.tail = FALSE)
    
    out_beta[number] = as.numeric(beta[2])
    out_se[number] = as.numeric(se[2])
    out_pvalue[number] = as.numeric(pval[2])
    out_variable[number] = outcome
    number = number + 1
    
    exp_beta[number] = as.numeric(beta[2])
    exp_se[number] = as.numeric(se[2])
    exp_pvalue[number] = as.numeric(pval[2])
    exp_variable[number] = exposure
    number = number + 1
  }
}
</pre>
<p>Create a dataframe with results:</p>
<pre>
outcome = data.frame(out_variable, out_beta, out_se, out_pvalue)
exposure = data.frame(exp_variable, exp_beta, exp_se, exp_pvalue)</pre>
<h2>Management of the dataframe</h2>
<p>We have 2 different dataframes with our results and we need to combine in one. With the help of <code>tidyverse</code> package this is a simple task. Basically we rename variables by giving the same name and after we merge both dataframes together.</p>
<pre>
library(tidyverse)
outcome = outcome %>% 
  rename(
    variable = out_variable,
    beta = out_beta,
    se = out_se,
    pvalue = out_pvalue,
    obs = out_nobs
    )
exposure = exposure %>% 
  rename(
    variable = exp_variable,
    beta = exp_beta,
    se = exp_se,
    pvalue = exp_pvalue,
    obs = exp_nobs
    )
all = rbind(outcome, exposure)
all = na.omit(all)

head(all)
<em> variable beta se pvalue
1 dx1 0.1 0.002 0.950
3 dx2 0.2 0.002 0.826
........
2 ix1 0.1 0.002 0.950
4 ix2 0.2 0.002 0.826
........</em></pre>
<p>Yet, this is not a dataframe that we are looking for. We need a dataframe to have both dependent and independent variables in one row. Therefore, we do the final transformation as follows:</p>
<pre>
data = all %>% 
  mutate(
    type = substr(variable, 1, 2)
  ) %>% 
  spread(type, variable) %>% 
  rename(
    d = dx,
    i = ix
  ) %>% 
  mutate (
    beta = round(beta, 5),
    se = round(se, 5),
    pvalue = round(pvalue, 5)
  ) %>% 
  select(d, i, beta, se, pvalue)

head(data)
<em> d i beta se pvalue
1 dx1 ix1 0.1 0.002 0.950
2 dx2 ix2 0.2 0.002 0.826
3 dx3 ix3 0.3 0.005 0.123</em></pre>
<p>I hope you find this post useful for your research and data analysis!</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toRegression model with auto correlated errors – Part 3, some astrology" target="_blank" rel="nofollow" href="https://datascienceplus.com/regression-model-with-auto-correlated-errors-part-3-some-astrology/">Regression model with auto correlated errors – Part 3, some astrology</a></li>
<li><a title="Permanent Link toRegression model with auto correlated errors – Part 2, the models" target="_blank" rel="nofollow" href="https://datascienceplus.com/regression-model-with-auto-correlated-errors-part-2-the-models/">Regression model with auto correlated errors – Part 2, the models</a></li>
<li><a title="Permanent Link toRegression model with auto correlated errors – Part 1, the data" target="_blank" rel="nofollow" href="https://datascienceplus.com/fitting-a-simple-regression-model-with-auto-correlated-errors-an-astrological-example-part-1-the-data/">Regression model with auto correlated errors – Part 1, the data</a></li>
<li><a title="Permanent Link toLinear Regression from Scratch in R" target="_blank" rel="nofollow" href="https://datascienceplus.com/linear-regression-from-scratch-in-r/">Linear Regression from Scratch in R</a></li>
<li><a title="Permanent Link toR for Publication by Page Piccinini: Lesson 5 – Analysis of Variance (ANOVA)" target="_blank" rel="nofollow" href="https://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-5-analysis-of-variance-anova/">R for Publication by Page Piccinini: Lesson 5 – Analysis of Variance (ANOVA)</a></li>
</ol></strong>
<div>
<a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=sHMeIJCqvTI:ZFGoMKa4ugk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=sHMeIJCqvTI:ZFGoMKa4ugk:D7DqB2pKExk" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=sHMeIJCqvTI:ZFGoMKa4ugk:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=sHMeIJCqvTI:ZFGoMKa4ugk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/how-to-create-a-loop-to-run-multiple-regression-models/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/QUD_dmxytRQ"></a></div>