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.
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!