# Run Multiple Regression Models in for-Loop in R (Example)

In this article, I’ll show how to estimate multiple regression models in a for-loop in the R programming language.

## Introducing Example Data

The following data is used as basement for this R programming tutorial:

```set.seed(98274) # Creating example data y <- rnorm(1000) x1 <- rnorm(1000) + 0.2 * y x2 <- rnorm(1000) + 0.2 * x1 + 0.1 * y x3 <- rnorm(1000) - 0.1 * x1 + 0.3 * x2 - 0.3 * y data <- data.frame(y, x1, x2, x3) head(data) # Head of data # y x1 x2 x3 # 1 0.5587036 -0.3779533 -0.5320515 -0.92069263 # 2 0.8422515 -1.3835572 1.2782521 0.87967960 # 3 -0.5395343 -0.9729798 -0.1515273 -0.05973894 # 4 -0.3522260 1.2977564 -0.3512013 -0.77239810 # 5 1.5848675 -1.3152806 -2.3644414 -1.14651812 # 6 0.2207957 1.8860636 0.1967851 -0.04963894```

As you can see based on the previous RStudio console output, our example data consists of four numeric columns. The first variable is our regression outcome and the three other variables are our predictors.

## Example: Running Multiple Linear Regression Models in for-Loop

In this Example, I’ll show how to run three regression models within a for-loop in R. In each for-loop iteration, we are increasing the complexity of our model by adding another predictor variable to the model.

First, we have to create a list in which we will store the outputs of our for-loop iterations:

`mod_summaries <- list() # Create empty list`

Now, we can write a for-loop that runs multiple linear regression models as shown below:

```for(i in 2:ncol(data)) { # Head of for-loop   predictors_i <- colnames(data)[2:i] # Create vector of predictor names mod_summaries[[i - 1]] <- summary( # Store regression model summary in list lm(y ~ ., data[ , c("y", predictors_i)]))   }```

Let’s have a look at the output of our previously executed for-loop:

`mod_summaries # Return summaries of all models` As you can see in Figure 1, we have created a list containing three different summary statistics of three different linear regressions.

## Video, Further Resources & Summary

If you need further explanations on the content of this tutorial, I can recommend having a look at the following video that I have published on my YouTube channel.

In the video, I’m explaining how top loop and repeat the estimation of multiple regression models using the R programming syntax of this tutorial in RStudio.

In addition, you may want to read the other R programming tutorials of my homepage.

Summary: At this point you should know how to write a for-loop executing several linear regressions in R programming. Please let me know in the comments below, in case you have further questions. Furthermore, please subscribe to my email newsletter to receive updates on new articles.

Subscribe to the Statistics Globe Newsletter

• Fernando Ramires
March 1, 2021 1:46 pm

the procedure can follow the stepwise line….

• Hey Fernando,

Could you explain what you mean? I’m afraid I don’t get it.

Thanks!

Joachim

• Farieda Hassanen
July 24, 2021 5:22 pm

Very good

• Thanks a lot Farieda, glad you like it! 🙂

• Anu
August 7, 2021 8:07 am

Thanks for the excellent explanation. If you can provide steps on how to use PCA (principal component analysis) on series of images, that would be great!

• Hey Anu,

Thanks a lot for the nice feedback, and for the topic suggestion! I’ve noted it on my to-do list.

Regards

Joachim

• Helen
August 28, 2021 9:14 pm

How can I modify this to have models for each variable by itself? For example I want the the second model to just have x2 and the third model to just have x3 so on, rather than them building onto each other. Also could I change this to do and OddsRatio with each predictor?

• Hey Helen,

To change the predictor variables as you want, you only have to change

`predictors_i <- colnames(data)[2:i]`

to

`predictors_i <- colnames(data)[i]`

(i.e. remove the “2:”).

I don’t have a tutorial on Odds Ratios yet. However, I think this article explains it nicely: https://jarrettmeyer.com/2019/07/23/odds-ratio-in-r

Regards

Joachim