gpt4 book ai didi

python - statsmodels:一起打印多个回归模型的摘要

转载 作者:行者123 更新时间:2023-11-28 20:04:50 39 4
gpt4 key购买 nike

在Python库Statsmodels中,可以用print(results.summary())打印出回归结果,如何打印出超过的摘要一张表中的一个回归,以便更好地比较?

线性回归,代码取自 statsmodels 文档:

nsample = 100
x = np.linspace(0, 10, 100)
X = np.column_stack((x, x**2))
beta = np.array([0.1, 10])
e = np.random.normal(size=nsample)
y = np.dot(X, beta) + e

model = sm.OLS(y, X)
results_noconstant = model.fit()

然后我向模型添加一个常量并再次运行回归:

beta = np.array([1, 0.1, 10])
X = sm.add_constant(X)
y = np.dot(X, beta) + e

model = sm.OLS(y, X)
results_withconstant = model.fit()

我希望看到 results_noconstantresults_withconstant 的摘要打印在一张表中。这应该是一个非常有用的功能,但我没有在statsmodels 文档中找到任何关于此的说明。

编辑:我想到的回归表类似于this。 ,我想知道是否有现成的功能可以做到这一点。

最佳答案

summary_col,文档中仍然缺少 AFAIR。

我并没有真正尝试太多,但我从一个问题中找到了一个相关的例子来删除一些“讨厌的”参数。

"""
mailing list, and issue https://github.com/statsmodels/statsmodels/pull/1638
"""

import pandas as pd
import numpy as np
import string
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col

df = pd.DataFrame({'A' : list(string.ascii_uppercase)*10,
'B' : list(string.ascii_lowercase)*10,
'C' : np.random.randn(260),
'D' : np.random.normal(size=260),
'E' : np.random.random_integers(0,10,260)})

m1 = smf.ols('E ~ D',data=df).fit()
m2 = smf.ols('E ~ D + C',data=df).fit()
m3 = smf.ols('E ~ D + C + B',data=df).fit()
m4 = smf.ols('E ~ D + C + B + A',data=df).fit()

print(summary_col([m1,m2,m3,m4]))

还有改进的空间。

关于python - statsmodels:一起打印多个回归模型的摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051673/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com