Print OLS regression summary to text file

前端 未结 2 2097
Happy的楠姐
Happy的楠姐 2021-02-15 10:29

I am running OLS regression using pandas.stats.api.ols using a groupby with the following code:

from pandas.stats.api import ols
df=pd.         


        
2条回答
  •  清歌不尽
    2021-02-15 10:56

    As of statsmodels 0.9, the Summary class supports export to multiple formats, including CSV and text:

    import numpy as np
    import statsmodels.api as sm
    import statsmodels.formula.api as smf
    
    dat = sm.datasets.get_rdataset("Guerry", "HistData").data
    results = smf.ols('Lottery ~ Literacy + np.log(Pop1831)', data=dat).fit()
    
    with open('summary.txt', 'w') as fh:
        fh.write(results.summary().as_text())
    
    with open('summary.csv', 'w') as fh:
        fh.write(results.summary().as_csv())
    

    The output of as_csv() is not machine-readable. Dumping results parameters with repr() would be.

提交回复
热议问题