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]
Re: st: Foreach loop, panel data, and residuals
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Foreach loop, panel data, and residuals
Date
Tue, 17 Dec 2013 16:50:06 +0000
Your -foreach- command cycles over a list containing precisely one
element, the variable -group-.
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
}
It is thus on all fours with
regress ex_ret_daily mktrf smb hml if group == group
predict temp, residuals
replace residual=temp if group == group
drop temp
which carries out the regression for every observation as -if group ==
group- selects all the data,
Your intent is to cycle over the distinct values of -group-, which is
quite a different matter. See Method 1 in
http://www.stata.com/support/faqs/data-management/try-all-values-with-foreach/index.html
Nick
[email protected]
On 17 December 2013 16:36, Adrian Stork <[email protected]> wrote:
> 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?
*
* 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/