Search code examples
pandaslinear-regressionpython-3.7statsmodels

Can I extract or construct as a Pandas dataframe the table with coefficient values etc. provided by the summary() method in statsmodels?


I have run an OLS model in statsmodels and I would like to have the table in the summary as a Pandas dataframe.

This is what I mean:

enter image description here

I would like the table within the red frame to be constructed / extracted and become a Pandas DataFrame.

My code up to that point was straightforward:

from statsmodels.regression.linear_model import OLS

mod = OLS(endog = coded_design_poly_select.response.values, exog = coded_design_poly_select.iloc[:, :-1].values)

fitted_model = mod.fit()

fitted_model.summary()

What would you suggest?


Solution

  • The fitted_model is in fact a RegressionResults object that stores all the regression results and you can access them via the corresponding methods/attributes.

    For what you asked for, I believe the following code would work

    data = {'coef': fitted_model.params,
            'std err': fitted_model.bse,
            't': fitted_model.tvalues,
            'P>|t|': fitted_model.pvalues,
            '[0.025': fitted_model.conf_int()[0],
            '0.975]': fitted_model.conf_int()[1]}
    
    pd.DataFrame(data).round(3)