liveBook · Manning

Logit

As explained in Statistics Every Programmer Needs

logit is a statsmodels formula API function that constructs a logistic regression model from a formula and data.

logit is a statsmodels formula API function for specifying a logistic regression from a dependent variable, predictors, and a data frame. Calling fit() estimates model parameters, while summary() reports coefficients, p-values, and goodness-of-fit statistics. section 5.2.2

Raisin variety classification

A model regresses Class against morphological measurements including Area, MajorAxisLength, MinorAxisLength, Eccentricity, ConvexArea, Extent, and Perimeter. The overall LLR p-value is 0, supporting discriminatory value, but Eccentricity and Extent have nonsignificant p-values and motivate model reduction. section 5.2.2

>>> import statsmodels.formula.api as smf
>>> log_model = smf.logit('Class ~ Area + '
>>>                   'MajorAxisLength + '
>>>                   'MinorAxisLength + '
>>>                   'Eccentricity + '
>>>                   'ConvexArea + '
>>>                   'Extent + '
>>>                   'Perimeter', data = raisins).fit()
>>> print(log_model.summary())
Optimization terminated successfully.
         Current function value: 0.338384
         Iterations 9
                           Logit Regression Results                           
===========================================================================
Dep. Variable:               Class   No. Observations:                  900
Model:                       Logit   Df Residuals:                      892
Method:                        MLE   Df Model:                            7
Date:             Wed, 27 Mar 2024   Pseudo R-squ.:                  0.5118
Time:                     15:03:56   Log-Likelihood:                -304.55
converged:                    True   LL-Null:                       -623.83
Covariance Type:         nonrobust   LLR p-value:                1.196e-133
===========================================================================

              coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------
Intercept  -2.6642      7.028     -0.379      0.705     -16.439      11.111
Area        0.0005      0.000      4.027      0.000       0.000       0.001
MajorAxisLength
           -0.0447      0.016     -2.801      0.005      -0.076      -0.013
MinorAxisLength    
           -0.0899      0.027     -3.342      0.001      -0.143      -0.037
Eccentricity      
           -3.5248      4.909     -0.718      0.473     -13.147       6.097
ConvexArea         
           -0.0004      0.000     -3.446      0.001      -0.001      -0.000
Extent     -0.6051      2.697     -0.224      0.822      -5.891       4.681
Perimeter   0.0361      0.007      5.464      0.000       0.023       0.049

Inspect the complete formula, fitting call, optimization output, and regression summary for the multiple logistic model. section 5.2.2

How Statistics Every Programmer Needs covers it

How deep it goes
Explains formula specification, maximum-likelihood fitting, summary output, significance testing, and subsequent predictor reduction.
Examples
Uses raisin morphology predictors to classify raisin variety and compares a full model with a reduced model.
What it assumes
Assumes a data frame with a binary response and candidate predictors, while demonstrating formula construction and model-output interpretation.
What you can do after
Apply it via statsmodels to fit, summarize, evaluate, and reduce a formula-based logistic regression.
How it connects
Connects logistic regression with maximum likelihood, p-values, goodness of fit, multicollinearity, and variance inflation factors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.