Simple regression loop in R
Budget: $30 – $250 USD
I want to conduct an event study for those who are familiar with that, and have problems with regressing the intercepts and betas in order to calculate expected return. For someone who knows R well, this should be a fairly easy task I believe.
Specifically I have a dataframe with 2,2million observations. Attached is only a sample data frame showing 87k observations. If the code works for the sample data, it will work for the original data frame too.
date = date of observation
TICKER = ticker for the company
RET = return for that company
sprtrn = benchmark index return
dummy = 1 if the company experienced the event on that date. A company can experience several events, but not on the same date.
Given the amount of observations it may be hard to run the loop for all 2,2million observations. I have assigned an event dummy variable in R that can be used to lessen the amount of regressions needed.
If dummy = 1 --> run the regression [RET ~sprtrn] for [-221,-20] observations back from that observation for that firm (NB! [-221,-20] observations back in time, not days back in time! Maybe its easier to use date instead of observations back in time. If so I want to use all dates between [-11,-1] months from that date) and store intercept and coefficient in two different columns in the data frame for all [-5, 60] observations. If the company experience a new event during [-5,60] period, I want the new intercepts and coefficents to overwrite the ones from the previous event.
If unclear, here is an example.
Example
For company XYL on 2020/06/24 the dummy = 1. Regress the [-221,-20] past observations for that firm (or [-11,-1] months) using the OLS regression [RET ~sprtrn]. That would be approximately the observations between [2019/07/24 - 2020/05/24]. Store the intercept and beta from the regression in all rows between the [-5,60] observations from the event date (or 1 week prior to and 12 week after if counting days and not observations). Lets say intercept = 0.01 and coefficient =0.98. These values should be stored in each row between 5 observations back and 60 observations forward.
Please give me feedback if further information is needed or if anything is unclear.
Specifically I have a dataframe with 2,2million observations. Attached is only a sample data frame showing 87k observations. If the code works for the sample data, it will work for the original data frame too.
date = date of observation
TICKER = ticker for the company
RET = return for that company
sprtrn = benchmark index return
dummy = 1 if the company experienced the event on that date. A company can experience several events, but not on the same date.
Given the amount of observations it may be hard to run the loop for all 2,2million observations. I have assigned an event dummy variable in R that can be used to lessen the amount of regressions needed.
If dummy = 1 --> run the regression [RET ~sprtrn] for [-221,-20] observations back from that observation for that firm (NB! [-221,-20] observations back in time, not days back in time! Maybe its easier to use date instead of observations back in time. If so I want to use all dates between [-11,-1] months from that date) and store intercept and coefficient in two different columns in the data frame for all [-5, 60] observations. If the company experience a new event during [-5,60] period, I want the new intercepts and coefficents to overwrite the ones from the previous event.
If unclear, here is an example.
Example
For company XYL on 2020/06/24 the dummy = 1. Regress the [-221,-20] past observations for that firm (or [-11,-1] months) using the OLS regression [RET ~sprtrn]. That would be approximately the observations between [2019/07/24 - 2020/05/24]. Store the intercept and beta from the regression in all rows between the [-5,60] observations from the event date (or 1 week prior to and 12 week after if counting days and not observations). Lets say intercept = 0.01 and coefficient =0.98. These values should be stored in each row between 5 observations back and 60 observations forward.
Please give me feedback if further information is needed or if anything is unclear.