Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model Output `to_excel` in Python?

Running a MixedLM and want to push the output to Excel or a CSV, see model code and output below:

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                                                
======================================================================

I've tried pushing mdf.summary().to_excel and that didn't work, in addition to creating a Pandas DataFrame with mdf.summary() and then pushing to Excel, that doesn't work either.

The extra credit piece here would be to create a unique filename for each output in Excel, so that if I run a few models they won't overwrite each other.

How can I get this to Excel?

like image 328
dre Avatar asked May 19 '18 12:05

dre


People also ask

How do I get python output in Excel?

Here's how to use Python to output an Excel-compatible CSV (actually TSV) file from a command-line script. You can then use this pattern: "python csv_example.py | pbcopy" to copy the output to your clipboard, then just focus on a cell in Excel or Google Sheets and hit "paste" to copy the data into a bunch of cells.

How do I convert DF to excel in python?

Use pandas to_excel() function to write a DataFrame to an excel sheet with extension . xlsx. By default it writes a single DataFrame to an excel file, you can also write multiple sheets by using an ExcelWriter object with a target file name, and sheet name to write to.


1 Answers

statsmodels has two underlying function for building summary tables. Some models use one or the other, some models have both summary() and summary2() methods in the results instance available.

MixedLM uses summary2 as summary which builds the underlying tables as pandas DataFrames.

I don't have a mixed effects model available right now, so this is for a GLM model results instance res1

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

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

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

These two tables can be used with pandas as in the deleted answer to create excel files.

The summary implementation, which is not available in MixedLM but is the default summary for most other models has a as_csv method, which however uses the same precision as in the string version. The summary version currently does not build an underlying 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

(Pull request for additional option in statsmodels summaries are welcome.)

like image 88
Josef Avatar answered Oct 04 '22 21:10

Josef