Skip to content

Instantly share code, notes, and snippets.

@zhiyzuo
Created May 20, 2018 21:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhiyzuo/972b8b95e115c44d6805c929b7b4e2ca to your computer and use it in GitHub Desktop.
Save zhiyzuo/972b8b95e115c44d6805c929b7b4e2ca to your computer and use it in GitHub Desktop.
Obtain regression model coefficients from statsmodels
import pandas as pd
def get_coef_table(lin_reg):
''' lin_reg is a fitted statsmodels regression model
Return a dataframe containing coefficients, pvalues, and the confidence intervals
'''
err_series = lin_reg.params - lin_reg.conf_int()[0]
coef_df = pd.DataFrame({'coef': lin_reg.params.values[1:],
'ci_err': err_series.values[1:],
'pvalue': lin_reg.pvalues.round(4).values[1:],
'varname': err_series.index.values[1:]
})
return coef_df
@bbartling
Copy link

This is great thank you! Comes up google search for python stats models how to print coef values

@faglezm
Copy link

faglezm commented May 11, 2022

This has been such a great help. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment