Same Explanatory Variables, Multiple Dependent Variables in R

I needed to run variations of the same regression model: the same explanatory variables with multiple dependent variables. In R, we can do this with a simple for() loop and assign().

First I specify the dependent variables:

dv <- c("dv1", "dv2", "dv3")

Then I create a for() loop to cycle through the different dependent variables:

for(i in 1:length(dv)){

Within this loop, I need to create an object to hold the models. I need a separate object for each model, so I create one with paste(). For the first dependent variable, this will be model1; for the second dependent variable model2, and so on.

model <- paste("model",i, sep="")

With this object to hold the model in place, I can run the model: the ith dependent variable is used. It is stored in an object called m.

m <- lm(as.formula(paste(dv[i],"~ ev1 + ev2")), data=mydata)

Now, I assign the model m to the model object created above: model1 for the first dependent variable, etc. That’s also the end of the for() loop.

assign(model,m)}

We can now look at the results:

summary(model1); summary(model2); summary(model3)

or, more practical to compare models:

library(memisc)
mtable(model1, model2, model3)

10 Replies to “Same Explanatory Variables, Multiple Dependent Variables in R”

  1. Your loop is great! Thank you.
    I only have a question, if i want proob the combination between n dependent variables with m independent variables, how i can write the loop?

    1. The approach using assign() described here should also work, just nest two for() loops. So you’d loop over say j (like for(j in 1:length(ev)){), and then have something like ev <- c("~ ev1 + ev2", "~ ev1 + ev2 + ev3", "~ ev2 + ev3") earlier on to define the explanatory variables. The line with the actual assignment may then be something like m <- lm(as.formula(paste(dv[i],ev[j])), data=mydata). In this example we'd have 9 models.

  2. Thank you, this was very helpful. If I wanted to put the regression output from all models into a data frame (e.g. with the coefficient, standard error and p-value), how would I go about doing this?

    1. I’m not sure why you’d want to have the output in a data.frame, but you’d have to create the data.frame bit by bit. Perhaps the easiest would be to change what you put into “m”; in the code above we put the model, but you can of course only but part of the model output. Check out the structure of the output by using str(m). For instance, in an OLS, you can access the coefficients using m$coefficients. Alternatively, modify the information in mtable or stargazer.

  3. I really liked the straightforward method and enjoyed your clear explanations, my appreciation. I have a problem with having categorical variables in the models in a loop (I have tried other methods and failed). When I use this: lm(as.formula(paste(dv[i],”~ ev1 + as.factor(ev2)”)), data=mydata)
    This error is reported “Error: Can’t convert to .” Note the use of as.factor(ev2) instead of ev2. Would appreciate any help.

    1. Thanks for checking in. I’m afraid, I cannot replicate your error. Here’s what I did:

      # create data
      dv1 = runif(15)
      dv2 = runif(15)
      dv3 = runif(15)
      ev1 = runif(15)
      ev2 = runif(15)
      ev3 = as.factor(c(rep(“A”, 10), rep(“B”, 5)))
      mydata = data.frame(dv1, dv2, dv3, ev1, ev2, ev3)
      # specify outcome variables:
      dv = c(“dv1”, “dv2”, “dv3”)
      # loop with continuous variables (ev1, ev2)
      for(i in 1:length(dv)){
      model = paste(“model”,i, sep=””)
      m = lm(as.formula(paste(dv[i],”~ ev1 + ev2″)), data=mydata)
      assign(model,m)}
      # results
      summary(model1); summary(model2); summary(model3)

      # loop with the factor variable (ev3)
      for(i in 1:length(dv)){
      model = paste(“model”,i, sep=””)
      m = lm(as.formula(paste(dv[i],”~ ev1 + ev3″)), data=mydata)
      assign(model,m)}
      # results
      summary(model1); summary(model2); summary(model3)

      So there’s nothing wrong with the loop when it comes to factor variables… Possibly it helps if you use the as.factor() before the loop:

      mydata$ev4 = as.factor(mydata$ev2)

      and then use ev4 in the loop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: