# Verifying my implementation of Logistic Regression spline

I am using splines to test the linearity assumption of Logistic Regression as per what is mentioned in this website: https://www.statisticssolutions.com/logistic-regression-assumptions/ :

Linearity with an ordinal or interval independent variable and the odds ratio can be checked by creating a new variable that divides the existing independent variable into categories of equal intervals and running the same regression on these newly categorized versions as categorical variables. Linearity is demonstrated if the beta coefficients increase or decrease in linear steps (Garson, 2009).

I just want to check that my implementation is right. I am using Python. My steps are:

1. Cut my continuous variable into 4 bins (using pd.cut)
2. Convert this into dummy variables so that I end up with 4 categoric variables that are either 0 or 1 depending on if the variable value originally fell into that bin
3. Perform a logistic regression on each categoric variable and obtain the beta parameter - when doing the regression I fit each categoric variable to my list of classes

I just performed this for one variable (this actually had 3 bins) and I got: -2.46, -0.25, 2.099. Given that the interval between each beta coefficient differs by 0.4 is this enough to say that my continuous variable doesn't break the linear assumption?

Would it be better to use more splines? Also, what is a good amount of samples to have in each bin? Does this even matter?

Thanks!

You could use a Generalize Additive Model, where you estimate the functional form of the relationship between your independent and dependent variable, i.e. $$\log \frac{p_i}{1 - p_i} = f(x_i)$$. If you are working in python I would recommend having a look at the pyGAM library.
• Hi, thanks for the response! I'm sorry if this is a dumb question but are you able to expand on this part more: estimate the functional form of the relationship between your independent and dependent variable, i.e. log$\frac{p_i}{1-p_i} = f(x_i)$ - I know that equation is what you get from Logistic Regression if you assume linearity. Is this what I should be looking for in the output of the GAM? Or will the GAM just give the functional form and if it is non linear then the assumption is violated? Dec 2 '20 at 11:44
• If the relationship is linear, then the output from the GAM will look like a straight line, i.e. the estimated $f$ will look like $\beta x$. The equation is the same as in the linear case, but also take into account that the relationship might be non-linear. If the GAM gives you a non-linear $f$, then I would say that the relationship is not linear. You could also compare (for example) the AIC of a logistic regression with $f$ and one with a linear trent $\beta x$. Dec 2 '20 at 12:03