python - Python 中的模型输出 `to_excel`?

标签 python python-3.x pandas statsmodels

运行 MixedLM 并希望将输出推送到 Excel 或 CSV,请参阅下面的模型代码和输出:

model = smf.mixedlm('y_var ~ gas_prices', dfModel, 
                 groups = dfModel['region'])
mdf = model.fit()
print(mdf.summary())

                Mixed Linear Model Regression Results
======================================================================
Model:                MixedLM   Dependent Variable:   yVar 
No. Observations:     420       Method:               REML            
No. Groups:           4         Scale:                45635645671.2271
Min. group size:      105       Likelihood:           -5720.8133      
Max. group size:      105       Converged:            Yes             
Mean group size:      105.0                                           
----------------------------------------------------------------------
              Coef.     Std.Err.    z    P>|z|    [0.025      0.975]  
----------------------------------------------------------------------
Intercept  3241461.947 112718.823 28.757 0.000 3020537.112 3462386.781
gas_prices -118128.471  46931.809 -2.517 0.012 -210113.126  -26143.816
xVar2          275.017    165.072  1.666 0.096     -48.518     598.553
groups RE        0.002                                                
======================================================================

除了使用 mdf.summary() 创建 Pandas DataFrame 和然后推送到 Excel,这也不起作用。

这里额外的功劳是在 Excel 中为每个输出创建一个唯一的文件名,这样如果我运行几个模型,它们就不会相互覆盖。

我怎样才能将其转换为 Excel?

最佳答案

statsmodels 有两个用于构建汇总表的基础函数。有些模型使用其中一种,有些模型在结果实例中同时提供 summary()summary2() 方法。

MixedLM 使用 summary2 作为 summary 将基础表构建为 pandas DataFrames。

我现在没有可用的混合效果模型,所以这是针对 GLM 模型结果实例 res1

>>> summ2 = res1.summary2()
>>> len(summ2.tables)
2

>>> type(summ2.tables[1])
pandas.core.frame.DataFrame

>>> type(summ2.tables[0])
pandas.core.frame.DataFrame

这两个表可以与 pandas 一起使用,如删除的答案中创建 excel 文件。

summary 实现在 MixedLM 中不可用,但它是大多数其他模型的默认摘要,它有一个 as_csv 方法,但是它使用与字符串版本。 summary 版本目前不构建底层 DataFrame。

>>> summ = res1.summary()
>>> print(summ.as_csv())
          Generalized Linear Model Regression Results           
Dep. Variable: ,['y1', 'y2']    ,  No. Observations:  ,   303   
Model:         ,GLM             ,  Df Residuals:      ,   282   
Model Family:  ,Binomial        ,  Df Model:          ,    20   
Link Function: ,logit           ,  Scale:             ,  1.0000 
Method:        ,IRLS            ,  Log-Likelihood:    , -2998.6 
Date:          ,Sat, 19 May 2018,  Deviance:          ,  4078.8 
Time:          ,08:42:45        ,  Pearson chi2:      ,4.05e+03 
No. Iterations:,5               ,  Covariance Type:   ,nonrobust
     ,   coef   , std err ,    z    ,P>|z| ,  [0.025 ,  0.975] 
x1   ,   -0.0168,    0.000,  -38.749, 0.000,   -0.018,   -0.016
x2   ,    0.0099,    0.001,   16.505, 0.000,    0.009,    0.011
x3   ,   -0.0187,    0.001,  -25.182, 0.000,   -0.020,   -0.017
x4   ,   -0.0142,    0.000,  -32.818, 0.000,   -0.015,   -0.013
x5   ,    0.2545,    0.030,    8.498, 0.000,    0.196,    0.313
x6   ,    0.2407,    0.057,    4.212, 0.000,    0.129,    0.353
x7   ,    0.0804,    0.014,    5.775, 0.000,    0.053,    0.108
x8   ,   -1.9522,    0.317,   -6.162, 0.000,   -2.573,   -1.331
x9   ,   -0.3341,    0.061,   -5.453, 0.000,   -0.454,   -0.214
x10  ,   -0.1690,    0.033,   -5.169, 0.000,   -0.233,   -0.105
x11  ,    0.0049,    0.001,    3.921, 0.000,    0.002,    0.007
x12  ,   -0.0036,    0.000,  -15.878, 0.000,   -0.004,   -0.003
x13  ,   -0.0141,    0.002,   -7.391, 0.000,   -0.018,   -0.010
x14  ,   -0.0040,    0.000,   -8.450, 0.000,   -0.005,   -0.003
x15  ,   -0.0039,    0.001,   -4.059, 0.000,   -0.006,   -0.002
x16  ,    0.0917,    0.015,    6.321, 0.000,    0.063,    0.120
x17  ,    0.0490,    0.007,    6.574, 0.000,    0.034,    0.064
x18  ,    0.0080,    0.001,    5.362, 0.000,    0.005,    0.011
x19  ,    0.0002, 2.99e-05,    7.428, 0.000,    0.000,    0.000
x20  ,   -0.0022,    0.000,   -6.445, 0.000,   -0.003,   -0.002
const,    1.9589,    1.547,    1.266, 0.205,   -1.073,    4.990

(欢迎请求 statsmodels 摘要中的附加选项。)

关于python - Python 中的模型输出 `to_excel`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425087/

相关文章:

python - 一一获取查询集数据

python - 从 python 对象获取函数的输入

python - random.shuffle() 的倒数?

python - 读取 excel 并将索引转换为 datetimeindex Pandas

python - 识别非连续零的索引值

python - Pandas Dataframe 在网页上显示

python - python中是否有一个函数可以替换字符串中的字符,反之亦然?

python - pip3.4 -V指的是python2.7的安装

python - 在 Python 控制台中终止多行字符串

python - 写入 csv,每个项目位于单独的列中