Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Foreach loop, panel data, and residuals

From   Adrian Stork <>
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

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,
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index