@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)