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)

One thought on “Same Explanatory Variables, Multiple Dependent Variables in R

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s