将OLS回归摘要打印到文本文件 [英] Print OLS regression summary to text file

查看:43
本文介绍了将OLS回归摘要打印到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 groupby 和以下代码使用 pandas.stats.api.ols 运行OLS回归:

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.read_csv(r'F:\file.csv')

result=df.groupby(['FID']).apply(lambda d: ols(y=d.loc[:, 'MEAN'], x=d.loc[:, ['Accum_Prcp', 'Accum_HDD']]))
for i in result:
    x=pd.DataFrame({'FID':i.index, 'delete':i.values})
    frame = pd.concat([x,DataFrame(x['delete'].tolist())], axis=1, join='outer')
    del frame['delete']
    print frame

但这会返回错误:

AttributeError: 'OLS' object has no attribute 'index'

我的小组中大约有2,000件物品,当我打印出每件物品时,它们看起来像这样:

I have about 2,000 items in my group by and when I print each one out they look something like this:

-

------------------------Summary of Regression Analysis-------------------------

Formula: Y ~ <Accum_Prcp> + <Accum_HDD> + <intercept>

Number of Observations:         79
Number of Degrees of Freedom:   3

R-squared:         0.1242
Adj R-squared:     0.1012

Rmse:              0.1929

F-stat (2, 76):     5.3890, p-value:     0.0065

Degrees of Freedom: model 2, resid 76

-----------------------Summary of Estimated Coefficients------------------------
      Variable       Coef    Std Err     t-stat    p-value    CI 2.5%   CI 97.5%
--------------------------------------------------------------------------------
    Accum_Prcp     0.0009     0.0003       3.28     0.0016     0.0004     0.0015
     Accum_HDD     0.0000     0.0000       1.98     0.0516     0.0000     0.0000
     intercept     0.4750     0.0811       5.86     0.0000     0.3161     0.6340
---------------------------------End of Summary---------------------------------

我希望能够将每个导出到csv,以便可以分别查看它们.

I want to be able to export each one to a csv so that I can view them individually.

推荐答案

为了写出 pandas.stats.api.ols 结果,请使用文本文件以匹配输出格式,例如:

In order to write out the result of pandas.stats.api.ols, use a text file to match the output format, for instance:

from pandas.stats.api import ols
grps = df.groupby(['FID'])
for fid, grp in grps:
    result = ols(y=grp.loc[:, 'MEAN'], x=grp.loc[:, ['Accum_Prcp', 'Accum_HDD']])

    text_file = open("Output {}.txt".format(fid), "w")
    text_file.write(result.summary)
    text_file.close()

这篇关于将OLS回归摘要打印到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆