Save multiple regressions' slope in loop

Tried everything to save the slopes, but the double loop made it extremely hard. I have to use the double loop because it is a multiple regression + moving windows regression, I'm trying to save the slopes for each 25 dependent variables of 6 independent variables so I can regress these slopes on other variables. It is similar to Fama and MacBeth two stage regression model, but I used moving window to get the slopes in first stage regression.

The loops is:

for (i in 3:27) {
  for (j in 1:272) { 
    y <- lm(data[j:(60+j),i]~data[j:(60+j),28]+data[j:(60+j),29]+data[j:(60+j),30]+data[j:(60+j),31]+data[j:(60+j),32]+data[j:(60+j),33])
    }
}

Hi @fdasoiuf. I cannot get your problem. Is your problem that you cannot store all result in a list? If yes, you can try the code like this.

y <- list()

for (i in 3:27) {
  for (j in 1:272) { 
    y[[paste(i, j, sep = "_")]] <- lm(data[j:(60+j),i]~data[j:(60+j),28]+data[j:(60+j),29]+data[j:(60+j),30]+data[j:(60+j),31]+data[j:(60+j),32]+data[j:(60+j),33])
  }
}

Thanks for helping, and sorry for my ambiguous expression.
I am trying to do 60 months rolling window regression from time 1 to 332 (60+272) for each dependent variable i, and for each i I want to store 6 slopes of regression in each period j:(j+60).
So for each i of 25 i, I was trying to get only 6 slopes from regression i on 6 variables in each time period of 1-60, 2-61, 3-62......272-332....
Thanks for helping again! This multiple regression and moving window stuff really make things awkward and ugly, I just started self-learning R and it is really hard.....

@fdasoiuf. I transform your code into map function and the result will be a nested list. But I am not sure the mean of 6 slopes is doing regression with the 6 variables separately.

library(tidyverse)

df <- sample(1:100, 33 * 332, replace = TRUE) %>%
  matrix(ncol = 33) %>%
  as.data.frame()

y <- map(3:27,
    function(i) {
      map(1:272,
          function(j, i) {
            map_dbl(28:33,
                function(k, j, i){lm(df[j:(60+j), i] ~ df[j:(60+j), k])[[1]][2]}, 
                j = j, i = i)
          }, i = i)
    })

y
#> [[1]]
#> [[1]][[1]]
#> [1] -0.08263745  0.28262045 -0.20991619  0.23952345  0.16062163  0.09757148
#> 
#> [[1]][[2]]
#> [1] -0.09001809  0.29052931 -0.20671269  0.24330058  0.15896100  0.11171012
#> 
#> [[1]][[3]]
#> [1] -0.1117666  0.3280135 -0.2088403  0.2357410  0.1681716  0.1360815

Or doing regression with 6 variables at once and get 6 coefficient.

library(tidyverse)

df <- sample(1:100, 33 * 332, replace = TRUE) %>%
  matrix(ncol = 33) %>%
  as.data.frame()

y <- map(3:27,
         function(i) {
           map(1:272,
               function(j, i) {
                 lm(df[j:(60+j), i] ~ .,  data =df[j:(60+j), 28:33])[[1]][2:7]
               }, i = i)
         })

y[1]
#> [[1]]
#> [[1]][[1]]
#>         V28         V29         V30         V31         V32         V33 
#> -0.31542857  0.01186105  0.06040676 -0.09049839 -0.14365929 -0.05322534 
#> 
#> [[1]][[2]]
#>         V28         V29         V30         V31         V32         V33 
#> -0.32240292  0.02595142  0.06665279 -0.07010871 -0.11478430 -0.06004581 
#> 
#> [[1]][[3]]
#>         V28         V29         V30         V31         V32         V33 
#> -0.33175693  0.01238759  0.05199046 -0.08300180 -0.12259426 -0.06825498 

Created on 2019-10-17 by the reprex package (v0.3.0)

Wow, that is really sophisticated and really nice coding. Thanks a lot!
I run your code and checked the y variable, they are exactly the format I was trying to achieve.
I will hate myself if I just copy and paste your code and change df to my data. I will try to learn your code first.
Thanks again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.