R: Increasing Loop speed of rolling regression

Viewed 42

I am currently working on a research project using mutual funds data. As part of my final regression, I will need to do several intermediate regressions. Many of them will be of a "rolling nature", i.e. the lm model will use a moving subset of the total data frame. I produced code, but it is taking ages and I am hoping you could help me bumping up the speed. The main issue is that I am doing a panel data analysis, and in my data frame df, the column which indicates the specific fund contains over 2000 different values. Hence, I cannot simply do a rolling regression with packages like rollRegres. My current code looks as follows:

rolling_1_year_alpha<-as.numeric(rep(0,nrow(df)) #vector to store values

for (i in 13:nrow(df)) {
    if (df[i,11]==df[i-12,11]) #column 11 contains the fund identifier
    {my_roll_reg<- lm(mret_RF~Mkt_RF,data=df[i-12:i,],na.action = na.exclude)    
    rolling_1_year_alpha[i,]<-coef(my_roll_reg)["(Intercept)"]
    } 
} 

From my Google research I took with me that indexing, as in [x,y] as well as concatenating slows loops significantly down. Thus, my idea with the already existing output vector. Nonetheless, this does not seem to have helped a lot, as the process still takes several hours. Therefore, I am hoping for some suggestions to improve the current code. Any help is much appreciated! Many thanks in advance!

Edit: the data frame df looks roughly as follows. (numbers in brackets indicate row numbers):

    fund_flows | fund_mtna | fund_mret | FundId_ms | date_crsp | Mkt_RF | mret_RF
(1) 0.04231541   99.6       -0.0048154   FS00009PK8  2013-11-30  0.0313  -0.00481
(2) 0.08113293   109.6       0.0192686   FS00009PK8  2013-12-31  0.0281   0.01926
(3)   .             .             .            .         .          .        .
(4)   .             .             .            .         .          .        .
(n-1) -0.00571   713.0       0.0435607   FSUSA00D77  2021-03-31  0.0308   0.04356
(n)  -0.005239   739.5       0.0424068   FSUSA00D77  2021-04-30  0.0493   0.04240

So the data frame is sorted on a first level by FundId_ms and on a second level by date_crsp. Hence, I need to check for my regression (loop) that FundId_ms of row (i) == FundId_ms of row (i-1). I could imagine that this might further prolong the processing time of the loop besides the large amount of rows.

0 Answers
Related