我使用statsmodels创建一些回归输出:
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col
import numpy as np
import pandas as pd
x1 = pd.Series(np.random.randn(2000))
x2 = pd.Series(np.random.randn(2000))
aa_milne_arr = ['a', 'b', 'c', 'd', "e", "f", "g", "h", "i"]
dummy = pd.Series(np.random.choice(aa_milne_arr, 2000,))
depen = pd.Series(np.random.randn(2000))
df = pd.DataFrame({"y": depen, "x1": x1, "x2": x2, "dummy": dummy})
df['const'] = 1
df['xsqr'] = df['x1']**2
mod = smf.ols('y ~ x1 + x2 + dummy', data=df)
mod2 = smf.ols('y ~ x1 + x2 + xsqr + dummy', data=df)
res = mod.fit()
res2 = mod2.fit()
print (summary_col([res,res2],stars=True,float_format='%0.3f',
model_names=['one\n(0)','two\n(1)'],
info_dict={'N':lambda x: "{0:d}".format(int(x.nobs)),
'R2':lambda x: "{:.2f}".format(x.rsquared)}))
它工作得很好,但是我有一个很大的数据集,其中有许多虚拟对象(比示例中的数据集多得多)。因此,我想从摘要输出(而不是从回归本身)中排除虚拟对象。有可能吗?在
一种快速而肮脏的方法是首先在最终的
summary_col
中找到这些dummy
索引,然后避免打印它们:不漂亮,但很管用。使用字符串格式:
^{pr2}$使用乳胶格式:
相关问题 更多 >
编程相关推荐