Print 'std err' value from statsmodels OLS results
results.bse
provides standard errors for the coefficients, identical to those listed in results.summary()
.
The standard error of the regression is obtained using results.scale**.5
.
Also identical to np.sqrt(np.sum(results.resid**2)/results.df_resid)
, where results is your fitted model.
The following function can be used to get an overview of the regression analysis result. The parameter ols_model
is the regression model generated by statsmodels.formula.api
. The output is a pandas data frame saving the regression coefficient, standard errors, p values, number of observations, AIC, and adjusted rsquared. The standard errors are saved in brackets. ***
, **
, and *
represent 0.001, 0.01, 0.1 significance level:
def output_regres_result(ols_model, variable_list: list): """ Create a pandas dataframe saving the regression analysis result :param ols_model: a linear model containing the regression result. type: statsmodels.regression.linear_model.RegressionResultsWrapper :param variable_list: a list of interested variable names :return: a pandas dataframe saving the regression coefficient, pvalues, standard errors, aic, number of observations, adjusted r squared """ coef_dict = ols_model.params.to_dict() # coefficient dictionary pval_dict = ols_model.pvalues.to_dict() # pvalues dictionary std_error_dict = ols_model.bse.to_dict() # standard error dictionary num_observs = np.int(ols_model.nobs) # number of observations aic_val = round(ols_model.aic, 2) # aic value adj_rsqured = round(ols_model.rsquared_adj, 3) # adjusted rsqured info_index = ['Num', 'AIC', 'Adjusted R2'] index_list = variable_list + info_index for variable in variable_list: assert variable in coef_dict, 'Something wrong with variable name!' coef_vals = [] for variable in variable_list: std_val = std_error_dict[variable] coef_val = coef_dict[variable] p_val = pval_dict[variable] if p_val <= 0.01: coef_vals.append('{}***({})'.format(round(coef_val, 4), round(std_val, 3))) elif 0.01 < p_val <= 0.05: coef_vals.append('{}**({})'.format(round(coef_val, 4), round(std_val, 3))) elif 0.05 < p_val <= 0.1: coef_vals.append('{}*({})'.format(round(coef_val, 4), round(std_val, 3))) else: coef_vals.append('{}({})'.format(round(coef_val, 4), round(std_val, 3))) coef_vals.extend([num_observs, aic_val, adj_rsqured]) result_data = pd.DataFrame() result_data['coef'] = coef_vals result_data_reindex = result_data.set_index(pd.Index(index_list)) return result_data_reindex