Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

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

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

Subject   Antwort: Re: Fw: st: xtfmb: Fama MacBeth regression
Date   Wed, 18 Jul 2012 15:28:25 +0200

dear maarten,

i tried and it works with the provided example, however i get error 
message "no data" with my data - I do have observations - what could be 
the problem:

. matrix regression_results = J(30, 9, .)

. matrix colnames regression_results = fyear CASH_FLOW Q SIZE 

. levelsof fyear, local(levs)
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1
> 996 1997 1998 1999 2000 2001

. local j = 1

. foreach i of local levs {
  2.                  reg DELTA_DEBT CASH_FLOW Q SIZE DELTA_CASH_HOLD 
DEBT_LAG1 if fyear == `i'
  3.                  matrix regression_results[`j',1] = `i', 
  4.                  local j = `j' + 1
  5. }
no observations

end of do-file


Von:    Maarten Buis <>
Datum:  17.07.2012 10:26
Betreff:        Re: Fw: st: xtfmb: Fama MacBeth regression
Gesendet von:

On Tue, Jul 17, 2012 at 9:10 AM,  <> 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 
> 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

A couple of comments:

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
*---------------------- end example -------------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

(*) 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

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index