Python中如何将模型输出写入Excel文件?

7
在运行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().to_excel 推送到Excel,但没有成功。我还创建了一个带有 mdf.summary() 的Pandas DataFrame,并尝试将其推送到Excel,但也没有成功。

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

我该如何将它传输到Excel?


你能解释一下当你说事情没有工作并且你得到什么错误/消息时的意思吗? - getglad
当我运行 mdf.summary().to_excel('./modelOutput.xlsx') 时,我会收到错误提示:AttributeError: 'Summary' object has no attribute 'to_excel' - dre
当我尝试运行 modelOutput = pd.DataFrame(mdf.summary()) modelOutput.to_excel('./modelOutput.xlsx') 时,出现了 ValueError: DataFrame constructor not properly called! 的错误提示。 - dre
1个回答

1

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摘要中增加选项的拉取请求。

我一直收到这个错误AttributeError: 'MixedLMResults' object has no attribute 'summary2' ... 这是我正在运行的代码,应该与您的相同:dfResults = mdf.summary2() len(dfResults.tables) - dre
尝试使用 summary() 复制您的示例时,我得到了以下错误:AttributeError: 'Summary' object has no attribute 'as_csv'以下是代码:model = smf.mixedlm('yVar ~ gas_prices + xVar', dfModel, groups = dfModel['region']) mdf = model.fit() res = mdf.summary() print(res.as_csv()) - dre
MixedLMResults.summary 内部实际上是一个 summary2,尝试使用 mdf.summary().tables - Josef
快了。我的输出只有一个逗号,所以我无法正确分隔:[ 0 1 2 3 0 模型: MixedLM 因变量: yVar 1 观测值数量: 420 方法: REML 5 平均组大小: 105.0, 系数 标准误 z P>|z| [0.025 0.975] 截距 3241461.947 112718.823 28.757 0.000 3020537.112 3462386.781 汽油价格 -118128.471 46931.809 -2.517 0.012 -210113.126 -26143.816 组 RE 0.002 ] - dre

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