任何Python库都会生成发布样式回归表

Tit*_*nic 18 python latex regression stata statsmodels

我一直在使用Python进行回归分析.获得回归结果后,我需要将所有结果汇总到一个表中并将它们转换为LaTex(用于发布).是否有任何包在Python中执行此操作?像Stata中的estout这样的东西给出了下表:

在此输入图像描述

Kar*_* D. 25

那么,有summary_colstatsmodels; 它没有所有的花里胡哨estout,但它确实具有您正在寻找的基本功能(包括导出到LaTeX):

import statsmodels.api as sm
from statsmodels.iolib.summary2 import summary_col

p['const'] = 1
reg0 = sm.OLS(p['p0'],p[['const','exmkt','smb','hml']]).fit()
reg1 = sm.OLS(p['p2'],p[['const','exmkt','smb','hml']]).fit()
reg2 = sm.OLS(p['p4'],p[['const','exmkt','smb','hml']]).fit()

print summary_col([reg0,reg1,reg2],stars=True,float_format='%0.2f')

===============================
         p0       p2      p4   
-------------------------------
const -1.03*** -0.01   0.62*** 
      (0.11)   (0.04)  (0.07)  
exmkt 1.28***  0.97*** 0.98*** 
       (0.02)   (0.01)  (0.01)  
smb   0.37***  0.28*** -0.14***
      (0.03)   (0.01)  (0.02)  
hml   0.77***  0.46*** 0.69*** 
      (0.04)   (0.01)  (0.02)  
===============================
Standard errors in parentheses.
* p<.1, ** p<.05, ***p<.01
Run Code Online (Sandbox Code Playgroud)

或者这里是我添加R-Squared的版本和观察数量:

print summary_col([reg0,reg1,reg2],stars=True,float_format='%0.2f',
                  info_dict={'N':lambda x: "{0:d}".format(int(x.nobs)),
                             'R2':lambda x: "{:.2f}".format(x.rsquared)})

===============================
         p0       p2      p4   
-------------------------------
const -1.03*** -0.01   0.62*** 
      (0.11)   (0.04)  (0.07)  
exmkt 1.28***  0.97*** 0.98*** 
      (0.02)   (0.01)  (0.01)  
smb   0.37***  0.28*** -0.14***
      (0.03)   (0.01)  (0.02)  
hml   0.77***  0.46*** 0.69*** 
      (0.04)   (0.01)  (0.02)  
R2    0.86     0.95    0.88    
N     1044     1044    1044    
===============================
Standard errors in parentheses.
* p<.1, ** p<.05, ***p<.01
Run Code Online (Sandbox Code Playgroud)

另一个例子,这次显示model_names了自变量变化的选项和回归的使用:

reg3 = sm.OLS(p['p4'],p[['const','exmkt']]).fit()
reg4 = sm.OLS(p['p4'],p[['const','exmkt','smb','hml']]).fit()
reg5 = sm.OLS(p['p4'],p[['const','exmkt','smb','hml','umd']]).fit()

print summary_col([reg3,reg4,reg5],stars=True,float_format='%0.2f',
                  model_names=['p4\n(0)','p4\n(1)','p4\n(2)'],
                  info_dict={'N':lambda x: "{0:d}".format(int(x.nobs)),
                             'R2':lambda x: "{:.2f}".format(x.rsquared)})

==============================
         p4      p4       p4  
        (0)     (1)      (2)  
------------------------------
const 0.66*** 0.62***  0.15***
      (0.10)  (0.07)   (0.04) 
exmkt 1.10*** 0.98***  1.08***
      (0.02)  (0.01)   (0.01) 
hml           0.69***  0.72***
              (0.02)   (0.01) 
smb           -0.14*** 0.07***
              (0.02)   (0.01) 
umd                    0.46***
                       (0.01) 
R2    0.78    0.88     0.96   
N     1044    1044     1044   
==============================
Standard errors in
parentheses.
* p<.1, ** p<.05, ***p<.01
Run Code Online (Sandbox Code Playgroud)

要导出到LaTeX,请使用该as_latex方法.

我可能错了,但我认为没有实现t-stats的选项而不是标准错误(如在你的例子中).