I am trying to run regressions by companyID and year, and save the coefficients for each firm-year model as new variables in a new column right besides the other columns. There is an additional wrinkle‹ I have panel data for 1990-2010 and want to run each regression using t to t-4 only (I.e., for 2001, use only 1998-2001 years of data and i.e. for 1990 then only the data of 1990 and so on). I am new to using foreach loops and I found some prior coding on the web. I have tried to adapt it to my situation but two issues: anything.....
the output is staying blank
I have not figured out how to use the rolling four year data periods.
Here is the code I tried. Any suggestions would be much appreciated.
use paneldata.dta // the dataset I am working in
generate coeff . //empty variable for coefficient
foreach x of local levels {
forval z = 1990/2010
{
capture reg excess_returns excess_market
replace coeff = _b[fyear] & _b[CompanyID] if e(sample) }
}
So below is a short snapshot of what the data looks like;
CompanyID Re_Rf Rm-Rf Year
10 2 2 1990
10 3 2 1991
15 3 2 1991
15 4 2 1992
15 5 2 1993
21 4 2 1990
21 4 2 1991
34 3 1 1990
34 3 1 1991
34 4 1 1992
34 2 1 1993
34 3 1 1994
34 4 1 1995
34 2 1 1996
Re_Rf = excess_returns
Rm_Rf = excess_market
I want to run the following regression:
reg excess_returns excess_market