title: Fama Macbeth Regression in Python (Pandas or Statsmodels)
body: <p><strong><em>Econometric Backgroud</em></strong></p>
  
  <p>Fama Macbeth regression refers to a procedure to run regression for panel data (where there are N different individuals and each individual corresponds to multiple periods T, e.g. day, months,year). So in total there are N x T obs. Notice it's OK if the panel data is not balanced. </p>
  
  <p>The Fama Macbeth regression is to first run regression for each period cross-sectinally, i.e. pool N individuals together in a given period t. And do this for t=1,...T. So in total T regressions are run. Then we have a time series of coefficients for each independent variable. Then we can perform hypothesis test using the time series of coefficients. Usually we take the average as the final coefficients of each independent variable. And we use t-stats to test significance.</p>
  
  <p><strong><em>My Problem</em></strong></p>
  
  <p>My problem is to implement this in pandas. From the source code of pandas, I noticed there is a procedure called <code>fama_macbeth</code>. But I can't find any documentation about this. </p>
  
  <p>The operation can be easily done through <code>groupby</code> as well. Currently I am doing this:</p>
  
  <pre><code>def fmreg(data,formula):
      return smf.ols(formula,data=data).fit().params[1]
  
  res=df.groupby('date').apply(fmreg,'ret~var1')
  </code></pre>
  
  <p>This works, <code>res</code> is a Series which is indexed by <code>date</code> and the values of Series are <code>params[1]</code>, which is the coefficient of <code>var1</code>. But now I want to have more independent variables, I need to extract the coefficients of all these independent variables, but I can't figure that out. I tried this</p>
  
  <pre><code>def fmreg(data,formula):
      return smf.ols(formula,data=data).fit().params
  
  res=df.groupby('date').apply(fmreg,'ret~var1+var2+var3')
  </code></pre>
  
  <p>This won't work. The desired result is that <code>res</code> is a dataframe indexed by <code>date</code>, and each column of the dataframe should contain the coefficients of each variable <code>intercept</code>, <code>var1</code>, <code>var2</code> and <code>var3</code>.</p>
  
  <p>I also checked with <code>statsmodels</code>, they don't have such built-in procedure as well.</p>
  
  <p>And is there any package that can produce publication-quality regression tables? Like <code>outreg2</code> in Stata and <code>texreg</code> in R?
  Thanks for your help! </p>