线性模型 panelOLS:带星号的回归输出

7
我正在使用 linearmodels 包估计 Panel-OLS。例如:
import numpy as np
from statsmodels.datasets import grunfeld
data = grunfeld.load_pandas().data
data.year = data.year.astype(np.int64)
# MultiIndex, entity - time
data = data.set_index(['firm','year'])
from linearmodels import PanelOLS
mod = PanelOLS(data.invest, data[['value','capital']], entity_effect=True)
res = mod.fit(cov_type='clustered', cluster_entity=True)

我想将回归结果输出到.tex文件中。是否有方便的方式可以格式化输出结果,包括置信星但不包括其他信息,例如置信区间?这个问题在标准OLS上下文中已经被提出here,但是对于“PanelEffectsResults”对象,则会出现以下错误:

'PanelEffectsResults' object has no attribute 'bse'

提前致谢。


2
你找到解决方案了吗? - Chris
2个回答

2

我已经为同行们寻找了几天,终于找到了一个非常简单的方法来解决同样的问题:包括显著性星号,去除CIs。

以下是具体步骤:

第一步:安装linearmodels包。

第二步:从linearmodels.panel中导入compare函数。

from linearmodels.panel import compare

步骤3:使用比较函数,并根据您需要在比较中指定参数。例如,指定stars = True将为您提供显著性星号。非常方便!
compare({'model_A_name': results of model_A, 'model_B_name': results of model_B, }, stars = True)

这个小函数救了我的命!享受它吧。
还有一件事,请注意,星号基于系数的p值,其中1、2和3星对应的p值分别为10%、5%和1%。我不确定是否有一种方法可以制定自定义的星级测量方式,例如1、2和3星对应的p值分别为5%、1%和0.1%。
功劳归于优秀的包开发者和维护者。感谢你们!请查看文件并在此处获得更多信息: ~/opt/anaconda3/lib/python3.7/site-packages/linearmodels/panel/results.py

2

有点晚了,但这是我使用的方法。在上面的例子中,我计算了两个固定效应回归,并将它们的结果存储在fe_res_VSfe_res_CVS中:

pd.set_option('precision', 4)
pd.options.display.float_format = '{:,.4f}'.format
Reg_Output_FAmount= pd.DataFrame()


#1) 
Table1 = pd.DataFrame(fe_res_VS.params)
Table1['id'] = np.arange(len(Table1))#create numerical index for pd.DataFrame
Table1 = Table1.reset_index().set_index(keys = 'id')#set numercial index as new index
Table1 = Table1.rename(columns={"index":"parameter", "parameter":"coefficient 1"})

P1 = pd.DataFrame(fe_res_VS.pvalues)
P1['id'] = np.arange(len(P1))#create numerical index for pd.DataFrame
P1 = P1.reset_index().set_index(keys = 'id')#set numercial index as new index
P1 = P1.rename(columns={"index":"parameter"})

Table1 = pd.merge(Table1, P1, on='parameter')
Table1['significance 1'] = np.where(Table1['pvalue'] <= 0.01, '***',\
       np.where(Table1['pvalue'] <= 0.05, '**',\
       np.where(Table1['pvalue'] <= 0.1, '*', '')))
Table1.rename(columns={"pvalue": "pvalue 1"}, inplace=True) 


SE1 = pd.DataFrame(fe_res_VS.std_errors)
SE1['id'] = np.arange(len(SE1))#create numerical index for pd.DataFrame
SE1 = SE1.reset_index().set_index(keys = 'id')#set numercial index as new index
SE1 = SE1.rename(columns={"index":"parameter", "std_error":"coefficient 1"})
SE1['parameter'] =  SE1['parameter'].astype(str) + '_SE'
SE1['significance 1'] = ''
SE1 = SE1.round(4)

SE1['coefficient 1'] = '(' + SE1['coefficient 1'].astype(str) + ')'
Table1 = Table1.append(SE1)
Table1 = Table1.sort_values('parameter')
Table1.replace(np.nan,'', inplace=True)
del P1
del SE1


#2) 
Table2 = pd.DataFrame(fe_res_CVS.params)
Table2['id'] = np.arange(len(Table2))#create numerical index for pd.DataFrame
Table2 = Table2.reset_index().set_index(keys = 'id')#set numercial index as new index
Table2 = Table2.rename(columns={"index":"parameter", "parameter":"coefficient 2"})

P2 = pd.DataFrame(fe_res_CVS.pvalues)
P2['id'] = np.arange(len(P2))#create numerical index for pd.DataFrame
P2 = P2.reset_index().set_index(keys = 'id')#set numercial index as new index
P2 = P2.rename(columns={"index":"parameter"})

Table2 = pd.merge(Table2, P2, on='parameter')
Table2['significance 2'] = np.where(Table2['pvalue'] <= 0.01, '***',\
       np.where(Table2['pvalue'] <= 0.05, '**',\
       np.where(Table2['pvalue'] <= 0.1, '*', '')))
Table2.rename(columns={"pvalue": "pvalue 2"}, inplace=True) 

SE2 = pd.DataFrame(fe_res_CVS.std_errors)
SE2['id'] = np.arange(len(SE2))#create numerical index for pd.DataFrame
SE2 = SE2.reset_index().set_index(keys = 'id')#set numercial index as new index
SE2 = SE2.rename(columns={"index":"parameter", "std_error":"coefficient 2"})
SE2['parameter'] =  SE2['parameter'].astype(str) + '_SE'
SE2['significance 2'] = ''
SE2 = SE2.round(4)

SE2['coefficient 2'] = '(' + SE2['coefficient 2'].astype(str) + ')'
Table2 = Table2.append(SE2)
Table2 = Table2.sort_values('parameter')
Table2.replace(np.nan,'', inplace=True)
del P2
del SE2


#Merging Tables and adding Stats
Reg_Output_FAmount= pd.merge(Table1, Table2, on='parameter', how='outer')

Reg_Output_FAmount = Reg_Output_FAmount.append(pd.DataFrame(np.array([["observ.", fe_res_VS.nobs, '', fe_res_CVS.nobs, '']]), columns=['parameter', 'pvalue 1', 'significance 1', 'pvalue 2', 'significance 2']), ignore_index=True)
Reg_Output_FAmount = Reg_Output_FAmount.append(pd.DataFrame(np.array([["Rsquared", "{:.4f}".format(fe_res_VS.rsquared), '',  "{:.4f}".format(fe_res_CVS.rsquared), '']]), columns=['parameter', 'pvalue 1', 'significance 1', 'pvalue 2', 'significance 2']), ignore_index=True)
Reg_Output_FAmount= Reg_Output_FAmount.append(pd.DataFrame(np.array([["Model type", fe_res_VS.name, '', fe_res_CVS.name, '']]), columns=['parameter', 'pvalue 1', 'significance 1', 'pvalue 2', 'significance 2']), ignore_index=True)
Reg_Output_FAmount = Reg_Output_FAmount.append(pd.DataFrame(np.array([["DV", fe_res_VS.model.dependent.vars[0], '', fe_res_CVS.model.dependent.vars[0], '']]), columns=['parameter', 'pvalue 1', 'significance 1', 'pvalue 2', 'significance 2']), ignore_index=True)

Reg_Output_FAmount.fillna('', inplace=True)


导致一个漂亮的回归输出结果,看起来像这样:
    parameter   coefficient 1   pvalue 1    significance 1  coefficient 2   pvalue 2   significance 2
0   IV          0.0676          0.2269                      0.0732          0.1835  
1   IV_SE       (0.0559)                                    (0.055)     
2   Control     0.3406          0.0125      **              0.3482          0.0118      **
3   Control_SE  (0.1363)        0.1383)     
4   const       0.2772          0.0000      ***             0.2769          0.0000      ***
5   const_SE    (0.012)         (0.012)     
6   observ.                     99003                                       99003   
7   Rsquared                    0.12                                        0.14    
8   Model type                  PanelOLS                                    PanelOLS    
9   DV                          FAmount                                     FAmount

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接