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

# Antwort: Re: Fw: st: xtfmb: Fama MacBeth regression

 From biljana.dlab@bf.uzh.ch To statalist@hsphsun2.harvard.edu Subject Antwort: Re: Fw: st: xtfmb: Fama MacBeth regression Date Wed, 18 Jul 2012 15:07:26 +0200

```Dear Maarten,

i got xtfmb from this web-page:

http://ideas.repec.org/c/boc/bocode/s456786.html

Thank you.

Best,
Biljana

Von:    Maarten Buis <maartenlbuis@gmail.com>
An:     statalist@hsphsun2.harvard.edu,
Datum:  17.07.2012 10:26
Betreff:        Re: Fw: st: xtfmb: Fama MacBeth regression
Gesendet von:   owner-statalist@hsphsun2.harvard.edu

On Tue, Jul 17, 2012 at 9:10 AM,  <biljana.dlab@bf.uzh.ch> wrote:
> I would like to do Fama MacBeth regression and i used xtfmb function.
>
> however I would like now to do it step-by-step, for each year I have in
> sample (1971 - 2001), so that I see changes from one year to the next
one,
> later on I can take the average of the coefficients:
>
> so I tried with this code:
>
>
> forvalues fyear ‘ i ’ {
> reg Cash Debt
> matrix coefficient =e(b)
> matrix regression_result [ ‘ i ’, 1] =coefficient [1 ,1]
> matrix regression_result [ ‘ i ’, 2] =coefficient [1 ,2]
> matrix regression_result [ ‘ i ’, 3] = e(N)
> matrix regression_result [ ‘ i ’, 4] = e(r2_a)
> i = ‘ i ’ +1
> }
> matrix list regression_result
>
> however I am getting error message

First, if you are referring to user written software (in this case
-xtfmb-) you need to tell us where you got it from (SSC, SJ, some
private website, ...). The logic behind this requirement is that often
there are multiple versions of user written software floating around
in cyberspace and you can imagine that not talking about the same
program can lead to a lot of unnecessary confusion. This is clearly
explained in the Statalist FAQ, which you were asked to read before
posting on Statalist. The Statalist FAQ also asked you to say exactly
what Stata told you in return, not just that it gave you an error
message, but also which error message.

Second, I see a couple of problems with your loop. First, you seem to
mixing the syntaxes of -while- and -forvalues-, or looping syntaxes
from different programming languages. For the right syntax see -help
forvalues-. In this case I would either use a combination of
-levelsof- and -foreach- or -forvalues-. The latter is a bit more
fragile as it would break when you get the beginning or end year wrong
or if there are gaps.  Second, you are not using the right single
quotes for referencing local macros, see -help macro-. It is a bit
hard to explain which quotes are the right ones as their location on
the keyboard can differ quite a bit from country to country. On German
keyboards the opening quote is above the ue (in combination with the
shift key), the closing quote is right of ae (also in combination with
the shift key)(*). Third, you are not estimating the regression on
different sub-samples, but just repeat the same regression on the
entire population over and over again.

Third, there is an easier solution that does not require you to write
a loop: -statsby-.

Below is an example that illustrates -levelsof- and -foreach-,
-forvalues- and -statsby-:

*--------------------- begin example ------------------------
sysuse nlsw88, clear

//------------------------------- doing the loop with foreach

// create an empty matrix that is to be filled in
matrix regression_results = J(13, 5, .)
matrix colnames regression_results = age grade _cons N r2_a

// collect the values of age in local levs
levelsof age, local(levs)

// loop over those values
local j = 1
foreach i of local levs {
reg wage grade if age == `i'
matrix regression_results[`j',1] = `i', e(b),e(N),e(r2_a)
local j = `j' + 1
}

// display the result
matlist regression_results

//------------------------------ doing the loop with levelsof
local j = 1
forvalues i = 34/46 {
reg wage grade if age == `i'
matrix regression_results[`j',1] = `i', e(b),e(N),e(r2_a)
local j = `j' + 1
}

// display the result
matlist regression_results

//-------------------------------------- the easy alternative
statsby _b e(N) e(r2_a), by(age) : reg wage grade
list
*---------------------- end example -------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

(*) For those not familiar with German: ue and ae are the plain text
equivalents of u umlaut or u with two dots on top and a umlaut or a
with two dots on top, and they are used so often in German that they
are directly on the German keyboard.

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```