0

I have 2 data frames. One contains a set of dependent variables (I believe 12), each of which I would like to run a series of regressions on.

The other contains a set of independent variables: 3 permanent variables that I want in every regression, and then 15 variables that I want to cycle through (for each dependent variable).

So in total, I think I'm looking at 180 different regressions.

To visualize what I'm working with, here's what the data frame of dependent variables looks like:

(Calling it "depvar")

# Date  ReturnsSector1  ReturnsSector2  ReturnsSector3  ...  ReturnsSector12
# 1990      .45             .65             .05                  .87
# 1991      .30             .20             .03                  .23
# 1992      .40             .23             .14                  .65
# ...

And here is what the data frame of the independent variables looks like.

(Calling it "indvar")

# Date  Risk1  Risk2  Risk3  Varied1  Varied2  Varied3  Varied4  ...  Varied15
# 1990  .2     .25    .3     4        6.98     3.43     1.23          1.90
# 1991  .3     .6     .4     4.95     5.67     3.45     1.22          2.92
# 1992  .4     .05    .3     3.75     6.33     3.12     4.98          3.19
# ...

I want "Risk1", "Risk2", and "Risk3" to be in every regression, but "Varied1" to be only in the 1st regression, "Varied2" to be only in the 2nd regression, and so on.

So for example, I'd like to have:

"ReturnsSector1" = "Risk1" + "Risk2" + "Risk3" + "Varied1"
"ReturnsSector1" = "Risk1" + "Risk2" + "Risk3" + "Varied2"
...                 ...       ...       ...       ...
"ReturnsSector1" = "Risk1" + "Risk2" + "Risk3" + "Varied15"

and then repeat until:

"ReturnsSector12" = "Risk1" + "Risk2" + "Risk3" + "Varied1"
"ReturnsSector12" = "Risk1" + "Risk2" + "Risk3" + "Varied2"
...                 ...       ...       ...       ...
"ReturnsSector12" = "Risk1" + "Risk2" + "Risk3" + "Varied15"

I've been able to use the answer from this question: How to Loop/Repeat a Linear Regression in R to cycle through each independent variable, but I couldn't figure out how to automate it to also cycle through the dependent variables at the same time.

Here is the code I have tried:

n <- 19

for (i in names(depvars)) {
  
  lms <- lapply(5:n, function(x) lm(depvars[,i] ~ indvars$Risk1 + indvars$Risk2 + indvars$Risk3 + indvars[,x]))
  
}

Sorry if I overexplained everything, I'm pretty new to R and not totally familiar with the terminology.

A. Handler
  • 63
  • 5
  • 1
    If you have the outer loop through Varied1 ... Varied15, the inner loop through ReturnsSector1 to ReturnsSector12 can be integrated into a single `lm` call. See [Fitting a linear model with multiple LHS](https://stackoverflow.com/q/39262534/4891738). This should greatly simplifies your code. – Zheyuan Li Jul 14 '22 at 03:37
  • Thank you so much! I didn't realize how much I was overcomplicating it. – A. Handler Jul 14 '22 at 14:03

1 Answers1

1

Thanks to @Zheyuan Li for directing me to an answer! I used the code:

n <- 19

lms <- lapply(5:n, function(x) cbind(depvar$ReturnSector1,depvar$ReturnSector2, depvar$ReturnSector3, depvar$ReturnSector4, depvar$ReturnSector5, depvar$ReturnSector6, depvar$ReturnSector7, depvar$ReturnSector8, depvar$ReturnSector9, depvar$ReturnSector10, depvar$ReturnSector11, depvar$ReturnSector12) ~ indvars$Risk1 + indvars$Risk2 + indvars$Risk3 + indvars[,x]))
A. Handler
  • 63
  • 5