0
$\begingroup$

I'm using Python statsmodel to do logistic regression. I'm trying out their glm(family=sm.families.Binomial()) and logit() models. Please correct me if I'm wrong but technically they should be the same model.

Here are the full sample code for reference

glm_model = sm.formula.glm("Y ~ X1 + X2 + ... + Xn", family=sm.families.Binomial(), data=df_train).fit()

enter image description here

logit_model = sm.formula.logit("Y ~ X1 + X2 + ... + Xn", data=df_train).fit()

enter image description here

So 2 things

  1. Why are the coefficients between the 2 models inverted? I assume the logit model one makes more sense (in the context of the training data), but I'm curious if there's an argument in the glm() function which I'm missing

  2. Why do some coefficients in the logit model have nan p-value while the glm model doesn't?

Thanks for your help! I normally use R but I'm moving to Python now. If I replicate this in R it does mimic the result of the logit model here, but without nan p-values.

$\endgroup$
3
  • 2
    $\begingroup$ Welcome to CV, Orvin. Here's an educated guess: your response variable is Boolean, with true coded as a binary 11...1B, which one program interprets as "true" and codes to 1 and the other interprets as the signed integer -1. (That, at least, could be a working theory for reading the documentation.) You don't supply any information to help us diagnose the nan values -- you don't even show the names of the variables, much less their meanings or any of their statistical properties. $\endgroup$
    – whuber
    Commented Mar 30, 2023 at 21:36
  • 2
    $\begingroup$ Ah yes, the Y was categorical data type (with only 2 levels, 1 and 0). I changed it to numeric and both logit and glm coefficients are now consistent, and the p-values are not nulls anymore, thanks! $\endgroup$ Commented Mar 30, 2023 at 23:17
  • 1
    $\begingroup$ some details how categorical y are interpreted in formulas stackoverflow.com/questions/48312979/… (and several statsmodels issues) $\endgroup$
    – Josef
    Commented Mar 31, 2023 at 2:51

1 Answer 1

1
$\begingroup$

the Y was categorical data type (with only 2 levels, 1 and 0). I changed it to numeric and both logit and glm coefficients are now consistent, and the p-values are not nulls anymore. Thanks to @whuber

$\endgroup$
1
  • 3
    $\begingroup$ It may be helpful to show the code you used to obtain this result so others who see this answer down the road know what you did to solve the question. $\endgroup$ Commented Mar 30, 2023 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.