[英]Converting statsmodels summary object to Pandas Dataframe
I am doing multiple linear regression with statsmodels.formula.api
(ver 0.9.0) on Windows 10. After fitting the model and getting the summary with following lines i get summary in summary object format.
X_opt = X[:, [0,1,2,3]]
regressor_OLS = sm.OLS(endog= y, exog= X_opt).fit()
regressor_OLS.summary()
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.951
Model: OLS Adj. R-squared: 0.948
Method: Least Squares F-statistic: 296.0
Date: Wed, 08 Aug 2018 Prob (F-statistic): 4.53e-30
Time: 00:46:48 Log-Likelihood: -525.39
No. Observations: 50 AIC: 1059.
Df Residuals: 46 BIC: 1066.
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 5.012e+04 6572.353 7.626 0.000 3.69e+04 6.34e+04
x1 0.8057 0.045 17.846 0.000 0.715 0.897
x2 -0.0268 0.051 -0.526 0.602 -0.130 0.076
x3 0.0272 0.016 1.655 0.105 -0.006 0.060
==============================================================================
Omnibus: 14.838 Durbin-Watson: 1.282
Prob(Omnibus): 0.001 Jarque-Bera (JB): 21.442
Skew: -0.949 Prob(JB): 2.21e-05
Kurtosis: 5.586 Cond. No. 1.40e+06
==============================================================================
我想对显着性水平 0.05 的 P 值进行反向消除。 为此,我需要删除具有最高 P 值的预测器并再次运行代码。
我想知道是否有办法从摘要 object 中提取 P 值,这样我就可以使用条件语句运行循环并找到重要变量,而无需手动重复这些步骤。
谢谢你。
@Michael B 的答案效果很好,但需要“重新创建”表格。 表格本身实际上可以直接从 summary().tables 属性中获得。 此属性中的每个表(即表列表)都是一个SimpleTable ,它具有用于输出不同格式的方法。 然后我们可以读取任何这些格式作为 pd.DataFrame:
import statsmodels.api as sm
model = sm.OLS(y,x)
results = model.fit()
results_summary = results.summary()
# Note that tables is a list. The table at index 1 is the "core" table. Additionally, read_html puts dfs in a list, so we want index 0
results_as_html = results_summary.tables[1].as_html()
pd.read_html(results_as_html, header=0, index_col=0)[0]
将您的模型拟合存储为变量results
,如下所示:
import statsmodels.api as sm
model = sm.OLS(y,x)
results = model.fit()
然后创建一个像下面这样的函数:
def results_summary_to_dataframe(results):
'''take the result of an statsmodel results table and transforms it into a dataframe'''
pvals = results.pvalues
coeff = results.params
conf_lower = results.conf_int()[0]
conf_higher = results.conf_int()[1]
results_df = pd.DataFrame({"pvals":pvals,
"coeff":coeff,
"conf_lower":conf_lower,
"conf_higher":conf_higher
})
#Reordering...
results_df = results_df[["coeff","pvals","conf_lower","conf_higher"]]
return results_df
您可以通过使用dir()打印来进一步探索results
对象的所有属性,然后将它们相应地添加到函数和 df 中。
一个简单的解决方案只是一行代码:
LRresult = (result.summary2().tables[1])
正如 ZaxR 在以下评论中提到的,Summary2 尚未被认为是稳定的,但它也适用于 Summary。 所以这可能是正确的答案:
LRresult = (result.summary().tables[1])
这将为您提供一个数据框对象:
type(LRresult)
pandas.core.frame.DataFrame
要获取重要变量并再次运行测试:
newlist = list(LRresult[LRresult['P>|z|']<=0.05].index)[1:]
myform1 = 'binary_Target' + ' ~ ' + ' + '.join(newlist)
M1_test2 = smf.logit(formula=myform1,data=myM1_1)
result2 = M1_test2.fit(maxiter=200)
LRresult2 = (result2.summary2().tables[1])
LRresult2
你可以写如下。这将是一个简单的修复并且几乎每次都能正常工作。
lr.summary2()
下面的代码将所有指标放入一个可通过 key 访问的字典中。 中间结果实际上是您可以使用的DataFrame
,我没有将系数制作成dictionary
,但是您可以应用类似的方法,然后是两层深dict[var][metric]
。
为了使键易于键入,我将一些度量标准名称转换为更易于键入的版本。 例如,"Prob(Omnibus):" 变为 prob_omnibus,这样您就可以通过 res_dict['prob_omnibus'] 访问该值。
import pandas as pd
res = sm.OLS(y, X).fit()
model_results_df = []
coefficient_df = None
for i, tab in enumerate(res.summary().tables):
header, index_col = None, None
if i == 1:
coefficient_df = pd.read_html(tab.as_html(), header=0, index_col=0)[0]
else:
df = pd.read_html(tab.as_html())[0]
model_results_df += [df.iloc[:,0:2], df.iloc[:,2:4]]
model_results_df = pd.DataFrame(np.concatenate(model_results_df), columns=['metric', 'value'])
model_results_df.dropna(inplace=True, axis=0)
model_results_df.metric = model_results_df.metric.apply(lambda x : x.lower().replace(' (', '_')
.replace('.', '').replace('(', '_')
.replace(')', '').replace('-', '_')
.replace(':', '').replace(' ', '_'))
res_dict = dict(zip(model_results_df.metric.values, model_results_df.value.values))
res_dict['f_statistic']
如果你想要周围的信息,请尝试以下操作:
import pandas as pd
dfs = {}
fs = fa_model.summary()
for item in fs.tables[0].data:
dfs[item[0].strip()] = item[1].strip()
dfs[item[2].strip()] = item[3].strip()
for item in fs.tables[2].data:
dfs[item[0].strip()] = item[1].strip()
dfs[item[2].strip()] = item[3].strip()
dfs = pd.Series(dfs)
我仍然认为没有一个清晰的答案可以完整地捕获查询。 这是捕获两个数据帧中所有内容的一种方法(一种用于中间表,一种用于顶部和底部的指标)。
def reform_df(dft):
# quick and dirty stacking of cols 2,3 on 0,1
dfl = dft[[0,1]]
dfr = dft[[2,3]]
dfr.columns = 0,1
dfout = pd.concat([dfl,dfr])
dfout.columns=['Parameter','Value']
return dfout
def model_summary_to_dataframe(model):
# first the middle table
results_df = pd.DataFrame(model.summary().tables[1])
results_df = results_df.set_index(0)
results_df.columns = results_df.iloc[0]
results_df = results_df.iloc[1:]
results_df.index.name='Parameter'
# now for the surrounding information
metrics_top = reform_df(pd.DataFrame(model.summary().tables[0]))
metrics_bot = reform_df(pd.DataFrame(model.summary().tables[2]))
metrics_df = pd.concat([metrics_top,metrics_bot])
return pd.DataFrame(results_df),metrics_df
它有效,但我在 item[3] 中发现了一个小错误 这是修复:
import pandas as pd
dfs = {}
fs = stepwise_fit.summary()
for item in fs.tables[0].data:
#print("item " + str(item))
dfs[item[0].strip()] = item[1].strip()
dfs[item[2].strip()] = str(item[3]).strip()
for item in fs.tables[2].data:
dfs[item[0].strip()] = item[1].strip()
dfs[item[2].strip()] = str(item[3]).strip()
dfs = pd.Series(dfs)
print(type(dfs))
这对我有用:
st, data, ss2 = summary_table(result, alpha=0.05)
df = pd.DataFrame( data=data, columns=ss2 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.