Antonio has asked the following question
Dear Sir,
I was wondering how to run a Fama and MacBeth regression over 25 Portfolios. In accordance with your code, the first variable needs to be the dependent variable while the following variables are considered as independent variables. Basically, I would like to calculate the risk premium of a factor over the 25 value and size-sorted portfolios. Therefore in my case, I would have more dependent variables and just one dependent variable.
Thanks for your availability
Answer
To answer your question, I have preareed a dummy dataset, which you can download by typing the following in Stata command window.
use http://fintechprofessor.com/ff.dta, clear
So before running the Fama and MacBeth regressions, this is how the data needs to be structured.
The data is in a long format where the portfolios are tracked by a variable, called the panelvar. The portfolio returns are written in a separate variable, in our case, it is named as returns. The panelvar has values from 1, up to 25. The first 10 observations of the portfolios 1 and 2 look like:
. list in 1/10, noob +---------------------------------------------+ | mofd P returns size MTB | |---------------------------------------------| | 1993m6 1 .038 64.0125 5.224508 | | 1993m7 1 .0539 71.86839 4.505145 | | 1993m8 1 -.0639 27.82528 1.888283 | | 1993m9 1 -.0328 20.08383 7.730755 | | 1993m10 1 .0249 59.34985 8.961844 | |---------------------------------------------| | 1993m11 1 .0657 47.42625 3.766557 | | 1993m12 1 .0408 81.47429 5.148165 | | 1994m1 1 .0185 42.39914 5.375627 | | 1994m2 1 .0323 62.36839 4.882884 | | 1994m3 1 -.00598 64.79323 1.281697 | +---------------------------------------------+ . list in 101/110, noob +---------------------------------------------+ | mofd P returns size MTB | |---------------------------------------------| | 1993m6 2 .0114 41.16883 4.549813 | | 1993m7 2 -.0158 10.09915 2.136258 | | 1993m8 2 .00616 73.43023 2.924793 | | 1993m9 2 -.0141 58.28651 7.608449 | | 1993m10 2 .0129 63.4972 1.137969 | |---------------------------------------------| | 1993m11 2 -.0223 16.1786 1.368057 | | 1993m12 2 .0322 64.10929 6.226629 | | 1994m1 2 -.0144 54.48264 7.883276 | | 1994m2 2 .0388 74.99379 1.362888 | | 1994m3 2 .0345 68.66164 7.102628 | +---------------------------------------------+
How to run the Fama and MacBeth regression
My asreg command is available on SSC, to download it, type:
ssc install asreg, replace
asreg can estimate three types of regressions: (1) cross-sectional regressions (2) rolling window regressions and (3) Fama and MacBeth regressions. You can read more details here.
Since our main focus here is on the Fama and MacBeth procedure, the discussion this point onwards will use option fmb of the asreg program. The syntax is:
asreg depvar indepvars, fmb
The data must be first declared as panel data with the xtset command. In our dataset, we have P as the panel variable and mofd as the time variable, therefore, to declare the data as panel data, the xtset command would be:
xtset P mofd
In our dataset, we have the variable returns as the dependent variable and size and MTB as the two independent variables. The command for the Fama and MacBeth regression would be:
. asreg returns size MTB , fmb

Explanation
retunrs = The dependent variable
size and MTB = independent variables
great and simple script to run
Dear Sir,
I thank you for your kind reply, however if I run this code I can not get the coefficients ( risk premia) for each factor. I think that the difference between your dataset and mine is that while my independent variables (e.g market excess return) are not related to the dependent variable (return of 25 portfolios), and therefore for each ID the factors have the same values, in your case the independent variables are related to the dependent one, therefore for each ID your independent variables have different values.
I hope that I have been able to explain my problem..
Again thanks a lot for your availability
Yes, cross-sectionally invariant variables will be omitted in Fama and MacBeth regressions. There was a lengthy discussion on this issue on Statalist, it might be helpful for you. The post can be read here
Dear Sir,
fmb not working, How to install fmb command?
Faisal
fmb is not a command, it is an open of asreg. If you read the above blog entry, it is used as an option of asreg i.e
sir, how do you change the time window for the betas to be calculated?
Dear Sir,
Thank you for your time and I am sorry to write you to your personal email, but none of the answers online seemed to satisfy my inquiries.
I am trying to run a Fama-Macbeth regression for a portfolio of 30 country equity indices, that I treat as stocks. This is in a very similar fashion to what a guy called Antonio asked on your forum and you replied with this post
In my case, I need to do the same but including the Fama French 3 factors, which I consider to be the same for each country, as I utilize the global (developed) factors from the Kenneth French library, and a conflict variable which varies per country (This one does not present any troubles as it is different per country and time).
I, however, get the classical omitted coefficients for those 3 factors as they are time invariant. I have seen plenty of academic research where they do this method, but I do not know how they deal with the time-invariant issue.
Attached, I give a screenshot of a bit of my data in the same way that you provide it on the Antonio post, mainly to note how the 3 factors are constant over id.
note: the code I have tried is the following
Hope I have explained myself correctly and I am deeply thankful for any clarification,
Assalam-O-Alaikum
Dear Juan
I appreciate the detailed email. To you or to anyone else asking about panel-invariant variables such as Fama and French 3 factors, my answer will remain the same. These are omitted in FMB regressions. The reason is simple. FMB estimates cross-sectional regression in each period, consider month March2018, and 100 assets / portfolios. The cross-sectional regression shall use the same value of the three factors against each portfolio, which means no variation i.e see this example
Therefore, the above regression cannot proceed. If you have any paper that uses Fama and French factors in FMB regression, then please provide details.
Dear Sir,
Thank you so much for taking the time to reply to my email. I understand, and that makes sense to me. However, since the excess return of the assets is not the same across time, I would have expected that the first step betas to be different.
I found this paper
Berkman, H., Jacobsen, B., & Lee, J. B. (2011). Time-varying rare disaster risk and stock returns. Journal of Financial Economics, 101(2), 313-332.
which I need to be able to replicate. In section 3.3.3. Cross-sectional evidence, the authors say they run a cross-sectional Fama-Macbeth regression with the crisis variable and the fama french factors:
ri = a0 +b1MKTRF + b2SMB+b3HML+ b4Crisis+er
Followed by the Fama-Macbeth approach.
I attach the paper so you can see for yourself. Thanks again for your time Sir.
Best regards, and thank you in advance.
So your attempt to implement the authors’ method seems incorrect. The authors first estimate factor loadings from a rolling window regression and then use those loadings in the second step, applying FMB procedure. Since the rolling window loadings are all different for the cross-sectional units, therefore, these loadings are not omitted in the FMB regressions.
If you find the above too complicated, you can use our paid help to code for the above two steps. See more details on our paid help https://fintechprofessor.com/paid-help/
Dear sir,
Thank you for the asreg package! I have a question regarding the standard deviations used for t-statistics in the Fama-Macbeth fmb command. Multiple sources on Fama-Macbeth report that the standard deviations of the cross-sectional regression estimates should be calculated as:
sigma^2 = (1/T^2)*Sum((gamma_it – average(gamma_i)^2)
However, when running the asreg fmb command, stata returns the t-statistics corresponding to standard deviations calculated as:
sigma^2 = (1/T)*Sum((gamma_it – average(gamma_i)^2)
I have tested this manually with my data in excel. What is the correct way to calculate standard deviations of the cross-sectional regression coeffisients?
Best,
Mathias
Dear Sir,
I am trying to run the FMB regressions both with asreg and xtfmb commends but neither command works for me. I tried in long and wide formats, for 1,5 and 25 portfolios, without luck.
Maybe you can help me.
For the xtfmb command: I got an r(2000) error with no observation.
For the asreg command: I got
stata() : 3598 Stata returned error
FMB3(): – function returned error
– function returned error
Thank your for your help,
Ricardo
Dear Sir,
Thank you for your post, it really helped me out.
I wanted to ask how you would test the significance of the coefficients using a t test. When I run the code: ttest ER by(Beta), it says “factor-variable and time-series not allowed”.
But I need to test if the averages of all the coefficients are significantly different from 0. How would one do the code? Would really appreciate any help!