Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Foreach loop, panel data, and residuals
From
Adrian Stork <[email protected]>
To
[email protected]
Subject
st: Foreach loop, panel data, and residuals
Date
Tue, 17 Dec 2013 17:36:46 +0100
Dear Stata-friends
I'm close to the solution I'm looking for but double-checking shows
some deviations that I can't figure out.
So I got a panel dataset defined by cusip and date which looks like this:
cusip date2 date
ex_ret_daily mktrf smb hml
90000000 1990m1 01Jan1990 0.10
1.5 0.3 0.2
90000000 1990m1 02Jan1990 0.40
0.7 0.6 0.7
.... .... ....
... ... ....
....
90000004 1983m1 01Jan1983 0.14
1.5 0.3 0.9
90000007 1983m1 02Jan1983 0.45
1.7 0.6 0.3
etc.
Now I want to retrieve the residuals from the regression:
ex_ret_daily= beta*mktrf + smb + hml
but for each cusip and month, that is, using the approx 25 daily
returns from a month of a cusip as the subsample each regression
should run on.
I wrote a code that works and the results even look reasonable, which
looks as follows:
***
egen group = group(cusip date2)
gen residual = .
foreach i of var group {
regress ex_ret_daily mktrf smb hml if group == `i'
predict temp, residuals
replace residual=temp if group == `i'
drop temp
}
***
However, when I compare the residuals from my code and the residuals
from a subsample of one cusip of one specific month, they are not
identical, and I don't know where I could've made a mistake? If anyone
has a suggestion, I would very much appreciate it.
Best regards & merry christmas,
Adrian
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/