I wanted to run separate regression across firms and years on two variables - "stockreturn" and "marketreturn" - to find the residuals and fitted values.
I tried to do it using a forvalue loop but in the results, the variable "fitted" gets dropped.
I couldnt figure out why. Can someone find what went wrong?
The data is a panel across 15years with 1893 firms.
'code' specifies the firm.
----------------------------------------------------------------
xtset code date
sort code date
gen fitted=.
gen residual =.
nois _dots 0, title(Loop running) reps(100)
forvalues i=1/1893 {
forvalues j=2001/2015 {
capture quietly regress stockreturn marketreturn if code== `i' & year== `j'
capture predict f if code== `i' & year== `j'
capture predict r if code== `i' & year== `j', resid
capture replace fitted = f if code== `i' & year== `j'
capture replace residual = r if code== `i' & year== `j'
capture drop f
capture drop r
}
nois _dots `i' 0
}
---------------------------------------------------------------
# I had added the "_dots" commands to observe loop progress.
# 'capture' was used so that the loop completes even in case of missing "i" and "j" values in the data.
# each regression is done on daily returns of each firm and for a particular year .. i.e almost 250 observations
I tried to do it using a forvalue loop but in the results, the variable "fitted" gets dropped.
I couldnt figure out why. Can someone find what went wrong?
The data is a panel across 15years with 1893 firms.
'code' specifies the firm.
----------------------------------------------------------------
xtset code date
sort code date
gen fitted=.
gen residual =.
nois _dots 0, title(Loop running) reps(100)
forvalues i=1/1893 {
forvalues j=2001/2015 {
capture quietly regress stockreturn marketreturn if code== `i' & year== `j'
capture predict f if code== `i' & year== `j'
capture predict r if code== `i' & year== `j', resid
capture replace fitted = f if code== `i' & year== `j'
capture replace residual = r if code== `i' & year== `j'
capture drop f
capture drop r
}
nois _dots `i' 0
}
---------------------------------------------------------------
# I had added the "_dots" commands to observe loop progress.
# 'capture' was used so that the loop completes even in case of missing "i" and "j" values in the data.
# each regression is done on daily returns of each firm and for a particular year .. i.e almost 250 observations