I am doing a Logistic regression in python using sm.Logit, then to get the model, the p-values, etc is the functions .summary, I want t storage the result from the .summary function, so far I have:

  • .params.values : give the beta value
  • .params : give the name of the variable and the beta value
  • .conf_int() : give the confidence interval

I still need to get the std err, z and the p-value

I am also wondering is there a way to get this (first part of the .summary function):

enter image description here

2

Best Answer


If you calculate your model using code like

model = sm.Logit(y_data, x_data)model_fit = model.fit()

then you can access the p-values directly with model_fit.pvalues.

For standard error for the coefficients, you can call

cov = model_fit.cov_params()std_err = np.sqrt(np.diag(cov))

to calculate the standard error as the square root of the variance terms (diagonal entries) of the model's covariance matrix estimate.

The z value is defined as each coefficient divided by its standard error, so you can calculate this as

z_values = model_fit.params / std_err

using std_err defined in the line above.

Regarding your last question, it's not clear if you are asking how to get the actual substring from the summary() output, or individually get all of the different pieces of data it prints.

If you want them individually, I suggest fitting your model in an interactive programming session with tab completion, like with jupyter, so that you can see the various degree of freedom options and other data available on the model_fit object.

If you're just looking for the strings, it's as simple as:

'\n'.join(str(model_fit.summary()).split('\n')[1:10])

Write this line of code print(dir(model_fit)) in your code it shows you all functions and methods you can access from your model_fit answer.